Java 如何使用uiautomator2通过appium验证toast消息?

Java 如何使用uiautomator2通过appium验证toast消息?,java,appium,Java,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

我写了一个应用程序,其中包括一个按钮,它将显示一个祝酒词的消息

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,'port')


您可以像这样捕捉烤面包片:

WebElement toastView=androidDriver.findElement(By.xpath(“//android.widget.Toast[1]”);
String text=toastView.getAttribute(“名称”)


这是我的解决方案。

诀窍在于找到正确的XPath。我使用以下方法之一进行管理:

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