简体   繁体   中英

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

The trick is finding the right 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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