/waiting-strategies

Waiting Strategies - Appium and Selenium Automation

Waiting Strategies - Appium and Selenium Automation

The repo contains code snippets used in this [post](..medium post..).

NoSuchElementException

image

from selenium.common.exceptions import NoSuchElementException
...

def test_demo():
    driver = webdriver.Remote(server_url, caps)
    
    try:
        element = driver.find_element(By.ID, "foo")
        element.click()
    except NoSuchElementException:
        print(driver.page_source)
        raise
    finally:
        driver.quit()

Static Wait

image

def test_login_static_wait():
    time.sleep(3)
    driver.find_element(login_screen).click()
    
    time.sleep(3)
    driver.find_element(username).send_keys(AUTH_USER)
    driver.find_element(password).send_keys(AUTH_PASS)
    driver.find_element(login_button).click()
    
    time.sleep(3)
    driver.find_element(get_logged_in_by(AUTH_USER))

Implicit Wait

image

def test_login_implicit_wait():
    driver.implicitly_wait(10)
    
    driver.find_element(login_screen).click()
    driver.find_element(username).send_keys(AUTH_USER)
    driver.find_element(password).send_keys(AUTH_PASS)
    driver.find_element(login_button).click()
    driver.find_element(get_logged_in_by(AUTH_USER))

Explicit Wait

image

def test_login_explicit_wait():
    wait = WebDriverWait(driver, 10)
    
    wait.until(expected_conditions.presence_of_element_located(login_screen)).click()
    wait.until(expected_conditions.presence_of_element_located(username)).send_keys(AUTH_USER)
    wait.until(expected_conditions.presence_of_element_located(password)).send_keys(AUTH_PASS)
    wait.until(expected_conditions.presence_of_element_located(login_button)).click()
    wait.until(expected_conditions.presence_of_element_located(get_logged_in_by(AUTH_USER)))

image

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

element = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "foo"))
    )

Custom Explicit Wait

image

class tap_is_successful(object):
    
    def __init__(self, locator):
        self.locator = locator
        
        def __call__(self, driver):
            try:
                element = driver.find_element(*self.locator)
                element.click()
                return True
            except:
                return False
                
wait = WebDriverWait(driver, 10)
wait.until(tap_is_successful(By.ID, "foo"))

image

class element_found_and_clicked(object):
    
    def __init__(self, locator):
        self.locator = locator
        
        def __call__(self, driver):
            try:
                element = driver.find_element(*self.locator)
                element.click()
                return True
            except:
                return False

image

def test_login_custom_wait():
    wait = WebDriverWait(driver, 10)
    
    wait.until(element_found_and_clicked(login_screen))
    wait.until(expected_conditions.presence_of_element_located(username)).send_keys(AUTH_USER)
    wait.until(expected_conditions.presence_of_element_located(password)).send_keys(AUTH_PASS)
    wait.until(element_found_and_clicked(login_button))
    wait.until(expected_conditions.presence_of_element_located(get_logged_in_by(AUTH_USER))) 

Fluent Wait

image

wait = WebDriverWait(
    driver, timeout=10, poll_frequency=2, ignored_exceptions=ignore_list
    )