WebDriverWait class actual combat of UI automation test

        In the automatic testing of UI, the resources can not be loaded due to the slow network loading, which affects the efficiency of testing,

Previously, we used the sleep() method in the time library to sleep for a few seconds, but this method is not feasible after all

Is a good solution. In UI automation test, the waiting part is mainly summarized as follows:

1. Fixed wait, that is, using the sleep() method

2. Wait implicitly. The method used is implicitly_ The method of wait can be understood as setting the maximum waiting time

3. Explicit wait mainly refers to that the program executes customized program judgment conditions every other period of time. If the judgment is true, the program will continue

If the judgment fails, an exception message of timeoutexpectation will be reported.

1, WebDriverWait class analysis

          In the automatic testing of UI, the class WebDriverWait is mainly used for explicit waiting, which provides many solutions,

The following is a detailed analysis of it. If we want to use it, we first need to find it. The imported code is as follows:

from selenium.webdriver.support.ui import WebDriverWait

The original code of this class is shown below:

class WebDriverWait(object):
    def __init__(self, driver, timeout, poll_frequency=POLL_FREQUENCY, ignored_exceptions=None):

In its source code, we can see that there are two forms of constructors, one is driver and the other is

timeout, in fact, driver is the object information after webdriver instantiation. timeout refers to the specific waiting time, and the unit is seconds

In the WebDriverWait class, the methods called are as follows:

   def until(self, method, message=''):
        """Calls the method provided with the driver as an argument until the \
        return value does not evaluate to ``False``.

        :param method: callable(WebDriver)
        :param message: optional message for :exc:`TimeoutException`
        :returns: the result of the last call to `method`
        :raises: :exc:`selenium.common.exceptions.TimeoutException` if timeout occurs
        """

In the until method, in the formal description of the method, method is first a method. In fact, this method is called

expected_conditions

If a function or method in a module is imported, the specific method is as follows:

from selenium.webdriver.support import  expected_conditions as es

After calling the functions and methods in this module, two kinds of result information will be returned. If it is True, the program will continue to execute,

If it is False, the exception information of TimeOutExpxction will be reported.

2, Action when element is visible

        Let's take a specific look at the case application of explicit waiting, element_to_be_clickable operates when the element is visible

Of course, on the contrary, the element is not visible for so long and cannot be operated. This mainly refers to loading resources for specific operations,

The following takes Baidu search as an example to demonstrate this part. The source code involved is:

#! /usr/bin/env python
# -*- coding:utf-8 -*-
#author: boundless


from selenium import  webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import  expected_conditions as es
from selenium.webdriver.common.by import By
import time as t

driver=webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get('http://www.baidu.com')
so=WebDriverWait(
	driver=driver,
	timeout=10).until(
	es.element_to_be_clickable((By.ID,'kw')))
so.send_keys('use Baidu Search')
driver.quit()

The following is a demonstration of an error. For example, if you deliberately modify the attributes of an element, you will report a timeout error message. The specific source code is:

#! /usr/bin/env python
# -*- coding:utf-8 -*-
#author: boundless


from selenium import  webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import  expected_conditions as es
from selenium.webdriver.common.by import By
import time as t

driver=webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get('http://www.baidu.com')
so=WebDriverWait(
	driver=driver,
	timeout=10).until(
	es.element_to_be_clickable((By.ID,'kwas')))
so.send_keys('use Baidu Search')
driver.quit()

After waiting for 10 seconds, if the element attribute cannot be found due to error, the error message will display timeout. The specific error message is as follows:

Traceback (most recent call last):
    es.element_to_be_clickable((By.ID,'kwas')))
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/support/wait.py", line 87, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 

3, Specifies the text location of the element

          This method is mainly applied to the verification of error text information. We need to display the error text information before we can assert

Verify that the method used is: text_to_be_present_in_element, let's take sina email as an example

The specific application code of this part is as follows:

#! /usr/bin/env python
# -*- coding:utf-8 -*-
#author: boundless


from selenium import  webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import  expected_conditions as es
from selenium.webdriver.common.by import By
import time as t


driver=webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get('https://mail.sina.com.cn/')
#Click the login button
driver.find_element_by_link_text('Sign in').click()
divText=WebDriverWait(
	driver=driver,
	timeout=10).until(
	es.element_to_be_clickable((
		By.XPATH,
		'/html/body/div[3]/div/div[2]/div/div/div[4]/div[1]/div[1]/div[1]/span[1]')))
assert divText.text=='Please enter a mailbox name'
driver.quit()

4, Determine whether the element is visible

          Here, take Baidu on Baidu's home page as a case, and the method used is: visibility_ of_ element_ Located, specifically implemented

The source code is:

#! /usr/bin/env python
# -*- coding:utf-8 -*-
#author: boundless


from selenium import  webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import  expected_conditions as es
from selenium.webdriver.common.by import By
import time as t


driver=webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(30)
driver.get('http://www.baidu.com')
aboutBaidu=WebDriverWait(
	driver=driver,
	timeout=10).until(
	es.visibility_of_element_located((
		By.LINK_TEXT,
		'About Baidu')))
#After judging the existence, click about Baidu
aboutBaidu.click()
driver.quit()

      Thank you for reading and updating!

Added by dad00 on Sat, 30 Oct 2021 17:27:40 +0300