软件测试 | 显示等待机制

93 阅读4分钟

1.显示等待简介

显示等待是针对待测App页面中某个特定的元素设置的等待时间。在设置的时间内,默认每隔一段时间测试代码执行检测当前页面的元素一次。

显示古代是一种只能的等待方式,它应用于等待某个指定的元素。显示等待比隐式等待更灵活,因为显示等待可以等待动态加载的AJAX “ 支持 ” 的元素(AJAX是一种无需重新加载整个网页的情况下,能够通过更新部分网页内容的技术),所以显示等待比隐式等待定位元素更加灵活。一般通过WebDriverWait类来声明显示等待。

2.WebDriverWait类解析

以下是WebDriverWait用法的代码(Python版和Java版)。

Python版本

driver.timeout,poll_frequency=0.5,ignored_exceptions=None

参数解析如下。

1)driver:WebDriver实例对象。

2)timeout:最长等待时间,单位为秒。

3)poll_frequency:检测的间隔步长,默认为0.5秒。

4)ignored_exceptions:执行过程中忽略的异常对象,默认值忽略TimeoutException异常。

Java版本

WebDriverWait(WebDriver driver,long timeOutInSeconds)

Java版本常用的有两个参数,参数解析如下。

1)driver:WebDriver实例对象。

2)timeOutInSeconds:最长等待时间,单位为秒。

3.until和util_not的用法

我们在使用WebDriverWait时,通常把它与until和 util_not结合使用。

  • until(method,message=“):在规定时间内,每隔一段时间调用一下method方法,,直到返回值为Ture,如果超时抛出带有message和TimeoutException异常。
  • until_not(method,message="):它与until()用法相反,表示在规定时间内,每隔一段时间调用一下method方法,直到返回值为False,如果超时抛出带有message的TimeoutException异常。

4.expected_condition介绍

expected_conditions是Selenium的一个模块,其中包含一系列可用判断的条件。这些条件可用用来判断页面的元素是否可见、是否可点击等。

(1)导入

需要先导入这个expected_conditions,导入代码如下(Python版和Java版)。

Python版

from selenium.webdriver.support import expected_conditions

Java版本

import org.openqa.selenium.support.ui.ExpectedConditions;

(2)expected_conditions方法介绍

1)判断元素是否别加到了DOM树里,但并不代表该元素一定可见,代码如下(Python版和Java版)。

Python版本

WebDriverWait().until(
    expected_conditions.presence_of_element_located(locator))

Java版本

new WebDriverWait()\
    .until(ExpectedConditions.presenceOfElementLocated(locator))

2)visibility_of_element_located(locator)方法,用来潘丹某个元素是否可见,可见代码表元素非隐藏,并且元素的宽和高都不等于0,代码如下(Python版和Java版)。

Python版本

WebDriverWait().until(
     expected_condition.visibility_of_element_located(locator))

Java版本

new WebDriverWait().until(
      expectedConditions.visibilityOfElementLocated(locator))

3)element_to_be_clickable(locator)方法,判断某元素是否可见并能点击,代码如下(Python版和Java版)。

Python版本

WebDriverWait().until(
     expected_conditions.element_to_be_clickable((By.ID,"kw")))

Java版本

new WebDriverWait().until(
   ExpectedConditions.elementToBeClickable(locator));

5.案例

案例使用的是 “雪球” App。打开雪球App,点击App页面上的搜索框输入框,先在输入框输入 “alibaba”,然后在搜索输入框“联想” 出来的列表里点击“阿里巴巴”项,选择股票分类,获取股票类型为“09988”的股票价格,最后验证价格大于170,核心代码如下(Python版和Java版)。

(1)Python演示代码

...
def test_wait(self):
    # 点击输入搜索框
    self.driver.find_element_by_id(
       "com.xueqiu.android:id/tv_search").click()
    #输入"alibaba"
    self.driver.find_element_by_id(
        "com.xueqiu.android:id/search_input_text"
        ).send_keys("alibaba")
    # 点击"阿里巴巴"项
    self.driver.find_element_by_xpath("//*[@text='阿里巴巴']").click()
    #点击"股票"项
    self.driver.find_element_by_xpath(
    "//*[contains(@resource-id,'title_container')]//*[@text='股票']"
    ).click()
    #获取股票价格
    locator = (MobileBy.XPATH,
    "//*[@text='09988']/../../..\
    //*[@resource-id='com.xueqiu.android:id/current_price']"
        
        ele = WebDriverWait(self.driver,10)\
        .until(expected_conditions.element_to_be_clickable(locator))
        print(ele.text)
        current_price = float(ele.text)
        expect_price = 170
        #判断价格大于 expec_price
        assert current_price > expect_price
 ...

(2)Java演示代码

...
private final(By locator = By.xpath("//*[@text='09988']/../../..\
   //*[@resource-id='com.xueqiu.android:id/current_price']"));
   
@Test
public void waitTest(){
//点击搜索输入框
driver.findElementById("com.xueqiu.android:id/tv_search").click();
//输入"alibaba"
driver.findElementById("com.xueqiu.android:id/\
    search_input_text").sendKeys("alibaba");
//点击"阿里巴巴"项
driver.findElemntByXPath("//*[@text='阿里巴巴']").click();
//点击"股票"项
driver.findElementByXPath("//*[contains(@resource-id,\
  'title_container')]//*[@text='股票']").click();
  //获取股票价格
  WebDriverWait wait=new WebDriverWait(driver,10);
  wait.until(ExpectedConditions.elementToBeClickable(locator));
  String locatorText = driver.fidnElement(locator).getText();
  System.out.println(locatorText);
  
  float currentPrice = Float.parseFloat(locatorText);
  float expectPrice = 170;
  //判断价格大于 expect_price
  assertThat(currentPrice,greaterThan(expectPrice));
}
...

这个测试用例作用是,对“当前价格”这个元素进行点击操作,我们需要等待这个元素处于“可点击”状态,才能对它进行操作,针对这种元素情况,使用隐式等待 是解决不了问题的。

上面的代码通过使用判断元素是否可点击的方法来判断元素是否处于可点击状态,在执行中间添加了10秒的等待时间,在10秒内每隔0.5秒查找一次元素,如果找到了这个元素,就继续向下执行;如果没找到就抛出TimeoutException异常。显式等待可以在某个元素灵活地添加等待时长,尤其是文件上传或者资源文件下载的测试场景中,可以添加显式等待,提高测试脚本的稳定性。

一般来说,测试脚本中会使用隐式等待与显式等待结合的方式,定义完driver之后立即设置一个隐式等待,在测试过程中需要判断某个元素属性的时候,再加上显式等待。

搜索微信公众号:TestingStuido霍格沃兹的干货都很硬核