1616# under the License.
1717
1818import re
19+ from typing import Callable
20+ from typing import List
21+ from typing import Tuple
22+ from typing import Union
1923
2024from selenium .common .exceptions import NoAlertPresentException
2125from selenium .common .exceptions import NoSuchElementException
2226from selenium .common .exceptions import NoSuchFrameException
2327from selenium .common .exceptions import StaleElementReferenceException
2428from selenium .common .exceptions import WebDriverException
29+ from selenium .webdriver import Chrome
30+ from selenium .webdriver import Edge
31+ from selenium .webdriver import Firefox
32+ from selenium .webdriver import Ie
33+ from selenium .webdriver import Safari
34+ from selenium .webdriver .common .alert import Alert
2535from selenium .webdriver .remote .webdriver import WebElement
2636
37+ # All driver types
38+ AnyDriver = Union [Chrome , Firefox , Safari , Ie , Edge ]
39+
2740"""
2841 * Canned "Expected Conditions" which are generally useful within webdriver
2942 * tests.
3043"""
3144
3245
33- def title_is (title : str ):
46+ def title_is (title : str ) -> Callable [[ AnyDriver ], bool ] :
3447 """An expectation for checking the title of a page.
3548
3649 title is the expected title, which must be an exact match returns
@@ -43,7 +56,7 @@ def _predicate(driver):
4356 return _predicate
4457
4558
46- def title_contains (title : str ):
59+ def title_contains (title : str ) -> Callable [[ AnyDriver ], bool ] :
4760 """An expectation for checking that the title contains a case-sensitive
4861 substring.
4962
@@ -57,7 +70,7 @@ def _predicate(driver):
5770 return _predicate
5871
5972
60- def presence_of_element_located (locator ) :
73+ def presence_of_element_located (locator : Tuple [ str , str ]) -> Callable [[ AnyDriver ], WebElement ] :
6174 """An expectation for checking that an element is present on the DOM of a
6275 page. This does not necessarily mean that the element is visible.
6376
@@ -71,7 +84,7 @@ def _predicate(driver):
7184 return _predicate
7285
7386
74- def url_contains (url : str ):
87+ def url_contains (url : str ) -> Callable [[ AnyDriver ], bool ] :
7588 """An expectation for checking that the current url contains a case-
7689 sensitive substring.
7790
@@ -85,7 +98,7 @@ def _predicate(driver):
8598 return _predicate
8699
87100
88- def url_matches (pattern : str ):
101+ def url_matches (pattern : str ) -> Callable [[ AnyDriver ], bool ] :
89102 """An expectation for checking the current url.
90103
91104 pattern is the expected pattern. This finds the first occurrence of
@@ -99,7 +112,7 @@ def _predicate(driver):
99112 return _predicate
100113
101114
102- def url_to_be (url : str ):
115+ def url_to_be (url : str ) -> Callable [[ AnyDriver ], bool ] :
103116 """An expectation for checking the current url.
104117
105118 url is the expected url, which must be an exact match returns True
@@ -112,7 +125,7 @@ def _predicate(driver):
112125 return _predicate
113126
114127
115- def url_changes (url : str ):
128+ def url_changes (url : str ) -> Callable [[ AnyDriver ], bool ] :
116129 """An expectation for checking the current url.
117130
118131 url is the expected url, which must not be an exact match returns
@@ -125,7 +138,7 @@ def _predicate(driver):
125138 return _predicate
126139
127140
128- def visibility_of_element_located (locator ) :
141+ def visibility_of_element_located (locator : Tuple [ str , str ]) -> Callable [[ AnyDriver ], Union [ WebElement , bool ]] :
129142 """An expectation for checking that an element is present on the DOM of a
130143 page and visible. Visibility means that the element is not only displayed
131144 but also has a height and width that is greater than 0.
@@ -143,7 +156,7 @@ def _predicate(driver):
143156 return _predicate
144157
145158
146- def visibility_of (element ) :
159+ def visibility_of (element : WebElement ) -> Callable [[ AnyDriver ], Union [ WebElement , bool ]] :
147160 """An expectation for checking that an element, known to be present on the
148161 DOM of a page, is visible.
149162
@@ -158,11 +171,11 @@ def _predicate(_):
158171 return _predicate
159172
160173
161- def _element_if_visible (element , visibility = True ):
174+ def _element_if_visible (element : WebElement , visibility : bool = True ) -> Union [ WebElement , bool ] :
162175 return element if element .is_displayed () == visibility else False
163176
164177
165- def presence_of_all_elements_located (locator ) :
178+ def presence_of_all_elements_located (locator : Tuple [ str , str ]) -> Callable [[ AnyDriver ], List [ WebElement ]] :
166179 """An expectation for checking that there is at least one element present
167180 on a web page.
168181
@@ -176,7 +189,7 @@ def _predicate(driver):
176189 return _predicate
177190
178191
179- def visibility_of_any_elements_located (locator ) :
192+ def visibility_of_any_elements_located (locator : Tuple [ str , str ]) -> Callable [[ AnyDriver ], List [ WebElement ]] :
180193 """An expectation for checking that there is at least one element visible
181194 on a web page.
182195
@@ -190,7 +203,9 @@ def _predicate(driver):
190203 return _predicate
191204
192205
193- def visibility_of_all_elements_located (locator ):
206+ def visibility_of_all_elements_located (
207+ locator : Tuple [str , str ]
208+ ) -> Callable [[AnyDriver ], Union [bool , List [WebElement ]]]:
194209 """An expectation for checking that all elements are present on the DOM of
195210 a page and visible. Visibility means that the elements are not only
196211 displayed but also has a height and width that is greater than 0.
@@ -212,7 +227,7 @@ def _predicate(driver):
212227 return _predicate
213228
214229
215- def text_to_be_present_in_element (locator , text_ ) :
230+ def text_to_be_present_in_element (locator : Tuple [ str , str ], text_ : str ) -> Callable [[ AnyDriver ], bool ] :
216231 """An expectation for checking if the given text is present in the
217232 specified element.
218233
@@ -229,7 +244,7 @@ def _predicate(driver):
229244 return _predicate
230245
231246
232- def text_to_be_present_in_element_value (locator , text_ ) :
247+ def text_to_be_present_in_element_value (locator : Tuple [ str , str ], text_ : str ) -> Callable [[ AnyDriver ], bool ] :
233248 """An expectation for checking if the given text is present in the
234249 element's value.
235250
@@ -246,7 +261,9 @@ def _predicate(driver):
246261 return _predicate
247262
248263
249- def text_to_be_present_in_element_attribute (locator , attribute_ , text_ ):
264+ def text_to_be_present_in_element_attribute (
265+ locator : Tuple [str , str ], attribute_ : str , text_ : str
266+ ) -> Callable [[AnyDriver ], bool ]:
250267 """An expectation for checking if the given text is present in the
251268 element's attribute.
252269
@@ -265,7 +282,7 @@ def _predicate(driver):
265282 return _predicate
266283
267284
268- def frame_to_be_available_and_switch_to_it (locator ) :
285+ def frame_to_be_available_and_switch_to_it (locator : Union [ Tuple [ str , str ], str ]) -> Callable [[ AnyDriver ], bool ] :
269286 """An expectation for checking whether the given frame is available to
270287 switch to.
271288
@@ -286,7 +303,9 @@ def _predicate(driver):
286303 return _predicate
287304
288305
289- def invisibility_of_element_located (locator ):
306+ def invisibility_of_element_located (
307+ locator : Union [WebElement , Tuple [str , str ]]
308+ ) -> Callable [[AnyDriver ], Union [WebElement , bool ]]:
290309 """An Expectation for checking that an element is either invisible or not
291310 present on the DOM.
292311
@@ -298,7 +317,7 @@ def _predicate(driver):
298317 target = locator
299318 if not isinstance (target , WebElement ):
300319 target = driver .find_element (* target )
301- return _element_if_visible (target , False )
320+ return _element_if_visible (target , visibility = False )
302321 except (NoSuchElementException , StaleElementReferenceException ):
303322 # In the case of NoSuchElement, returns true because the element is
304323 # not present in DOM. The try block checks if the element is present
@@ -310,7 +329,9 @@ def _predicate(driver):
310329 return _predicate
311330
312331
313- def invisibility_of_element (element ):
332+ def invisibility_of_element (
333+ element : Union [WebElement , Tuple [str , str ]]
334+ ) -> Callable [[AnyDriver ], Union [WebElement , bool ]]:
314335 """An Expectation for checking that an element is either invisible or not
315336 present on the DOM.
316337
@@ -319,7 +340,7 @@ def invisibility_of_element(element):
319340 return invisibility_of_element_located (element )
320341
321342
322- def element_to_be_clickable (mark ) :
343+ def element_to_be_clickable (mark : Union [ WebElement , Tuple [ str , str ]]) -> Callable [[ AnyDriver ], Union [ WebElement , bool ]] :
323344 """An Expectation for checking an element is visible and enabled such that
324345 you can click it.
325346
@@ -340,7 +361,7 @@ def _predicate(driver):
340361 return _predicate
341362
342363
343- def staleness_of (element ) :
364+ def staleness_of (element : WebElement ) -> Callable [[ AnyDriver ], bool ] :
344365 """Wait until an element is no longer attached to the DOM.
345366
346367 element is the element to wait for. returns False if the element is
@@ -358,7 +379,7 @@ def _predicate(_):
358379 return _predicate
359380
360381
361- def element_to_be_selected (element ) :
382+ def element_to_be_selected (element : WebElement ) -> Callable [[ AnyDriver ], bool ] :
362383 """An expectation for checking the selection is selected.
363384
364385 element is WebElement object
@@ -370,7 +391,7 @@ def _predicate(_):
370391 return _predicate
371392
372393
373- def element_located_to_be_selected (locator ) :
394+ def element_located_to_be_selected (locator : Tuple [ str , str ]) -> Callable [[ AnyDriver ], bool ] :
374395 """An expectation for the element to be located is selected.
375396
376397 locator is a tuple of (by, path)
@@ -382,7 +403,7 @@ def _predicate(driver):
382403 return _predicate
383404
384405
385- def element_selection_state_to_be (element , is_selected ) :
406+ def element_selection_state_to_be (element : WebElement , is_selected : bool ) -> Callable [[ AnyDriver ], bool ] :
386407 """An expectation for checking if the given element is selected.
387408
388409 element is WebElement object is_selected is a Boolean.
@@ -394,7 +415,7 @@ def _predicate(_):
394415 return _predicate
395416
396417
397- def element_located_selection_state_to_be (locator , is_selected ) :
418+ def element_located_selection_state_to_be (locator : Tuple [ str , str ], is_selected : bool ) -> Callable [[ AnyDriver ], bool ] :
398419 """An expectation to locate an element and check if the selection state
399420 specified is in that state.
400421
@@ -411,7 +432,7 @@ def _predicate(driver):
411432 return _predicate
412433
413434
414- def number_of_windows_to_be (num_windows ) :
435+ def number_of_windows_to_be (num_windows : int ) -> Callable [[ AnyDriver ], bool ] :
415436 """An expectation for the number of windows to be a certain value."""
416437
417438 def _predicate (driver ):
@@ -420,7 +441,7 @@ def _predicate(driver):
420441 return _predicate
421442
422443
423- def new_window_is_opened (current_handles ) :
444+ def new_window_is_opened (current_handles : List [ str ]) -> Callable [[ AnyDriver ], bool ] :
424445 """An expectation that a new window will be opened and have the number of
425446 windows handles increase."""
426447
@@ -430,7 +451,7 @@ def _predicate(driver):
430451 return _predicate
431452
432453
433- def alert_is_present ():
454+ def alert_is_present () -> Callable [[ AnyDriver ], Union [ Alert , bool ]] :
434455 """An expectation for checking if an alert is currently present and
435456 switching to it."""
436457
@@ -443,7 +464,7 @@ def _predicate(driver):
443464 return _predicate
444465
445466
446- def element_attribute_to_include (locator , attribute_ ) :
467+ def element_attribute_to_include (locator : Tuple [ str , str ], attribute_ : str ) -> Callable [[ AnyDriver ], bool ] :
447468 """An expectation for checking if the given attribute is included in the
448469 specified element.
449470
@@ -460,7 +481,7 @@ def _predicate(driver):
460481 return _predicate
461482
462483
463- def any_of (* expected_conditions ):
484+ def any_of (* expected_conditions ) -> Callable [[ AnyDriver ], Union [ WebElement , bool ]] :
464485 """An expectation that any of multiple expected conditions is true.
465486
466487 Equivalent to a logical 'OR'. Returns results of the first matching
@@ -480,7 +501,7 @@ def any_of_condition(driver):
480501 return any_of_condition
481502
482503
483- def all_of (* expected_conditions ):
504+ def all_of (* expected_conditions ) -> Callable [[ AnyDriver ], Union [ WebElement , bool ]] :
484505 """An expectation that all of multiple expected conditions is true.
485506
486507 Equivalent to a logical 'AND'.
@@ -503,7 +524,7 @@ def all_of_condition(driver):
503524 return all_of_condition
504525
505526
506- def none_of (* expected_conditions ):
527+ def none_of (* expected_conditions ) -> Callable [[ AnyDriver ], bool ] :
507528 """An expectation that none of 1 or multiple expected conditions is true.
508529
509530 Equivalent to a logical 'NOT-OR'. Returns a Boolean
0 commit comments