How to test Android toast messages in Appium ( selenium Java)

RJX picture RJX · May 26, 2015 · Viewed 12.9k times · Source

I am using Selenium with Java to run scripts on android (thru Appium server). I see that it is not possible to locate a toast by using selenium's

driver.findElement(By.LinkText("User not logged in")

in Appium

But can be used in Selendroid to capture toast messages.

I there a way I can use both Selendroid and Appium in the same script?

Answer

Prajeeth Anand picture Prajeeth Anand · May 2, 2020

Finally, we are able to read the toast message without the need of taking screenshots and performing OCR. I have tested this on Appium 1.15.1.

Toast messages comes under com.package.system.

Normally, Xpath for this will be "/hierarchy/android.widget.Toast". And, Class Name will be "android.widget.settings"

You can confirm this by refreshing element inspector screen when toast message is displayed.

WebDriverWait waitForToast = new WebDriverWait(driver.25);

waitForToast.until(ExpectedConditions.presenceOfElementLoacted(By.xpath("/hierarchy/android.widget.Toast")));

String toastMessage = driver.findElement((By.xpath("/hierarchy/android.widget.Toast")).getText();

System.out.println(toastMessage);