How to read toast message in android

@Chris_Trevarthen,

Still no luck,

I did what you mention, and ran object spy but still no luck. No clue whats going on.

@GeneralChingChicken, you cannot use latest Appium version, but to downgrade it.
Try this command for specific version:
npm install -g appium@1.8.1

Check Katalon supported versions here.

Also, read here further details in relating to above npm command.

After npm install appium, you can then installing appium-doctor as I find this useful to help you resolved any other dependent issues or installer.

Cheers.

Hi @GeneralChingChicken,

To help diagnose further, could you share the console log when you run your test? I’m especially interested in what Appium thinks is on the screen at the time the toast shows up (that’s the output from the driver.getPageSource() command I mentioned above. However, having the full console log would be helpful.

Also, I realized that I didn’t answer your question from above about what what version of Appium or what goes in the “?” for AppiumDriver<?>.

I’m using Appium Server 1.8.2-beta (although 1.8.1 should be fine for Android).

As for the “?”, you just leave it as-is - it’s sort of a wildcard when accessing some types of classes.

Thanks,

Chris

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