Due to the abuse of waiting time, the automation test engineer was dismissed by the company that day

Posted by jug on Fri, 17 Dec 2021 13:23:37 +0100

preface

  • Automated testing mostly simulates the actual production scenario operation by locating page elements.
  • However, when writing automated test scripts, elements are often not located. There are two reasons:
  • 1. With frame
  • 2. Wait is not set

  this happens because the code running speed and browser loading and rendering speed are not of the same order of magnitude. webdriver provides three types of waiting: explicit waiting, implicit waiting and forced waiting.

Forced waiting

  • That is, the sleep() method, provided by the time module in python, forces the code to wait xxx time. No matter whether the execution of the previous code is completed or not, it must wait for the set time.

The example code is as follows:

# -*- coding: utf-8 -*-
# @Author: start on code

from selenium import webdriver
from time import sleep

driver = webdriver.Chrome()
driver.get('https://www.baidu.com')
sleep(5)
print(driver.current_url)
driver.quit()

Code parsing:

  • In this example, set the mandatory waiting time to 5 seconds. After 5 seconds, print the url of the current page, and then close the window.

  • This forced wait method is very useful in debug ging, but it is recommended to use this method with caution, because it is too rigid and seriously affects the execution speed of the program!

Implicit waiting

  • Definition: wait for the page elements to be loaded for the set time, and then execute the following code. If the loading is not completed after the set time, continue to execute the following code
  • (Note: if the loading is completed within the set time, execute the following code immediately);

The method of implicit waiting is: implicitly_wait

The example code is as follows:

# -*- coding: utf-8 -*-
# @Author: start on code

from selenium import webdriver

driver = webdriver.Chrome()
driver.implicitly_wait(8) # Wait recessively for up to 8 seconds
driver.get('https://www.baidu.com')

print(driver.current_url)
driver.quit()

Code parsing:

  • The set waiting time is 8 seconds, but these 8 seconds are not a fixed time and do not affect the script execution speed;
  • Important: implicit waiting works for the entire driver cycle, so you only need to set it once.

Display wait

  • Definition: continue to execute when a condition is established, otherwise an exception (TimeoutException) will be thrown when the maximum duration is reached;

  • WebDriverWait class is a waiting method provided by webdriver, which cooperates with until() and until provided by this class_ Using the not() method together, you can wait flexibly according to the judgment conditions. The format is as follows:

Grammar introduction

WebDriverWait(driver,timeout,poll_frequency=0.5,ignored_exceptions=None)

driver:Browser driven
timeout:Maximum timeout
poll_frequency:Detection interval, default 0.5s
ignored_exceptions:Exception information after timeout. Thrown by default NoSuchElementException abnormal
WebDriverWait()Generally by until()or until_not Methods are used together. The following are the descriptions of the two methods:
until(method,message=''):Call the driver provided by the method as a parameter until the return value is True;
until_not(method,message=''):Call the driver provided by the method as a parameter until the return value is Flase;

Sample code

# -*- coding: utf-8 -*-
# @Author: start on code

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

driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get('https://www.baidu.com')
locator = (By.LINK_TEXT, 'Journalism')

try:
    WebDriverWait(driver, 20, 0.5).until(EC.presence_of_element_located(locator))
    print(driver.find_element_by_link_text('Journalism').get_attribute('href'))
finally:
    driver.close()

Code parsing:

  • Use the as keyword to set expected_ Rename the conditions to EC and call presence_ of_ element_ The located () method determines whether the element exists;

  • In the above example, both implicit waiting and explicit waiting are used, but it should be noted that the longest waiting time is the maximum of the two;

expected_ The expected condition judgment method provided by the conditions class is as follows:

title_is:  Determine the of the current page title Exactly equal to(==)Expected string, return Boolean
title_contains : Determine the of the current page title Whether it contains the expected string and returns a Boolean value
presence_of_element_located : Determine whether an element is added to dom In the tree, it does not mean that the element must be visible
visibility_of_element_located : Determine whether an element is visible. Visible means that the element is not hidden, and the width and height of the element are not equal to 0
visibility_of : Do the same thing as the above method, but the above method needs to be passed in locator,This method is directly transmitted to the location element Just fine
presence_of_all_elements_located : Determine whether at least 1 element exists in dom In the tree. For example, if there is n Of elements class All'column-md-3',Then as long as one element exists, this method returns True
text_to_be_present_in_element : Determine the value in an element text Does it contain the expected string
text_to_be_present_in_element_value : Determine the value in an element value Property contains the expected string
frame_to_be_available_and_switch_to_it : Judge the frame Whether it can be or not? switch Go in, if you can, go back True also switch Go in, or go back False
invisibility_of_element_located : Determine whether an element does not exist in dom Trees or invisible
element_to_be_clickable : Determine whether an element is visible and enable Yes, that's what it's called clickable
staleness_of : Wait for an element from dom Remove from the tree. Note that this method also returns True or False
element_to_be_selected : Determine whether an element is selected,Generally used in drop-down lists
element_selection_state_to_be : Judge whether the selected state of an element meets the expectation
element_located_selection_state_to_be : The function of the above method is the same as that of the above method, except that the above method is passed in and located element,And this method is passed in locator
alert_is_present : Determine whether there is on the page alert

summary

  • For the above three waiting methods, you need to select appropriate methods according to the situation in the specific scene and use them flexibly...

  • If you have any questions during the learning process, you can communicate in the group at 893694563. If you think it is helpful to you, please point a praise in the lower right corner. Thank you for your support!

Topics: Python crawler