31
31
from selenium .common .exceptions import StaleElementReferenceException
32
32
from selenium .common .exceptions import WebDriverException
33
33
from selenium .webdriver .common .alert import Alert
34
+ from selenium .webdriver .common .by import ByType
34
35
from selenium .webdriver .remote .webdriver import WebDriver
35
36
from selenium .webdriver .remote .webdriver import WebElement
37
+ from selenium .webdriver .support .relative_locator import RelativeBy
36
38
37
39
"""
38
40
* Canned "Expected Conditions" which are generally useful within webdriver
43
45
T = TypeVar ("T" )
44
46
45
47
WebDriverOrWebElement = Union [WebDriver , WebElement ]
48
+ LocatorType = Union [Tuple [ByType , str ], Tuple [RelativeBy , None ]]
46
49
47
50
48
51
def title_is (title : str ) -> Callable [[WebDriver ], bool ]:
@@ -72,7 +75,7 @@ def _predicate(driver: WebDriver):
72
75
return _predicate
73
76
74
77
75
- def presence_of_element_located (locator : Tuple [ str , str ] ) -> Callable [[WebDriverOrWebElement ], WebElement ]:
78
+ def presence_of_element_located (locator : LocatorType ) -> Callable [[WebDriverOrWebElement ], WebElement ]:
76
79
"""An expectation for checking that an element is present on the DOM of a
77
80
page. This does not necessarily mean that the element is visible.
78
81
@@ -141,7 +144,7 @@ def _predicate(driver: WebDriver):
141
144
142
145
143
146
def visibility_of_element_located (
144
- locator : Tuple [ str , str ]
147
+ locator : LocatorType
145
148
) -> Callable [[WebDriverOrWebElement ], Union [Literal [False ], WebElement ]]:
146
149
"""An expectation for checking that an element is present on the DOM of a
147
150
page and visible. Visibility means that the element is not only displayed
@@ -179,7 +182,7 @@ def _element_if_visible(element: WebElement, visibility: bool = True) -> Union[L
179
182
return element if element .is_displayed () == visibility else False
180
183
181
184
182
- def presence_of_all_elements_located (locator : Tuple [ str , str ] ) -> Callable [[WebDriverOrWebElement ], List [WebElement ]]:
185
+ def presence_of_all_elements_located (locator : LocatorType ) -> Callable [[WebDriverOrWebElement ], List [WebElement ]]:
183
186
"""An expectation for checking that there is at least one element present
184
187
on a web page.
185
188
@@ -193,7 +196,7 @@ def _predicate(driver: WebDriverOrWebElement):
193
196
return _predicate
194
197
195
198
196
- def visibility_of_any_elements_located (locator : Tuple [ str , str ] ) -> Callable [[WebDriverOrWebElement ], List [WebElement ]]:
199
+ def visibility_of_any_elements_located (locator : LocatorType ) -> Callable [[WebDriverOrWebElement ], List [WebElement ]]:
197
200
"""An expectation for checking that there is at least one element visible
198
201
on a web page.
199
202
@@ -208,7 +211,7 @@ def _predicate(driver: WebDriverOrWebElement):
208
211
209
212
210
213
def visibility_of_all_elements_located (
211
- locator : Tuple [ str , str ]
214
+ locator : LocatorType
212
215
) -> Callable [[WebDriverOrWebElement ], Union [List [WebElement ], Literal [False ]]]:
213
216
"""An expectation for checking that all elements are present on the DOM of
214
217
a page and visible. Visibility means that the elements are not only
@@ -231,7 +234,7 @@ def _predicate(driver: WebDriverOrWebElement):
231
234
return _predicate
232
235
233
236
234
- def text_to_be_present_in_element (locator : Tuple [ str , str ] , text_ : str ) -> Callable [[WebDriverOrWebElement ], bool ]:
237
+ def text_to_be_present_in_element (locator : LocatorType , text_ : str ) -> Callable [[WebDriverOrWebElement ], bool ]:
235
238
"""An expectation for checking if the given text is present in the
236
239
specified element.
237
240
@@ -249,7 +252,7 @@ def _predicate(driver: WebDriverOrWebElement):
249
252
250
253
251
254
def text_to_be_present_in_element_value (
252
- locator : Tuple [ str , str ] , text_ : str
255
+ locator : LocatorType , text_ : str
253
256
) -> Callable [[WebDriverOrWebElement ], bool ]:
254
257
"""An expectation for checking if the given text is present in the
255
258
element's value.
@@ -268,7 +271,7 @@ def _predicate(driver: WebDriverOrWebElement):
268
271
269
272
270
273
def text_to_be_present_in_element_attribute (
271
- locator : Tuple [ str , str ] , attribute_ : str , text_ : str
274
+ locator : LocatorType , attribute_ : str , text_ : str
272
275
) -> Callable [[WebDriverOrWebElement ], bool ]:
273
276
"""An expectation for checking if the given text is present in the
274
277
element's attribute.
@@ -288,7 +291,7 @@ def _predicate(driver: WebDriverOrWebElement):
288
291
return _predicate
289
292
290
293
291
- def frame_to_be_available_and_switch_to_it (locator : Union [Tuple [ str , str ] , str ]) -> Callable [[WebDriver ], bool ]:
294
+ def frame_to_be_available_and_switch_to_it (locator : Union [LocatorType , str ]) -> Callable [[WebDriver ], bool ]:
292
295
"""An expectation for checking whether the given frame is available to
293
296
switch to.
294
297
@@ -310,7 +313,7 @@ def _predicate(driver: WebDriver):
310
313
311
314
312
315
def invisibility_of_element_located (
313
- locator : Union [WebElement , Tuple [ str , str ] ]
316
+ locator : Union [WebElement , LocatorType ]
314
317
) -> Callable [[WebDriverOrWebElement ], Union [WebElement , bool ]]:
315
318
"""An Expectation for checking that an element is either invisible or not
316
319
present on the DOM.
@@ -336,7 +339,7 @@ def _predicate(driver: WebDriverOrWebElement):
336
339
337
340
338
341
def invisibility_of_element (
339
- element : Union [WebElement , Tuple [ str , str ] ]
342
+ element : Union [WebElement , LocatorType ]
340
343
) -> Callable [[WebDriverOrWebElement ], Union [WebElement , bool ]]:
341
344
"""An Expectation for checking that an element is either invisible or not
342
345
present on the DOM.
@@ -347,7 +350,7 @@ def invisibility_of_element(
347
350
348
351
349
352
def element_to_be_clickable (
350
- mark : Union [WebElement , Tuple [ str , str ] ]
353
+ mark : Union [WebElement , LocatorType ]
351
354
) -> Callable [[WebDriverOrWebElement ], Union [Literal [False ], WebElement ]]:
352
355
"""An Expectation for checking an element is visible and enabled such that
353
356
you can click it.
@@ -399,7 +402,7 @@ def _predicate(_):
399
402
return _predicate
400
403
401
404
402
- def element_located_to_be_selected (locator : Tuple [ str , str ] ) -> Callable [[WebDriverOrWebElement ], bool ]:
405
+ def element_located_to_be_selected (locator : LocatorType ) -> Callable [[WebDriverOrWebElement ], bool ]:
403
406
"""An expectation for the element to be located is selected.
404
407
405
408
locator is a tuple of (by, path)
@@ -424,7 +427,7 @@ def _predicate(_):
424
427
425
428
426
429
def element_located_selection_state_to_be (
427
- locator : Tuple [ str , str ] , is_selected : bool
430
+ locator : LocatorType , is_selected : bool
428
431
) -> Callable [[WebDriverOrWebElement ], bool ]:
429
432
"""An expectation to locate an element and check if the selection state
430
433
specified is in that state.
@@ -474,7 +477,7 @@ def _predicate(driver: WebDriver):
474
477
return _predicate
475
478
476
479
477
- def element_attribute_to_include (locator : Tuple [ str , str ] , attribute_ : str ) -> Callable [[WebDriverOrWebElement ], bool ]:
480
+ def element_attribute_to_include (locator : LocatorType , attribute_ : str ) -> Callable [[WebDriverOrWebElement ], bool ]:
478
481
"""An expectation for checking if the given attribute is included in the
479
482
specified element.
480
483
0 commit comments