[笔记]Selenium Testing Tools Cookb
2019-12-15 本文已影响0人
elf_fff
Chapter 5 Synchronizing Tests
5.1 Synchronizing a test with an implicit wait
tc = unittest.TestCase()
driver.implicitly_wait(30)
button = driver.find_element_by_id("web-nav-app-download-btn")
button.click()
title = driver.find_element_by_class_name("slogan")
tc.assertEqual(title.text,"一个优质创作社区")
driver.find_element_by_class_name("logo").click()
5.2 Synchronizing a test with an explicit wait
The expected_conditions
class provides a set of predefined conditions to wait for before proceeding further in the code. The following table shows some common conditions that we frequently come across when automating web browsers supported by the expected_conditions
class
Predefined conditions | Selenium method |
---|---|
An element is visible and enabled | element_to_be_clickable(locator) |
An element is selected | element_to_be_selected(element) |
Presence of an element. This doesn't necessarily mean that the element is visible. | presence_of_element_located(locator) |
Specific text present in an element | text_to_be_present_in_element(locator,text_) |
An expectation for checking if the given text is present in the element's locator, text | text_to_be present_in_element_value |
Title | title_contains(title) |
button = driver.find_element_by_id("web-nav-app-download-btn")
button.click()
wait = WebDriverWait(driver,10)
wait.until(expected_conditions.title_contains("简书移动应用"))
5.3 Synchronizing a test with custom-expected conditions
button = driver.find_element_by_id("web-nav-app-download-btn")
button.click()
wait = WebDriverWait(driver,10)
wait.until(lambda driver: driver.find_element_by_class_name("logo"))
5.4 Synchronizing a test with FluentWait
There's no FluentWait
class for Python's Selenium, but the WebDriverWait
class has optional arguments for timeout, poll_frequency and ignored_exceptions.
from selenium.common.exceptions import *
button = driver.find_element_by_id("web-nav-app-download-btn")
button.click()
wait = WebDriverWait(driver,10,poll_frequency=1,ignored_exceptions=[ElementNotVisibleException])
wait.until(lambda driver: driver.find_element_by_class_name("logo"))