diff --git a/py/selenium/webdriver/support/expected_conditions.py b/py/selenium/webdriver/support/expected_conditions.py index 2a64920ddfa32..fe4aec15b8922 100644 --- a/py/selenium/webdriver/support/expected_conditions.py +++ b/py/selenium/webdriver/support/expected_conditions.py @@ -404,6 +404,58 @@ def __call__(self, driver): return False +def any_of(*expected_conditions): + """ An expectation that any of multiple expected conditions is true. + Equivalent to a logical 'OR'. + Returns results of the first matching condition, or False if none do. """ + def any_of_condition(driver): + 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): + """ An expectation that all of multiple expected conditions is true. + 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): + results = [] + 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): + """ An expectation that none of 1 or multiple expected conditions is true. + Equivalent to a logical 'NOT-OR'. + Returns a Boolean """ + def none_of_condition(driver): + for expected_condition in expected_conditions: + try: + result = expected_condition(driver) + if result: + return False + except WebDriverException: + pass + return True + return none_of_condition + + def _find_element(driver, by): """Looks up an element. Logs and re-raises ``WebDriverException`` if thrown.""" diff --git a/py/test/selenium/webdriver/support/expected_conditions_tests.py b/py/test/selenium/webdriver/support/expected_conditions_tests.py new file mode 100644 index 0000000000000..9257286da43eb --- /dev/null +++ b/py/test/selenium/webdriver/support/expected_conditions_tests.py @@ -0,0 +1,66 @@ +# 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 pytest + +from selenium.common.exceptions import TimeoutException +from selenium.webdriver.support.wait import WebDriverWait +from selenium.webdriver.support import expected_conditions as EC +from selenium.webdriver.common.by import By +from selenium.webdriver.remote.webelement import WebElement + + +def test_any_of_true(driver, pages): + pages.load("simpleTest.html") + WebDriverWait(driver, 0.1).until(EC.any_of( + EC.title_is("Nope"), EC.title_is("Hello WebDriver"))) + + +def test_any_of_false(driver, pages): + pages.load("simpleTest.html") + with pytest.raises(TimeoutException): + WebDriverWait(driver, 0.1).until(EC.any_of( + EC.title_is("Nope"), EC.title_is("Still Nope"))) + + +def test_all_of_true(driver, pages): + pages.load("simpleTest.html") + results = WebDriverWait(driver, 0.1).until(EC.all_of( + EC.title_is("Hello WebDriver"), + EC.visibility_of_element_located((By.ID, "oneline")))) + assert results[0] is True + assert type(results[1]) is WebElement + + +def test_all_of_false(driver, pages): + pages.load("simpleTest.html") + with pytest.raises(TimeoutException): + WebDriverWait(driver, 0.1).until(EC.all_of( + EC.title_is("Nope"), EC.title_is("Still Nope"))) + + +def test_none_of_true(driver, pages): + pages.load("simpleTest.html") + WebDriverWait(driver, 0.1).until(EC.none_of( + EC.title_is("Nope"), EC.title_is("Still Nope"))) + + +def test_none_of_false(driver, pages): + pages.load("simpleTest.html") + with pytest.raises(TimeoutException): + WebDriverWait(driver, 0.1).until(EC.none_of( + EC.title_is("Nope"), EC.title_is("Hello WebDriver")))