WebDriver element waiting mechanism

The ability to build robust and reliable tests is one of the key factors in the success of UI automation testing. But the reality is that when one test after another is executed, various situations are often encountered. For example, when a script locates an element or verifies the running state of a program, it sometimes finds that the element cannot be found. This may be caused by a sudden resource limitation or a network delay that causes the response speed to be too slow. At this time, a test failure will be returned. result. So we need to introduce a delay mechanism in the test script to match the running speed of the script with the response speed of the program. Even script and program responses can be synchronized. WebDriver provides us with two mechanisms , implicit wait and explicit wait . Let's explain them one by one:

implicit wait

  Implicit await provides a generic method for synchronizing a complete test case or a group of tests in WebDriver. It is very effective for solving program response time inconsistencies caused by network delay or dynamic loading using Ajax. 

  When the implicit wait time is set, WebDriver will continue to detect and search the DOM for a certain period of time in order to find one or more elements that are not immediately available to load successfully. In general, the default timeout for implicit waits is set to 0. But once the setting will be used for the entire life cycle of this WebDriver instance or during the execution of a complete test, and WebDriver will make it effective for element lookups of the entire page contained in all test steps.

  WebDriver provides the implicity_wait() method to configure the timeout period. Test scripts written based on unittest often add an implicit wait time to the setUp() method and set it to 30 seconds. When the test is executed, WebDriver will wait 30 seconds if it cannot find an element. When the timeout of 30 seconds is reached, a NoSuchElementException will be thrown. code show as below:

import unittest
from selenium import webdriver

class BaiduSearchTest(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.implicitly_wait(30) #set implicit wait time 30s  self.driver.maximize_window() #open the baidu page self.driver.get('https://www.baidu.com') def test_search_python(self): #get the search textbox search_textbox = self.driver.find_element_by_id('kw') search_textbox.clear() #enter search keyword search_textbox.send_keys("python") #get the and seacrh button and click search_button = self.driver.find_element_by_id('su') search_button.click() #add assert tag = self.driver.find_element_by_link_text("PyPI").text self.assertEqual('PyPI',tag) def tearDown(self): #close the browser window  self.driver.quit() if __name__ == '__main__': unittest.main(verbosity=2) 

explicit wait   

  Explicit waits are another waiting mechanism for synchronous testing in WebDriver. Explicit waits provide better control than implicit waits. Different from implicit waiting, explicit waiting needs to set some preset or customized conditions for the script, and wait for the conditions to be satisfied before proceeding to the next test.

  Explicit waits can only be used for test cases that only need synchronization. WebDriver provides the WebDriverWait class and the expected_conditions class to implement explicit waits. The expected_condition class provides some preset conditions as the judgment basis for the test script to perform the next test. Here is a simple test script with an explicit wait, the code is as follows:

import unittest
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions class BaitduExplicitWaitTest(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://www.baidu.com') def test_login_link(self): WebDriverWait(self.driver, 10).until(lambda s: s.find_element_by_name("tj_login").get_attribute("class") == "lb") login_link = WebDriverWait(self.driver, 10).until(expected_conditions.element_to_be_clickable((By.LINK_TEXT, "登录"))) login_link.click() def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main(verbosity=2)

  The above script first uses python's lambda expression and implements custom expected condition judgment based on WebDriverWait. Set the explicit wait timeout to 10s until the class attribute of the login option is 1b. At the same time, use the element_to_be_clickable method to determine whether the expected conditions are met. The condition is to wait for the element found through the locator to be visible and available. Here, the login option can be clicked until the maximum waiting time is 10s. Once the element is found according to the specified locator, the expected condition method returns the element to the test script for the next click. If no visible clickable element is found through the locator within the set timeout period, a TimeoutExpection exception will be thrown.

  The following table shows some common wait conditions commonly used in web browser automation operations supported by the expected_conditions class.

  The expected_conditions class has provided a variety of built-in expected waiting judgment conditions, which we can call directly in actual work. But if it exceeds the scope of expected_conditions, don't be afraid, the WebDriverWait class also provides a powerful custom expected wait judgment function.

  Note: Try to avoid mixing implicit waits with explicit waits in your tests to deal with synchronization issues.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325268330&siteId=291194637