How to read toast message in android

Hi @Chris_Trevarthen

I followed several of your interventions on this forum which are very interesting for me and that allowed me to use Katalon studio well I thank you very much

I am not a developer so I find it difficult to follow the code and script

For the moment and in the same context of the previous messages I am trying to know how to verify a message on a toast, but when I record a scenario, the toast is not detected by Katalon Studio therefore there is a solution to capture toast and do a test on the message that is on it.

I read your February 25 message on the same topic but I do not know where do I put the code you mentioned

Is it possible to make a demonstrative video in the subject “How to detect a toast and check the text on it”

I use Katalon Version: 5.10.1 Build: 1
Appium 1.8.1 (recomended by you)
I’m using a local Samsung S7 device that is linked by USB to a Sierra macOS version 10.12.6

1000 Thanks in advance

Refer to this guide: https://appiumpro.com/editions/64

1 Like

Hi @walpokbymon,

There’s not a whole lot to show in a video, but I can try to explain it step by step:

  1. Record a test using the Record Mobile feature of Katalon Studio (you have probably already done this). You will probably not be able to record the toast message - we’ll put that in later.

  2. Once you’ve finished recording your test, open your new test case file

  3. Click the tab at the bottom of the test case for “Script” - you’ll see the code for the test

  4. At the top of the test file, copy and paste the following lines:

    import com.kms.katalon.core.mobile.keyword.internal.MobileDriverFactory
    import io.appium.java_client.AppiumDriver
    import com.kms.katalon.core.util.KeywordUtil
    
  5. Find the spot in the test code right after you perform the action that triggers the toast.

  6. Copy and paste the following lines after the action that triggers the toast:

    AppiumDriver<?> driver = MobileDriverFactory.getDriver()
    def toast = driver.findElementByXPath("//android.widget.Toast[@text='Added to cart']")
    println("Toast element: " + toast)
    if (toast == null) {
        KeywordUtil.markFailed('ERROR: Toast object not found!')
    }
    
  7. Change the 'Added to cart' text to whatever the text of your toast should say.

  8. Save the test file

  9. Run your test

  10. Look at the “Console” tab of the test for the text “Toast element”

  11. If all goes well, your tests will pass. If for some reason the toast cannot be found, the test will fail when detecting it.

Hope this helps,

Chris

4 Likes

Hi @Chris_Trevarthen

It works :tada::tada::tada:
You’re just awesome :muscle:

Thank you so much Chris :clap:

1 Like

Hi Chris,

I captured the toast message using Mobile Spy.
My problem now is I can’t verify if element is visible after the trigger.

I did what you said above and it seems that I can’t seem to make it work still.

Please see attached images.

I tried to follow this step, but why there is some error ? could you help me
by the way I am using real device

Hi @teguh.hariyadi,

Could you please share your test script where you’re trying to read the toast? From the error message, it looks like several of the variables already exist, e.g. toast, driver and they are being redefined with the lines def toast = and def driver = within the same test script. Maybe you copy and pasted the lines a couple of times?

If you’re trying to use the variables again, you can remove the def from all but the first references to those variables in your test script.

Hope this helps,

Chris

Hi @francis.gamad,

When you run the test script from the 2nd image, do you see anything in the console for the line “Toast element:”? Could you please include the contents of the console and the log tabs so I can investigate further?

Thanks,

Chris

Hello Chris,

Here are the contents of the Console and Log Tabs.
I also didn’t see the “Toast element” text in the Console tab.

Is there something that I’ve missed?

https://drive.google.com/drive/folders/1n9wNEetC5pj55-6FXIK3dhBOX5aoSDGw?usp=sharing

Hoping for your quick response.

Francis G.

Hi @francis.gamad,

It looks from the logs that the toast is definitely not found. Can you try removing the text qualifier from the xpath to open it up and make it more flexible? For example:

def toast = driver.findElementByXPath("//android.widget.Toast")

Hope this helps,

Chris

Hey Chris,

