I’m participating inDenver Creators Training Camp # 4, click here to learn more!
1. Encountering problems
During Web automation testing, it is common to encounter pages that load very slowly. These pages load slowly for a number of reasons:
-
Pages already have a lot of resources to load, usually the home page;
-
The page uses some unstable external dependencies, such as calling immature CSS style libraries and JS libraries;
-
Company servers have limited processing capacity;
-
Developers don’t write good code, etc.
2. Analyze the problem
If we want to carry out an automated test, we first need to open the page through the WebDriver get method, but because the page load time is too long, the following operations cannot be carried out, or even directly error, which has a great impact on the operation efficiency of the automated test.
class TestLogin(unittest.TestCase): def test_login_success(self): Driver = webdriver.chrome () driver.implicitly_wait(20) url = 'http://_url' driver.get(url driver.find_element_by_name('phone').send_keys(current_phone) pwd_elem = driver.find_element_by_name('password') Pwd_elem. Send_keys (current_pwd) pwd_elem. Send_keys (keys.enter) # actual = driver.find_element_by_xpath('//img[@class="mr-5"]/.. ') # assert self.assertin (' little bird ', actual. Text)Copy the code
When it comes to getting the actual results, a lot of time is spent waiting for the front page to load correctly, even though the later test cases pass. But there was a lot of wasted time, with one test case taking 26.5 seconds to run.
3. Solve problems
For some resources that load slowly, there is no need to wait until all elements are loaded before performing element positioning during automated testing.
Set a timeout period. If the page loads longer than the specified time, manually terminate the page, which is equivalent to clicking the X button of the browser to stop the page loading.
Driver.set_page_load_timeout (5) try: return driver.get(url) except TimeoutException: Execute_script ("window.stop()")Copy the code
The code has three points:
-
1. Set the timeout period to 5 seconds
-
2. When driver.get(URL) is executed, if the page load takes less than 5 seconds, no error message is displayed. If the page load takes more than 5 seconds, a timeout exception is thrown
-
3. Catch timeout exceptions with TimeoutException. When times out, execute js to terminate page loading.
Next, wrap the page GET method:
class IndexPage():
url = 'http://jiubing '
def __init__(self, driver, load_timeout=5):
self.driver = driver
self.driver.set_page_load_timeout(load_timeout)
def get(self):
try:
return self.driver.get(self.url)
except TimeoutException:
self.driver.execute_script("window.stop()")
...
Copy the code
Modify the original automation script:
class TestLogin(unittest.TestCase): def test_login_success(self): Driver = webdriver.chrome () driver.implicitly_wait(20) url = 'http://lemonban_url' driver.get(url driver.find_element_by_name('phone').send_keys(current_phone) pwd_elem = driver.find_element_by_name('password') Pwd_em.send_keys (current_pwd) pwd_em.send_keys (keys.enter) # actual result IndexPage(driver).get() actual = driver.find_element_by_xpath('//img[@class="mr-5"]/.. ') # assert self.assertin (' little bird ', actual. Text)Copy the code
4. Summarize the problem
The rest of the code doesn’t need to change, just add IndexPage(driver).get() where the load time is and let the page load at the specified timeout.
The new test time is 11.3 seconds, and the test efficiency is improved by nearly 60%.