app自动化测试(Android)

编程入门 行业动态 更新时间:2024-10-26 12:28:16

app自动化<a href=https://www.elefans.com/category/jswz/34/1771117.html style=测试(Android)"/>

app自动化测试(Android)

WebDriverWait类解析

WebDriverWait 用法代码

Python 版本
WebDriverWait(driver,timeout,poll_frequency=0.5,ignored_exceptions=None)

参数解析:

  • driver:WebDriver 实例对象

  • timeout: 最长等待时间,单位秒

  • poll_frequency: 检测的间隔步长,默认为 0.5s

  • ignored_exceptions: 执行过程中忽略的异常对象,默认只忽略 TimeoutException 异常类

Java 版本
WebDriverWait(WebDriver driver, long timeOutInSeconds)

Java 版本常用的有两个参数,参数解析:

  • driver:WebDriver 实例对象

  • timeOutInSeconds: 最长等待时间,单位秒

until、util_not用法

WebDriverWait 通常与 until 和 util_not 结合使用,Java 与 Python 用法相同。

  • until(method, message=’’) 在规定时间内,每隔一段时间调用一下 method 方法,直到返回值为 True,如果超时抛出带有 message 的 TimeoutException 异常信息

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

expected_conditions介绍

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

导入

需要先导入这个模块,导入代码如下:

  • Python 版本:
from selenium.webdriver.support import expected_conditions
  • Java 版本:
import org.openqa.selenium.support.ui.ExpectedConditions;
方法介绍

1.判断元素是否被加到了 DOM 树里,并不代表该元素一定可见,用法如下:

  • 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 版本
WebDriverWait().until(expected_conditions.visibility_of_element_located(locator))
  • Java 版本
new WebDriverWait( ).until(ExpectedConditions.visibilityOfElementLocated(locator));

3.element_to_be_clickable(locator) 方法,判断某元素是否可见并能点击,用法如下:

  • Python 版本
WebDriverWait().until(expected_conditions.element_to_be_clickable((By.ID, "kw")))
  • Java 版本
new WebDriverWait( ).until(ExpectedConditions.elementToBeClickable(locator));

案例

使用“雪球”应用,打开雪球 APP,点击页面上的搜索输入框输入“alibaba”,然后在搜索联想出来的列表里面点击“阿里巴巴”,选择股票分类,获取股票类型为“09988”的股票价格,最后验证价格大于 170,核心代码如下:

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# 判断价格大于 expect_priceassert current_price > expect_price
...
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.findElementByXPath("//*[@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.findElement(locator).getText();System.out.println(locatorText);float currentPrice = Float.parseFloat(locatorText);float expectPrice = 170;//判断价格大于 expect_priceassertThat(currentPrice, greaterThan(expectPrice));
}
...

这条测试用例仅仅使用隐式等待是解决不了问题的,因为【当前价格】这个元素一直在,而实际需要等待的是这个元素是否处于可点击的状态。

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

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

最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你! 

更多推荐

app自动化测试(Android)

本文发布于:2023-11-15 17:01:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1603142.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:测试   app   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!