java实现获取toast弱提示 进行断言
// 获取到toast信息
// toast只能够根据它的文本值来定位到
WebElement toastElement = androidDriver.findElementByXPath("//*[contains(@text,'登录成功')]");
//toast元素是不能够点击,提示用户信息
System.out.println(toastElement.getText());
方法
/**
* 封装通用获取toast的方法
* @return
*/
public String getToast(String tips){
//显示等待
//1.初始化WebDriverWait对象
WebDriverWait webDriverWait = new WebDriverWait(androidDriver, 10);//10秒
//2.调用WebDriverWait的util
//第一种方法
WebElement webElement = webDriverWait.until(new ExpectedCondition<WebElement>() {
@Override
public WebElement apply(WebDriver arg0) {
// TODO Auto-generated method stub
try {
return androidDriver.findElementByXPath("//*[contains(@text,'"+tips+"')]");
} catch (NoSuchElementException e) {
// 再去做日志
}
return null;
}
});
return webElement.getText();
}
调用
//断言(根据toast)
String expectedValue=tips;//tips是我们的期望值
String actualValue = getToast(tips);
Assert.assertEquals(actualValue, expectedValue);//testng自带的断言,第一个是实际结果,第二个是期望结果
其他
没有验证是否可用
//断言(根据toast)
String expectaedValve=tips;//tips是我们的期望值
String actualValue = androidDriver.findElementByXPath("//*[contains(@test'"+tips+"')]").getText();
//隐式等待 30s 超过30s还没有继续往下操作则报错
androidDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);//秒为单位
//testng自带的断言,第一个是实际结果,第二个是期望结果
Assert.assertEquals(actualValue, expectaedValve);