Verifying Toast

I’m using:
io.appium 6.1.0
selenium-java 3.11.0
guava 24.0-jre.

Try this:
String xmlFormat = driver.getPageSource();
if(xmlFormat.contains(“Your Toast Message Here”)){
System.out.println("Toast message displayed: "+yourToastMessage);
}

  • The first line of instruction will give the xml format of the app page. In this, I got the toast message in one of the tags. It works for me.
    You can put the getPageSource instruction after the desired activity, and since toast messages generally have 2 seconds of timeOut, so the above method will work.
1 Like

I tried your method but for me is not working.
I don’t think you have a toast message there as far as the getPageSource will find the message you looking for.
Normally if getPageSource is finding the element/text we should be able to identify the element like the rest elements.

Hi @Zuzeac,

We need to check if the xmlFormat string contains the toast message. Whether getPageSource() method is able to capture the exact XML DOM at the time of the toast message getting displayed is dependent on how you tweak wait times to ensure the exact toast message is captured.
You need to give some TimeOut.MILLISECONDS.sleep(200) or 500 before the page source is captured.
Also, the capture has to be done before the toast message vanishes away.
I think the standard of any toast message time out is 2 seconds.

Please let me know after doing it if you are still facing any problems.
More than this, I need to see your code what you are exactly trying to achieve.

Thanks … Cheers :slight_smile:

Try this:

long startTime = System.currentTimeMillis(); //fetch starting time
boolean neededStatus;
do{
xmlFormat = driver.getPageSource();
neededStatus = xmlFormat.contains(“Needed”);
}while(!(neededStatus) && (((System.currentTimeMillis()-startTime) <= (5*1000))));

‘5’ is for 5 seconds to wait till the toast message is displayed. Generally standard timeout of a toast message is 2 seconds. You can keep this value around 2 to 5 seconds.

Let me know if this works !

@sam69
This looks like best approach for me. Can you please let me know how to achieve the same thing using ruby_lib

It works for me,
Thanks so much!

Here is my working code in C#. I have identified toast message as “List
RecordSavedToastMessage=driver.FindElementsByXPath(”/hierarchy/android.widget.Toast").ToList()" and used timer and while loop.

bool isSuccessMessageAppeared = false;

        var stopWatch = Stopwatch.StartNew();
        while (stopWatch.ElapsedMilliseconds<6000)
        {
            if(RecordSavedToastMessage.Count>0)
            {
                isSuccessMessageAppeared = true;
                 break;
            }
        }

        return isSuccessMessageAppeared;

No need of OCR (Tesseract) .
you can get it using UiAutomator2 .

Follow the code below:
js:
var toastMessage = await client.$$(’//android.widget.Toast’);//gets the toast element.
var value=await toastMessage[0].getAttribute(“name”);//use name attribute t get the value.

IMPORTANT NOTE: you have to capture it exactly when the toast message appears on the screen.