Skip to content

Commit

Permalink
Implement logical expected conditions in Python
Browse files Browse the repository at this point in the history
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
  • Loading branch information
GQAssurance committed Jul 29, 2019
1 parent b31f2e0 commit ead2a2c
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
52 changes: 52 additions & 0 deletions py/selenium/webdriver/support/expected_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
66 changes: 66 additions & 0 deletions py/test/selenium/webdriver/support/expected_conditions_tests.py
Original file line number Diff line number Diff line change
@@ -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")))

0 comments on commit ead2a2c

Please sign in to comment.