Last active
August 29, 2015 14:14
-
-
Save adamduren/4b98132d81001d2a9b4f to your computer and use it in GitHub Desktop.
Thoughts on Selenium Explicit VS Implicit Waits
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from selenium.webdriver.common.by import By | |
from selenium.webdriver.support.ui import WebDriverWait | |
from selenium.webdriver.support import expected_conditions as EC | |
from selenium.common.exceptions import NoSuchElementException | |
class BaseTestClass(object): | |
def __init__(self, driver): | |
self.wait = WebDriverWait(driver, 5, .25, (NoSuchElementException,)) | |
def find_element_by_css_selector(self, selector, expected_condition=None): | |
expected_condition = expected_condition or EC.presence_of_element_located | |
return self.wait.until(expected_condition((By.CSS_SELECTOR, '.test'))) | |
class TestFoo(BaseTestClass): | |
def test_foo(self): | |
# Implicit waits and Explicit waits should no be mixed | |
# http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp | |
# Existing implicit wait | |
# Suffers from the flaw returning when an element is immediately available | |
# In the case of ajax you may want to wait for a condition | |
# such as waiting for a loading icon to go away | |
element = self.driver.find_element_by_css_selector('.loaded-ajax') | |
# So something like this is needed | |
# although I'm not sure this will always work in angular | |
# the text may not always be rendered instantaneously | |
self.wait.until_not(EC.visibility_of(element)) | |
# Something like this may work instead | |
self.wait.until_not(EC.text_to_be_present_in_element((By.CSS_SELECTOR, '.value-im-looking-for'), '0')) | |
# Since they should not be mixed we should find a strategy for using explicit waits | |
# to locate elements in our tests that are not dependant on ajax calls | |
# This is the default explicit wait invocation | |
element = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#mainContainer'))) | |
# This is if we created our own helper functions to wrap explicit waits | |
# It would reduce boilerplate most of the time (defaults to presence) | |
element = self.find_element_by_css_selector('#mainContainer') | |
element = self.find_element_by_css_selector('#mainContainer', EC.visibility_of) | |
self.assertEqual(element.text, '1') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment