简体   繁体   English

如何使用uiautomator2通过appium验证烤面包消息?

[英]How to verify the toast message via appium with uiautomator2?

I have write one app which include one button and it will show one toast message. 我写了一个包含一个按钮的应用程序,它将显示一个吐司消息。

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

And then, i write one test case with appium try to verify it. 然后,我用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')]")))

But, the result is fail: 但是,结果是失败:

org.openqa.selenium.TimeoutException: Expected condition failed: waiting for presence of element located by: By.xpath: //*contains(@text, 'port') org.openqa.selenium.TimeoutException:预期条件失败:等待元素的存在:By.xpath:// *包含(@ text,'port')

The trick is finding the right XPath . 诀窍是找到正确的XPath I managed this using one of the following: 我使用以下方法之一对此进行了管理:

//android.widget.Toast
//android.widget.Toast[@text='toast text']

You can capture the toast like this: 您可以这样捕获烤面包:

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

Here is my solution. 是我的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM