UI自动化测试之WebDriverWait类

 在UI的自动化测试中,经常会由于网络加载慢的问题导致资源加载不出来,从而影响测试的效率,之前的处理方案是使用了time库里面的sleep()方法来休眠几秒钟,但是这样的方式毕竟不是很好的解决方案。在UI自动化测试中,关于等待的部分,主要汇总为如下三点,具体如下:

1、固定等待,也就是使用sleep()方法

2、隐式等待,使用到的方法是implicitly_wait的方法,可以把它理解为设置最长等待时间

3、显式等待,主要指的是程序每隔一段时间执行自定义的程序判断条件,如果判断成立,程序就会继续执行,那么如果判断失败,就会报TimeOutExpection的异常信息。

一、WebDriverWait类分析

         在UI的自动化测试中,显式等待主要使用的是类WebDriverWait,它里面提供了很多的解决方案,下面具体对它进行分析,我们要使用它,那么我们就首先需要导入它,导入的代码具体如下:

from selenium.webdriver.support.ui import WebDriverWait

下面具体显示该类的原代码,具体如下:

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

在上面源代码中,可以看到构造函数有两个形式,我们需要刻意的关注,一个是driver,还有另外一个是timeout,其实driver就是webdriver实例化后的对象信息,timeout指的是具体等待的时间,单位是秒。在WebDriverWait的类里面,调用的方法具体为:

 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
        """

在until的方法里面,该方法的形式阐述中,method它首先是一个方法,这个方法就是调用 expected_conditions

模块中的函数或者是方法,那么导入它的方式具体为:

from selenium.webdriver.support import  expected_conditions as es

调用这个模块里面的函数以及方法后,一般会返回两种结果信息,如果是True程序就会继续执行,如果是False那么就会报TimeOutExpxction的异常信息。

二、元素可见时操作(element_to_be_clickable

       显式等待:element_to_be_clickable是元素可见的时候进行操作,当然相反:元素不可见,那么就无法操作,这个主要指的是资源加载出来进行具体的操作,下面以百度搜索为例来演示这部分,源码为:

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('百度一下')
driver.quit()

下面演示一个错误的,比如把元素的属性故意修改错误,再执行就会报超时的错误信息,具体源码为:

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('百度一下')
driver.quit()


等待10秒后,由于元素属性错误查找不到,那么错误的信息就会显示超时,具体错误信息如下:

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: 

三、指定元素的文本位置(text_to_be_present_in_element)

          这个方法主要应用于错误文本信息的验证,首先需要错误文本信息显示出来才能够进行断言的验证,使用到的方法为:text_to_be_present_in_element,下面我们主要是以sina email为例来演示下这部分的具体应用,具体代码如下:

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/')
#点击登录按钮
driver.find_element_by_link_text('登录').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=='请输入邮箱名'
driver.quit()

四、判断元素是否可见(visibilty_of_element_located)

          以百度首页的’关于百度‘为例,使用到的方法为:visibilty_of_element_located,具体实现的源码为:

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,
        '关于百度')))
#判断存在后点击关于百度
aboutBaidu.click()
driver.quit()

 

posted @ 2022-07-01 19:56  LaraCroft  阅读(462)  评论(0)    收藏  举报