• 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是元素可见的时候进行操

    作,当然相反的是元素不可见那么久无法操作,这个主要指的是资源加载出来进行具体的操作,

    下面以百度搜索为案例来演示这部分,涉及到的源码为:

    #! /usr/bin/env python
    # -*- coding:utf-8 -*-
    #author:无涯
    
    
    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()

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

    #! /usr/bin/env python
    # -*- coding:utf-8 -*-
    #author:无涯
    
    
    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,下面我们主要是以sina email为案例来演示

    下这部分的具体应用,具体代码如下:

    #! /usr/bin/env python
    # -*- coding:utf-8 -*-
    #author:无涯
    
    
    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,具体实现的

    源码为:

    #! /usr/bin/env python
    # -*- coding:utf-8 -*-
    #author:无涯
    
    
    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()

          感谢您的阅读,后续持续更新!

    欢迎关注微信公众号“Python自动化测试”
  • 相关阅读:
    scrapy-redis 分布式爬虫
    爬虫-框架-Scrapy
    MongoDB
    爬虫-请求库之-selenium
    通过位异或来交换a,b的值和通过中间变量交换a,b的值
    位运算
    sizeof运算符
    运算符和表达式(类型转换)
    为什么计算机存储的是二进制补码?
    各种进制的学习与转换
  • 原文地址:https://www.cnblogs.com/weke/p/15486916.html
Copyright © 2020-2023  润新知