Skip to content

Commit 594fdfc

Browse files
committed
Use timedelta for timeout
1 parent 6956550 commit 594fdfc

File tree

5 files changed

+63
-26
lines changed

5 files changed

+63
-26
lines changed

src/SeleniumLibrary/base/librarycomponent.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@
1515
# limitations under the License.
1616

1717
import os
18-
from typing import Optional
18+
from datetime import timedelta
19+
from typing import Optional, Union
1920

2021
from robot.api import logger
2122
from robot.libraries.BuiltIn import BuiltIn, RobotNotRunningError
2223

23-
from SeleniumLibrary.utils import is_noney, timestr_to_secs
24+
from SeleniumLibrary.utils import is_noney
2425

2526
from .context import ContextAware
2627

@@ -74,10 +75,12 @@ def assert_page_not_contains(
7475
raise AssertionError(message)
7576
logger.info(f"Current page does not contain {tag_message} '{locator}'.")
7677

77-
def get_timeout(self, timeout: Optional[str] = None) -> float:
78-
if is_noney(timeout):
78+
def get_timeout(self, timeout: Union[str, int, timedelta, None] = None) -> float:
79+
if timeout is None:
7980
return self.ctx.timeout
80-
return timestr_to_secs(timeout)
81+
if isinstance(timeout, timedelta):
82+
return timeout.total_seconds()
83+
return timeout
8184

8285
@property
8386
def log_dir(self):

src/SeleniumLibrary/keywords/alert.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
16-
from typing import Union
16+
from datetime import timedelta
17+
from typing import Union, Optional
1718

1819
from selenium.common.exceptions import TimeoutException, WebDriverException
1920
from selenium.webdriver.support import expected_conditions as EC
@@ -31,7 +32,7 @@ class AlertKeywords(LibraryComponent):
3132

3233
@keyword
3334
def input_text_into_alert(
34-
self, text: str, action: str = ACCEPT, timeout: Union[float, str, None] = None
35+
self, text: str, action: str = ACCEPT, timeout: Optional[timedelta] = None
3536
):
3637
"""Types the given ``text`` into an input field in an alert.
3738
@@ -52,7 +53,7 @@ def alert_should_be_present(
5253
self,
5354
text: str = "",
5455
action: str = ACCEPT,
55-
timeout: Union[float, str, None] = None,
56+
timeout: Optional[timedelta] = None,
5657
):
5758
"""Verifies that an alert is present and by default, accepts it.
5859
@@ -68,6 +69,7 @@ def alert_should_be_present(
6869
In earlier versions, the alert was always accepted and a timeout was
6970
hardcoded to one second.
7071
"""
72+
self.info(f"{type(timeout)}::{timeout}")
7173
message = self.handle_alert(action, timeout)
7274
if text and text != message:
7375
raise AssertionError(
@@ -76,7 +78,7 @@ def alert_should_be_present(
7678

7779
@keyword
7880
def alert_should_not_be_present(
79-
self, action: str = ACCEPT, timeout: Union[float, str, None] = 0
81+
self, action: str = ACCEPT, timeout: Union[timedelta] = 0
8082
):
8183
"""Verifies that no alert is present.
8284
@@ -101,7 +103,7 @@ def alert_should_not_be_present(
101103

102104
@keyword
103105
def handle_alert(
104-
self, action: str = ACCEPT, timeout: Union[float, str, None] = None
106+
self, action: str = ACCEPT, timeout: Optional[timedelta] = None
105107
):
106108
"""Handles the current alert and returns its message.
107109
@@ -127,6 +129,7 @@ def handle_alert(
127129
128130
New in SeleniumLibrary 3.0.
129131
"""
132+
self.info(f"HANDLE::{type(timeout)}::{timeout}")
130133
alert = self._wait_alert(timeout)
131134
return self._handle_alert(alert, action)
132135

@@ -143,6 +146,7 @@ def _handle_alert(self, alert, action):
143146

144147
def _wait_alert(self, timeout=None):
145148
timeout = self.get_timeout(timeout)
149+
self.info(f"WAITING::{type(timeout)}::{timeout}")
146150
wait = WebDriverWait(self.driver, timeout)
147151
try:
148152
return wait.until(EC.alert_is_present())

src/SeleniumLibrary/keywords/waiting.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# limitations under the License.
1616

1717
import time
18+
from datetime import timedelta
1819
from typing import Optional, Union
1920

2021
from selenium.common.exceptions import StaleElementReferenceException
@@ -29,7 +30,7 @@ class WaitingKeywords(LibraryComponent):
2930
def wait_for_condition(
3031
self,
3132
condition: str,
32-
timeout: Union[str, float, None] = None,
33+
timeout: Optional[timedelta] = None,
3334
error: Optional[str] = None,
3435
):
3536
"""Waits until ``condition`` is true or ``timeout`` expires.
@@ -64,7 +65,7 @@ def wait_for_condition(
6465
def wait_until_location_is(
6566
self,
6667
expected: str,
67-
timeout: Union[str, float, None] = None,
68+
timeout: Optional[timedelta] = None,
6869
message: Optional[str] = None,
6970
):
7071
"""Waits until the current URL is ``expected``.
@@ -93,7 +94,7 @@ def wait_until_location_is(
9394
def wait_until_location_is_not(
9495
self,
9596
location: str,
96-
timeout: Union[str, float, None] = None,
97+
timeout: Optional[timedelta] = None,
9798
message: Optional[str] = None,
9899
):
99100
"""Waits until the current URL is not ``location``.
@@ -121,7 +122,7 @@ def wait_until_location_is_not(
121122
def wait_until_location_contains(
122123
self,
123124
expected: str,
124-
timeout: Union[str, float, None] = None,
125+
timeout: Optional[timedelta] = None,
125126
message: Optional[str] = None,
126127
):
127128
"""Waits until the current URL contains ``expected``.
@@ -149,7 +150,7 @@ def wait_until_location_contains(
149150
def wait_until_location_does_not_contain(
150151
self,
151152
location: str,
152-
timeout: Union[str, float, None] = None,
153+
timeout: Optional[timedelta] = None,
153154
message: Optional[str] = None,
154155
):
155156
"""Waits until the current URL does not contains ``location``.
@@ -177,7 +178,7 @@ def wait_until_location_does_not_contain(
177178
def wait_until_page_contains(
178179
self,
179180
text: str,
180-
timeout: Union[str, float, None] = None,
181+
timeout: Optional[timedelta] = None,
181182
error: Optional[str] = None,
182183
):
183184
"""Waits until ``text`` appears on the current page.
@@ -199,7 +200,7 @@ def wait_until_page_contains(
199200
def wait_until_page_does_not_contain(
200201
self,
201202
text: str,
202-
timeout: Union[str, float, None] = None,
203+
timeout: Optional[timedelta] = None,
203204
error: Optional[str] = None,
204205
):
205206
"""Waits until ``text`` disappears from the current page.
@@ -221,7 +222,7 @@ def wait_until_page_does_not_contain(
221222
def wait_until_page_contains_element(
222223
self,
223224
locator: str,
224-
timeout: Union[str, float, None] = None,
225+
timeout: Optional[timedelta] = None,
225226
error: Optional[str] = None,
226227
limit: Optional[int] = None,
227228
):
@@ -259,7 +260,7 @@ def wait_until_page_contains_element(
259260
def wait_until_page_does_not_contain_element(
260261
self,
261262
locator: str,
262-
timeout: Union[str, float, None] = None,
263+
timeout: Optional[timedelta] = None,
263264
error: Optional[str] = None,
264265
limit: Optional[int] = None,
265266
):
@@ -297,7 +298,7 @@ def wait_until_page_does_not_contain_element(
297298
def wait_until_element_is_visible(
298299
self,
299300
locator: str,
300-
timeout: Union[str, float, None] = None,
301+
timeout: Optional[timedelta] = None,
301302
error: Optional[str] = None,
302303
):
303304
"""Waits until the element ``locator`` is visible.
@@ -320,7 +321,7 @@ def wait_until_element_is_visible(
320321
def wait_until_element_is_not_visible(
321322
self,
322323
locator: str,
323-
timeout: Union[str, float, None] = None,
324+
timeout: Optional[timedelta] = None,
324325
error: Optional[str] = None,
325326
):
326327
"""Waits until the element ``locator`` is not visible.
@@ -343,7 +344,7 @@ def wait_until_element_is_not_visible(
343344
def wait_until_element_is_enabled(
344345
self,
345346
locator: str,
346-
timeout: Union[str, float, None] = None,
347+
timeout: Optional[timedelta] = None,
347348
error: Optional[str] = None,
348349
):
349350
"""Waits until the element ``locator`` is enabled.
@@ -372,7 +373,7 @@ def wait_until_element_contains(
372373
self,
373374
locator: str,
374375
text: str,
375-
timeout: Union[str, float, None] = None,
376+
timeout: Optional[timedelta] = None,
376377
error: Optional[str] = None,
377378
):
378379
"""Waits until the element ``locator`` contains ``text``.
@@ -396,7 +397,7 @@ def wait_until_element_does_not_contain(
396397
self,
397398
locator: str,
398399
text: str,
399-
timeout: Union[str, float, None] = None,
400+
timeout: Optional[timedelta] = None,
400401
error: Optional[str] = None,
401402
):
402403
"""Waits until the element ``locator`` does not contain ``text``.

utest/test/api/test_get_timeout.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from datetime import timedelta
2+
3+
import pytest
4+
from mockito import mock, unstub
5+
6+
from SeleniumLibrary.base import LibraryComponent
7+
8+
9+
@pytest.fixture
10+
def lib():
11+
ctx = mock()
12+
ctx.timeout = 5.0
13+
return LibraryComponent(ctx)
14+
15+
16+
def teardown_function():
17+
unstub()
18+
19+
20+
def test_timeout_as_none(lib: LibraryComponent):
21+
assert lib.get_timeout(None) == 5.0
22+
23+
24+
def test_timeout_as_float(lib: LibraryComponent):
25+
assert lib.get_timeout(1.0) == 1.0
26+
27+
28+
def test_timeout_as_timedelta(lib: LibraryComponent):
29+
assert lib.get_timeout(timedelta(seconds=0.1)) == 0.1

utest/test/keywords/test_keyword_arguments_waiting.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def test_wait_for_condition(waiting):
2626
assert "did not become true" in str(error.value)
2727

2828
with pytest.raises(AssertionError) as error:
29-
waiting.wait_for_condition(condition, "None", "foobar")
29+
waiting.wait_for_condition(condition, None, "foobar")
3030
assert "foobar" in str(error.value)
3131

3232

@@ -38,5 +38,5 @@ def test_wait_until_page_contains(waiting):
3838
assert "Text 'text' did not" in str(error.value)
3939

4040
with pytest.raises(AssertionError) as error:
41-
waiting.wait_until_page_contains(text, "None", "error")
41+
waiting.wait_until_page_contains(text, None, "error")
4242
assert "error" in str(error.value)

0 commit comments

Comments
 (0)