2017-04-19 96 views
0

我写了一个包含一个按钮的应用程序,它会显示一个Toast消息。如何使用uiautomator2通过appium验证吐司消息?

public void onClickShowToast(View view) { 
    Toast.makeText(getBaseContext(), "Change port successfully", Toast.LENGTH_LONG).show(); 
} 

然后,我用appium编写一个测试用例来验证它。

//APPIUM Version: 1.6.3; 
//JAVA: 1.8; 
//JAVA Client: 5.0.0; 
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.ANDROID_UIAUTOMATOR2); 

driver.findElementByXPath("//*[contains(@text,'Button')]").click(); 
WebDriverWait wait = new WebDriverWait(driver, 10); 
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[contains(@text, 'port')]"))) 

但是,结果是失败的:

org.openqa.selenium.TimeoutException:预期条件失败: 等待位于由元素的存在:By.xpath: // *包含(@Text, '口')

回答

0

您可以捕获这样的敬酒:

WebElement toastView = androidDriver.findElement(By.xpath("//android.widget.Toast[1]")); String text = toastView.getAttribute("name");

Here是我的解决方案。