forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpected_conditions.py
1026 lines (801 loc) · 31 KB
/
expected_conditions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import re
from collections.abc import Iterable
from typing import Any
from typing import Callable
from typing import List
from typing import Literal
from typing import Tuple
from typing import TypeVar
from typing import Union
from selenium.common.exceptions import NoAlertPresentException
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoSuchFrameException
from selenium.common.exceptions import StaleElementReferenceException
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.remote.webdriver import WebElement
"""
* Canned "Expected Conditions" which are generally useful within webdriver
* tests.
"""
D = TypeVar("D")
T = TypeVar("T")
WebDriverOrWebElement = Union[WebDriver, WebElement]
def title_is(title: str) -> Callable[[WebDriver], bool]:
"""An expectation for checking the title of a page.
Parameters:
----------
title : str
The expected title, which must be an exact match.
Returns:
-------
boolean : True if the title matches, False otherwise.
"""
def _predicate(driver: WebDriver):
return driver.title == title
return _predicate
def title_contains(title: str) -> Callable[[WebDriver], bool]:
"""An expectation for checking that the title contains a case-sensitive
substring.
Parameters:
----------
title : str
The fragment of title expected.
Returns:
-------
boolean : True when the title matches, False otherwise.
"""
def _predicate(driver: WebDriver):
return title in driver.title
return _predicate
def presence_of_element_located(locator: Tuple[str, str]) -> Callable[[WebDriverOrWebElement], WebElement]:
"""An expectation for checking that an element is present on the DOM of a
page. This does not necessarily mean that the element is visible.
Parameters:
----------
locator : Tuple[str, str]
Used to find the element.
Returns:
-------
WebElement : The WebElement once it is located.
Example:
--------
>>> from selenium.webdriver.common.by import By
>>> from selenium.webdriver.support.ui import WebDriverWait
>>> from selenium.webdriver.support import expected_conditions as EC
>>> element = WebDriverWait(driver, 10).until(
... EC.presence_of_element_located((By.NAME, "q")))
"""
def _predicate(driver: WebDriverOrWebElement):
return driver.find_element(*locator)
return _predicate
def url_contains(url: str) -> Callable[[WebDriver], bool]:
"""An expectation for checking that the current url contains a case-
sensitive substring.
Parameters:
----------
url : str
The fragment of url expected.
Returns:
-------
boolean : True when the url matches, False otherwise.
"""
def _predicate(driver: WebDriver):
return url in driver.current_url
return _predicate
def url_matches(pattern: str) -> Callable[[WebDriver], bool]:
"""An expectation for checking the current url.
Parameters:
----------
pattern : str
The pattern to match with the current url.
Returns:
-------
boolean : True when the pattern matches, False otherwise.
Notes:
------
More powerful than url_contains, as it allows for regular expressions.
"""
def _predicate(driver: WebDriver):
return re.search(pattern, driver.current_url) is not None
return _predicate
def url_to_be(url: str) -> Callable[[WebDriver], bool]:
"""An expectation for checking the current url.
Parameters:
----------
url : str
The expected url, which must be an exact match.
Returns:
-------
boolean : True when the url matches, False otherwise.
"""
def _predicate(driver: WebDriver):
return url == driver.current_url
return _predicate
def url_changes(url: str) -> Callable[[WebDriver], bool]:
"""An expectation for checking the current url is different than a given
string.
Parameters:
----------
url : str
The expected url, which must not be an exact match.
Returns:
-------
boolean : True when the url does not match, False otherwise
"""
def _predicate(driver: WebDriver):
return url != driver.current_url
return _predicate
def visibility_of_element_located(
locator: Tuple[str, str]
) -> Callable[[WebDriverOrWebElement], Union[Literal[False], WebElement]]:
"""An expectation for checking that an element is present on the DOM of a
page and visible. Visibility means that the element is not only displayed
but also has a height and width that is greater than 0.
Parameters:
----------
locator : Tuple[str, str]
Used to find the element.
Returns:
-------
WebElement : The WebElement once it is located and visible.
Example:
--------
>>> from selenium.webdriver.common.by import By
>>> from selenium.webdriver.support.ui import WebDriverWait
>>> from selenium.webdriver.support import expected_conditions as EC
>>> element = WebDriverWait(driver, 10).until(
... EC.visibility_of_element_located((By.NAME, "q")))
"""
def _predicate(driver: WebDriverOrWebElement):
try:
return _element_if_visible(driver.find_element(*locator))
except StaleElementReferenceException:
return False
return _predicate
def visibility_of(element: WebElement) -> Callable[[Any], Union[Literal[False], WebElement]]:
"""An expectation for checking that an element, known to be present on the
DOM of a page, is visible.
Parameters:
----------
element : WebElement
The WebElement to check.
Returns:
-------
WebElement : The WebElement once it is visible.
Example:
--------
>>> from selenium.webdriver.common.by import By
>>> from selenium.webdriver.support.ui import WebDriverWait
>>> from selenium.webdriver.support import expected_conditions as EC
>>> element = WebDriverWait(driver, 10).until(
... EC.visibility_of(driver.find_element(By.NAME, "q")))
Notes:
------
Visibility means that the element is not only displayed but also has
a height and width that is greater than 0. element is the WebElement
returns the (same) WebElement once it is visible
"""
def _predicate(_):
return _element_if_visible(element)
return _predicate
def _element_if_visible(element: WebElement, visibility: bool = True) -> Union[Literal[False], WebElement]:
"""An expectation for checking that an element, known to be present on the
DOM of a page, is of the expected visibility.
Parameters:
----------
element : WebElement
The WebElement to check.
visibility : bool
The expected visibility of the element.
Returns:
-------
WebElement : The WebElement once it is visible or not visible.
"""
return element if element.is_displayed() == visibility else False
def presence_of_all_elements_located(locator: Tuple[str, str]) -> Callable[[WebDriverOrWebElement], List[WebElement]]:
"""An expectation for checking that there is at least one element present
on a web page.
Parameters:
----------
locator : Tuple[str, str]
Used to find the element.
Returns:
-------
List[WebElement] : The list of WebElements once they are located.
Example:
--------
>>> from selenium.webdriver.common.by import By
>>> from selenium.webdriver.support.ui import WebDriverWait
>>> from selenium.webdriver.support import expected_conditions as EC
>>> elements = WebDriverWait(driver, 10).until(
... EC.presence_of_all_elements_located((By.CLASS_NAME, "foo")))
"""
def _predicate(driver: WebDriverOrWebElement):
return driver.find_elements(*locator)
return _predicate
def visibility_of_any_elements_located(locator: Tuple[str, str]) -> Callable[[WebDriverOrWebElement], List[WebElement]]:
"""An expectation for checking that there is at least one element visible
on a web page.
Parameters:
----------
locator : Tuple[str, str]
Used to find the element.
Returns:
-------
List[WebElement] : The list of WebElements once they are located and visible.
Example:
--------
>>> from selenium.webdriver.common.by import By
>>> from selenium.webdriver.support.ui import WebDriverWait
>>> from selenium.webdriver.support import expected_conditions as EC
>>> elements = WebDriverWait(driver, 10).until(
... EC.visibility_of_any_elements_located((By.CLASS_NAME, "foo")))
"""
def _predicate(driver: WebDriverOrWebElement):
return [element for element in driver.find_elements(*locator) if _element_if_visible(element)]
return _predicate
def visibility_of_all_elements_located(
locator: Tuple[str, str]
) -> Callable[[WebDriverOrWebElement], Union[List[WebElement], Literal[False]]]:
"""An expectation for checking that all elements are present on the DOM of
a page and visible. Visibility means that the elements are not only
displayed but also has a height and width that is greater than 0.
Parameters:
----------
locator : Tuple[str, str]
Used to find the elements.
Returns:
-------
List[WebElement] : The list of WebElements once they are located and visible.
Example:
--------
>>> from selenium.webdriver.common.by import By
>>> from selenium.webdriver.support.ui import WebDriverWait
>>> from selenium.webdriver.support import expected_conditions as EC
>>> elements = WebDriverWait(driver, 10).until(
... EC.visibility_of_all_elements_located((By.CLASS_NAME, "foo")))
"""
def _predicate(driver: WebDriverOrWebElement):
try:
elements = driver.find_elements(*locator)
for element in elements:
if _element_if_visible(element, visibility=False):
return False
return elements
except StaleElementReferenceException:
return False
return _predicate
def text_to_be_present_in_element(locator: Tuple[str, str], text_: str) -> Callable[[WebDriverOrWebElement], bool]:
"""An expectation for checking if the given text is present in the
specified element.
Parameters:
----------
locator : Tuple[str, str]
Used to find the element.
text_ : str
The text to be present in the element.
Returns:
-------
boolean : True when the text is present, False otherwise.
Example:
--------
>>> from selenium.webdriver.common.by import By
>>> from selenium.webdriver.support.ui import WebDriverWait
>>> from selenium.webdriver.support import expected_conditions as EC
>>> is_text_in_element = WebDriverWait(driver, 10).until(
... EC.text_to_be_present_in_element((By.CLASS_NAME, "foo"), "bar"))
"""
def _predicate(driver: WebDriverOrWebElement):
try:
element_text = driver.find_element(*locator).text
return text_ in element_text
except StaleElementReferenceException:
return False
return _predicate
def text_to_be_present_in_element_value(
locator: Tuple[str, str], text_: str
) -> Callable[[WebDriverOrWebElement], bool]:
"""An expectation for checking if the given text is present in the
element's value.
Parameters:
----------
locator : Tuple[str, str]
Used to find the element.
text_ : str
The text to be present in the element's value.
Returns:
-------
boolean : True when the text is present, False otherwise.
Example:
--------
>>> from selenium.webdriver.common.by import By
>>> from selenium.webdriver.support.ui import WebDriverWait
>>> from selenium.webdriver.support import expected_conditions as EC
>>> is_text_in_element_value = WebDriverWait(driver, 10).until(
... EC.text_to_be_present_in_element_value((By.CLASS_NAME, "foo"), "bar"))
"""
def _predicate(driver: WebDriverOrWebElement):
try:
element_text = driver.find_element(*locator).get_attribute("value")
return text_ in element_text
except StaleElementReferenceException:
return False
return _predicate
def text_to_be_present_in_element_attribute(
locator: Tuple[str, str], attribute_: str, text_: str
) -> Callable[[WebDriverOrWebElement], bool]:
"""An expectation for checking if the given text is present in the
element's attribute.
Parameters:
----------
locator : Tuple[str, str]
Used to find the element.
attribute_ : str
The attribute to check the text in.
text_ : str
The text to be present in the element's attribute.
Returns:
-------
boolean : True when the text is present, False otherwise.
Example:
--------
>>> from selenium.webdriver.common.by import By
>>> from selenium.webdriver.support.ui import WebDriverWait
>>> from selenium.webdriver.support import expected_conditions as EC
>>> is_text_in_element_attribute = WebDriverWait(driver, 10).until(
... EC.text_to_be_present_in_element_attribute((By.CLASS_NAME, "foo"),
... "bar", "baz"))
"""
def _predicate(driver: WebDriverOrWebElement):
try:
element_text = driver.find_element(*locator).get_attribute(attribute_)
if element_text is None:
return False
return text_ in element_text
except StaleElementReferenceException:
return False
return _predicate
def frame_to_be_available_and_switch_to_it(locator: Union[Tuple[str, str], str, WebElement]) -> Callable[[WebDriver], bool]:
"""An expectation for checking whether the given frame is available to
switch to.
Parameters:
----------
locator: Union[Tuple[str, str], str, WebElement]
Used to find the frame.
Returns:
-------
boolean : True when the frame is available, False otherwise.
Example:
--------
>>> from selenium.webdriver.support.ui import WebDriverWait
>>> from selenium.webdriver.support import expected_conditions as EC
>>> WebDriverWait(driver, 10).until(
... EC.frame_to_be_available_and_switch_to_it("frame_name"))
Notes:
------
If the frame is available it switches the given driver to the
specified frame.
"""
def _predicate(driver: WebDriver):
try:
if isinstance(locator, Iterable) and not isinstance(locator, str):
driver.switch_to.frame(driver.find_element(*locator))
else:
driver.switch_to.frame(locator)
return True
except NoSuchFrameException:
return False
return _predicate
def invisibility_of_element_located(
locator: Union[WebElement, Tuple[str, str]]
) -> Callable[[WebDriverOrWebElement], Union[WebElement, bool]]:
"""An Expectation for checking that an element is either invisible or not
present on the DOM.
Parameters:
----------
locator : Union[WebElement, Tuple[str, str]]
Used to find the element.
Returns:
-------
boolean : True when the element is invisible or not present, False otherwise.
Example:
--------
>>> from selenium.webdriver.common.by import By
>>> from selenium.webdriver.support.ui import WebDriverWait
>>> from selenium.webdriver.support import expected_conditions as EC
>>> is_invisible = WebDriverWait(driver, 10).until(
... EC.invisibility_of_element_located((By.CLASS_NAME, "foo")))
Notes:
------
- In the case of NoSuchElement, returns true because the element is not
present in DOM. The try block checks if the element is present but is
invisible.
- In the case of StaleElementReference, returns true because stale element
reference implies that element is no longer visible.
"""
def _predicate(driver: WebDriverOrWebElement):
try:
target = locator
if not isinstance(target, WebElement):
target = driver.find_element(*target)
return _element_if_visible(target, visibility=False)
except (NoSuchElementException, StaleElementReferenceException):
# In the case of NoSuchElement, returns true because the element is
# not present in DOM. The try block checks if the element is present
# but is invisible.
# In the case of StaleElementReference, returns true because stale
# element reference implies that element is no longer visible.
return True
return _predicate
def invisibility_of_element(
element: Union[WebElement, Tuple[str, str]]
) -> Callable[[WebDriverOrWebElement], Union[WebElement, bool]]:
"""An Expectation for checking that an element is either invisible or not
present on the DOM.
Parameters:
----------
element : Union[WebElement, Tuple[str, str]]
Used to find the element.
Returns:
-------
boolean : True when the element is invisible or not present, False otherwise.
Example:
--------
>>> from selenium.webdriver.common.by import By
>>> from selenium.webdriver.support.ui import WebDriverWait
>>> from selenium.webdriver.support import expected_conditions as EC
>>> is_invisible_or_not_present = WebDriverWait(driver, 10).until(
... EC.invisibility_of_element(driver.find_element(By.CLASS_NAME, "foo")))
"""
return invisibility_of_element_located(element)
def element_to_be_clickable(
mark: Union[WebElement, Tuple[str, str]]
) -> Callable[[WebDriverOrWebElement], Union[Literal[False], WebElement]]:
"""An Expectation for checking an element is visible and enabled such that
you can click it.
Parameters:
----------
mark : Union[WebElement, Tuple[str, str]]
Used to find the element.
Returns:
-------
WebElement : The WebElement once it is located and clickable.
Example:
--------
>>> from selenium.webdriver.common.by import By
>>> from selenium.webdriver.support.ui import WebDriverWait
>>> from selenium.webdriver.support import expected_conditions as EC
>>> element = WebDriverWait(driver, 10).until(
... EC.element_to_be_clickable((By.CLASS_NAME, "foo")))
"""
# renamed argument to 'mark', to indicate that both locator
# and WebElement args are valid
def _predicate(driver: WebDriverOrWebElement):
target = mark
if not isinstance(target, WebElement): # if given locator instead of WebElement
target = driver.find_element(*target) # grab element at locator
element = visibility_of(target)(driver)
if element and element.is_enabled():
return element
return False
return _predicate
def staleness_of(element: WebElement) -> Callable[[Any], bool]:
"""Wait until an element is no longer attached to the DOM.
Parameters:
----------
element : WebElement
The element to wait for.
Returns:
-------
boolean : False if the element is still attached to the DOM, true otherwise.
Example:
--------
>>> from selenium.webdriver.common.by import By
>>> from selenium.webdriver.support.ui import WebDriverWait
>>> from selenium.webdriver.support import expected_conditions as EC
>>> is_element_stale = WebDriverWait(driver, 10).until(
... EC.staleness_of(driver.find_element(By.CLASS_NAME, "foo")))
"""
def _predicate(_):
try:
# Calling any method forces a staleness check
element.is_enabled()
return False
except StaleElementReferenceException:
return True
return _predicate
def element_to_be_selected(element: WebElement) -> Callable[[Any], bool]:
"""An expectation for checking the selection is selected.
Parameters:
----------
element : WebElement
The WebElement to check.
Returns:
-------
boolean : True if the element is selected, False otherwise.
Example:
--------
>>> from selenium.webdriver.common.by import By
>>> from selenium.webdriver.support.ui import WebDriverWait
>>> from selenium.webdriver.support import expected_conditions as EC
>>> is_selected = WebDriverWait(driver, 10).until(
... EC.element_to_be_selected(driver.find_element(By.CLASS_NAME, "foo")))
"""
def _predicate(_):
return element.is_selected()
return _predicate
def element_located_to_be_selected(locator: Tuple[str, str]) -> Callable[[WebDriverOrWebElement], bool]:
"""An expectation for the element to be located is selected.
Parameters:
----------
locator : Tuple[str, str]
Used to find the element.
Returns:
-------
boolean : True if the element is selected, False otherwise.
Example:
--------
>>> from selenium.webdriver.common.by import By
>>> from selenium.webdriver.support.ui import WebDriverWait
>>> from selenium.webdriver.support import expected_conditions as EC
>>> is_selected = WebDriverWait(driver, 10).until(
... EC.element_located_to_be_selected((By.CLASS_NAME, "foo")))
"""
def _predicate(driver: WebDriverOrWebElement):
return driver.find_element(*locator).is_selected()
return _predicate
def element_selection_state_to_be(element: WebElement, is_selected: bool) -> Callable[[Any], bool]:
"""An expectation for checking if the given element is selected.
Parameters:
----------
element : WebElement
The WebElement to check.
is_selected : bool
Returns:
-------
boolean : True if the element's selection state is the same as is_selected
Example:
--------
>>> from selenium.webdriver.common.by import By
>>> from selenium.webdriver.support.ui import WebDriverWait
>>> from selenium.webdriver.support import expected_conditions as EC
>>> is_selected = WebDriverWait(driver, 10).until(
... EC.element_selection_state_to_be(driver.find_element(By.CLASS_NAME, "foo"), True))
"""
def _predicate(_):
return element.is_selected() == is_selected
return _predicate
def element_located_selection_state_to_be(
locator: Tuple[str, str], is_selected: bool
) -> Callable[[WebDriverOrWebElement], bool]:
"""An expectation to locate an element and check if the selection state
specified is in that state.
Parameters:
----------
locator : Tuple[str, str]
Used to find the element.
is_selected : bool
Returns:
-------
boolean : True if the element's selection state is the same as is_selected
Example:
--------
>>> from selenium.webdriver.common.by import By
>>> from selenium.webdriver.support.ui import WebDriverWait
>>> from selenium.webdriver.support import expected_conditions as EC
>>> is_selected = WebDriverWait(driver, 10).until(
... EC.element_located_selection_state_to_be((By.CLASS_NAME, "foo"), True))
"""
def _predicate(driver: WebDriverOrWebElement):
try:
element = driver.find_element(*locator)
return element.is_selected() == is_selected
except StaleElementReferenceException:
return False
return _predicate
def number_of_windows_to_be(num_windows: int) -> Callable[[WebDriver], bool]:
"""An expectation for the number of windows to be a certain value.
Parameters:
----------
num_windows : int
The expected number of windows.
Returns:
-------
boolean : True when the number of windows matches, False otherwise.
Example:
--------
>>> from selenium.webdriver.support.ui import WebDriverWait
>>> from selenium.webdriver.support import expected_conditions as EC
>>> is_number_of_windows = WebDriverWait(driver, 10).until(
... EC.number_of_windows_to_be(2))
"""
def _predicate(driver: WebDriver):
return len(driver.window_handles) == num_windows
return _predicate
def new_window_is_opened(current_handles: List[str]) -> Callable[[WebDriver], bool]:
"""An expectation that a new window will be opened and have the number of
windows handles increase.
Parameters:
----------
current_handles : List[str]
The current window handles.
Returns:
-------
boolean : True when a new window is opened, False otherwise.
Example:
--------
>>> from selenium.webdriver.support.ui import By
>>> from selenium.webdriver.support.ui import WebDriverWait
>>> from selenium.webdriver.support import expected_conditions as EC
>>> is_new_window_opened = WebDriverWait(driver, 10).until(
... EC.new_window_is_opened(driver.window_handles))
"""
def _predicate(driver: WebDriver):
return len(driver.window_handles) > len(current_handles)
return _predicate
def alert_is_present() -> Callable[[WebDriver], Union[Alert, Literal[False]]]:
"""An expectation for checking if an alert is currently present and
switching to it.
Returns:
-------
Alert : The Alert once it is located.
Example:
--------
>>> from selenium.webdriver.support.ui import WebDriverWait
>>> from selenium.webdriver.support import expected_conditions as EC
>>> alert = WebDriverWait(driver, 10).until(EC.alert_is_present())
Notes:
------
If the alert is present it switches the given driver to it.
"""
def _predicate(driver: WebDriver):
try:
return driver.switch_to.alert
except NoAlertPresentException:
return False
return _predicate
def element_attribute_to_include(locator: Tuple[str, str], attribute_: str) -> Callable[[WebDriverOrWebElement], bool]:
"""An expectation for checking if the given attribute is included in the
specified element.
Parameters:
----------
locator : Tuple[str, str]
Used to find the element.
attribute_ : str
The attribute to check.
Returns:
-------
boolean : True when the attribute is included, False otherwise.
Example:
--------
>>> from selenium.webdriver.common.by import By
>>> from selenium.webdriver.support.ui import WebDriverWait
>>> from selenium.webdriver.support import expected_conditions as EC
>>> is_attribute_in_element = WebDriverWait(driver, 10).until(
... EC.element_attribute_to_include((By.CLASS_NAME, "foo"), "bar"))
"""
def _predicate(driver: WebDriverOrWebElement):
try:
element_attribute = driver.find_element(*locator).get_attribute(attribute_)
return element_attribute is not None
except StaleElementReferenceException:
return False
return _predicate
def any_of(*expected_conditions: Callable[[D], T]) -> Callable[[D], Union[Literal[False], T]]:
"""An expectation that any of multiple expected conditions is true.
Parameters:
----------
expected_conditions : Callable[[D], T]
The list of expected conditions to check.
Returns:
-------
T : The result of the first matching condition, or False if none do.
Example:
--------
>>> from selenium.webdriver.common.by import By
>>> from selenium.webdriver.support.ui import WebDriverWait
>>> from selenium.webdriver.support import expected_conditions as EC
>>> element = WebDriverWait(driver, 10).until(
... EC.any_of(EC.presence_of_element_located((By.NAME, "q"),
... EC.visibility_of_element_located((By.NAME, "q"))))
Notes:
------
Equivalent to a logical 'OR'. Returns results of the first matching
condition, or False if none do.
"""
def any_of_condition(driver: D):
for expected_condition in expected_conditions:
try:
result = expected_condition(driver)
if result:
return result
except WebDriverException:
pass
return False
return any_of_condition
def all_of(
*expected_conditions: Callable[[D], Union[T, Literal[False]]]
) -> Callable[[D], Union[List[T], Literal[False]]]:
"""An expectation that all of multiple expected conditions is true.
Parameters:
----------
expected_conditions : Callable[[D], Union[T, Literal[False]]]
The list of expected conditions to check.
Returns:
-------
List[T] : The results of all the matching conditions, or False if any do not.
Example:
--------
>>> from selenium.webdriver.common.by import By
>>> from selenium.webdriver.support.ui import WebDriverWait
>>> from selenium.webdriver.support import expected_conditions as EC
>>> elements = WebDriverWait(driver, 10).until(
... EC.all_of(EC.presence_of_element_located((By.NAME, "q"),
... EC.visibility_of_element_located((By.NAME, "q"))))
Notes:
------
Equivalent to a logical 'AND'.
Returns: When any ExpectedCondition is not met: False.
When all ExpectedConditions are met: A List with each ExpectedCondition's return value.
"""
def all_of_condition(driver: D):
results: List[T] = []
for expected_condition in expected_conditions:
try:
result = expected_condition(driver)
if not result:
return False
results.append(result)
except WebDriverException:
return False
return results
return all_of_condition
def none_of(*expected_conditions: Callable[[D], Any]) -> Callable[[D], bool]:
"""An expectation that none of 1 or multiple expected conditions is true.
Parameters:
----------
expected_conditions : Callable[[D], Any]
The list of expected conditions to check.
Returns:
-------
boolean : True if none of the conditions are true, False otherwise.