Skip to content

Commit ef81299

Browse files
GQAssuranceAutomatedTester
authored andcommitted
Implement logical expected conditions in Python
Replicates the functionality of Java's AND / OR / NOT expected conditions. Because of reserved word constraints in Python, these are named: OR: `any_of(*expected_condition)` AND: `all_of(*expected_condition)` NOT: `none_of(*expected_condition)` Each function takes an unlimited number of expected_conditions as arguments. Implements #7121
1 parent 404c800 commit ef81299

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

py/selenium/webdriver/support/expected_conditions.py

+52
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,58 @@ def __call__(self, driver):
404404
return False
405405

406406

407+
def any_of(*expected_conditions):
408+
""" An expectation that any of multiple expected conditions is true.
409+
Equivalent to a logical 'OR'.
410+
Returns results of the first matching condition, or False if none do. """
411+
def any_of_condition(driver):
412+
for expected_condition in expected_conditions:
413+
try:
414+
result = expected_condition(driver)
415+
if result:
416+
return result
417+
except WebDriverException:
418+
pass
419+
return False
420+
return any_of_condition
421+
422+
423+
def all_of(*expected_conditions):
424+
""" An expectation that all of multiple expected conditions is true.
425+
Equivalent to a logical 'AND'.
426+
Returns: When any ExpectedCondition is not met: False
427+
When all ExpectedConditions are met: A List with each
428+
ExpectedCondition's return value """
429+
def all_of_condition(driver):
430+
results = []
431+
for expected_condition in expected_conditions:
432+
try:
433+
result = expected_condition(driver)
434+
if not result:
435+
return False
436+
results.append(result)
437+
except WebDriverException:
438+
return False
439+
return results
440+
return all_of_condition
441+
442+
443+
def none_of(*expected_conditions):
444+
""" An expectation that none of 1 or multiple expected conditions is true.
445+
Equivalent to a logical 'NOT-OR'.
446+
Returns a Boolean """
447+
def none_of_condition(driver):
448+
for expected_condition in expected_conditions:
449+
try:
450+
result = expected_condition(driver)
451+
if result:
452+
return False
453+
except WebDriverException:
454+
pass
455+
return True
456+
return none_of_condition
457+
458+
407459
def _find_element(driver, by):
408460
"""Looks up an element. Logs and re-raises ``WebDriverException``
409461
if thrown."""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Licensed to the Software Freedom Conservancy (SFC) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The SFC licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
import pytest
19+
20+
from selenium.common.exceptions import TimeoutException
21+
from selenium.webdriver.support.wait import WebDriverWait
22+
from selenium.webdriver.support import expected_conditions as EC
23+
from selenium.webdriver.common.by import By
24+
from selenium.webdriver.remote.webelement import WebElement
25+
26+
27+
def test_any_of_true(driver, pages):
28+
pages.load("simpleTest.html")
29+
WebDriverWait(driver, 0.1).until(EC.any_of(
30+
EC.title_is("Nope"), EC.title_is("Hello WebDriver")))
31+
32+
33+
def test_any_of_false(driver, pages):
34+
pages.load("simpleTest.html")
35+
with pytest.raises(TimeoutException):
36+
WebDriverWait(driver, 0.1).until(EC.any_of(
37+
EC.title_is("Nope"), EC.title_is("Still Nope")))
38+
39+
40+
def test_all_of_true(driver, pages):
41+
pages.load("simpleTest.html")
42+
results = WebDriverWait(driver, 0.1).until(EC.all_of(
43+
EC.title_is("Hello WebDriver"),
44+
EC.visibility_of_element_located((By.ID, "oneline"))))
45+
assert results[0] is True
46+
assert type(results[1]) is WebElement
47+
48+
49+
def test_all_of_false(driver, pages):
50+
pages.load("simpleTest.html")
51+
with pytest.raises(TimeoutException):
52+
WebDriverWait(driver, 0.1).until(EC.all_of(
53+
EC.title_is("Nope"), EC.title_is("Still Nope")))
54+
55+
56+
def test_none_of_true(driver, pages):
57+
pages.load("simpleTest.html")
58+
WebDriverWait(driver, 0.1).until(EC.none_of(
59+
EC.title_is("Nope"), EC.title_is("Still Nope")))
60+
61+
62+
def test_none_of_false(driver, pages):
63+
pages.load("simpleTest.html")
64+
with pytest.raises(TimeoutException):
65+
WebDriverWait(driver, 0.1).until(EC.none_of(
66+
EC.title_is("Nope"), EC.title_is("Hello WebDriver")))

0 commit comments

Comments
 (0)