Its still not working on my end. It say that the object does not exist in DOM anymore :frowning:

Maybe you have another workaround to check if the toast really shows?

Hi @francis.gamad,

One think you could do is to see what Katalon/Appium detect on the screen at the time you’re showing the toast.

I put some instructions on how to do this in another post:

If you can share the XML from the test right after you display the toast, we can troubleshoot further.

– Chris

Hello Chris

Sorry it took a long time to respond.
Here is the XML that came up

Hoping for a quick response.

Thank you.

Francis G.

Hi @francis.gamad,

Thanks for the XML, that’s very helpful. It looks like the toast message is part of the contents of the screen (if you look at the very end of the XML file, it’s there). So that’s very weird that your test isn’t seeing it.

I did notice that the bounds of the object are [0,0] to [0,0] which tells me that it’s probably not visible by the time that XML is logged.

A few things we can try:

  • Can you try putting that Mobile.delay statement back in to see if the check is happening too fast? I think you should be able to just use Mobile.delay(5) and not pass the FailureHandling parameter.
  • It looks like you’re using Appium 1.17.1. I’m not sure if that would be an issue, but I usually have luck with Appium 1.15.1.
  • You also might want to make sure your Android Studio and Android SDK are up-to-date.

If you’re still not seeing it work after all this, could you please send an updated console log?

– Chris

Hi Chris!

Going back to this solution. Do i still need to add another Verify command to verify the element was visible? Or is this our validation command already?

AppiumDriver<?> driver = MobileDriverFactory.getDriver()
def toast = driver.findElementByXPath("//android.widget.Toast[@text=‘Added to cart’]")
println("Toast element: " + toast)
if (toast == null) {
KeywordUtil.markFailed(‘ERROR: Toast object not found!’)
}

I did this one previously.

Hi Chris,

Sadly I tried all the things that you mentioned but still it shows me an error.

What I’m trying to do for this testcase is to verify if the toast was visible and verify the text inside it was correct. :frowning:

Hoping we could find a way to verify this

Francis

Hi @francis.gamad,

Typically, if an element isn’t visible, it won’t be found and returned by Katalon or Appium, so if your test doesn’t error out when getting an element, you can consider it visible without need to further verify it.

When you’re running the test, and observing the simulator or device, do you actually see the toast message appear?

As for the test you’re trying to run, can change any of the test code you were using for the toast to this simple check to just see if the toast is there:

Mobile.delay(5)
AppiumDriver<?> driver = MobileDriverFactory.getDriver()
def toast = driver.findElementByXPath("//android.widget.Toast")
println("Toast element: " + toast)
if (toast == null) {
    KeywordUtil.markFailed('ERROR: Toast object not found!')
}

Then can you check if there is a message in the log that the test fails or ‘ERROR: Toast object not found’

Could you also please share the script of the test you’re running (you can view the script display by clicking the “Script” tab that’s located in the middle of the last screenshot you shared. That way I can get a better idea of the other test code you’re running.

Thanks, Chris

Hello Chris,

There is no error in the Log viewer.
Here is my script as requested:

Hoping to get this fixed soon.

Thank you for your help!

Francis

Hi @francis.gamad,

Thanks for sharing the script. The key lines that would fail the test if the toast is not found are:

if (toast == null) {
    KeywordUtil.markFailed('ERROR: Toast object not found!')
}

So if the toast is not found, the test will be marked as failed.

If you want to be specific about the text on the toast, then you can go back to the toast xpath definition of:

def toast = driver.findElementByXPath('//android.widget.Toast[@text="The email must be a valid email address."]')

In your script, you should then be able to get rid of these lines, which are redundant:

Mobile.verifyElementVisible(findTestObject('Login/InvalidEmail-toast'), 0)

Mobile.verifyElementText(findTestObject('Login/InvalidEmail-toast'), 'The email must be a valid email address.')

After those changes, if the test runs and doesn’t fail, you can know that the toast was displayed and has the text you’re looking for.

Hope this helps,

Chris

1 Like