Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PY] Implement logical expected conditions in Python #7406

Merged
merged 1 commit into from
Aug 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")))