How to read toast message in android

I updated/reinstalled Android SDK and all my VD.
I am running 5.10 katalon studio
I am running 1.10 Appium - uiautomator2 driver

Still not seeing - toast message

Hi @GeneralChingChicken,

That’s interesting - I would not have expected Appium 1.10 to work at all (it’s been my experience that Katalon didn’t work with anything higher than Appium 1.8.1). I’m using Appium 1.8.1 and Katalon Studio 5.10.1. Any chance you could try with Appium 1.8.1?

NOTE: Appium 1.8.1 isn’t the same as Appium Desktop 1.8.1 - it’s a bit confusing with the version numbers. If you’re using Appium Desktop, you’ll need Appium Desktop 1.6.3.

– Chris

1 Like

Sorry guys was not on this forum actively.

@Zarashima, @U.G.Chinthaka_Devind, here the sample code that works on basic OCR:

  1. First you make use Katalon API to capture some pictures/snapshots:
    SnapTotal = total snapshots
    ScrSnapDir = path to store snapshots as you would need to clear this prior snapshot, sorta like working directory for OCR.
    for (def index : (0…SnapTotal)) {
    String filename = Mobile.concatenate([
    ScrSnapDir,
    ‘\’,
    ‘file’,
    index,
    ‘.png’] as String[], FailureHandling.STOP_ON_FAILURE)
    Mobile.takeScreenshot(filename, FailureHandling.STOP_ON_FAILURE)
    }

  2. You need Tess4j (tess4j-4.0.1.jar, in my case) for the basic OCR as here I not involving yet OpenCV (see another topic created today). Here the basic codes:
    Tesseract instance = new Tesseract();
    instance.setDatapath(TessDataDir);
    instance.setLanguage(TessDataLang);

    Since Tess4j v4alpha, the whitelist seems broken; thus, left out from here.
    Basically variables, you can assign via Katalon Profiles:
    TessDataLang = ‘eng’
    TessDataDir = ‘D:\Dev\tools\tessdata.ocr’ <- this host the Eng data dictionary (eng.traineddata)
    Get files from: https://github.com/tesseract-ocr/tessdata

  3. Code to perform simple OCR, where imageFile is individual full-path-filename that you snapped earlier that you can perform a loop scan to stop when the TEXT you looking for found.
    try {
    String result = instance.doOCR(imageFile);
    }
    catch (TesseractException e) {
    return ‘Error while reading image’
    }

    Notes:

    1. result basically containing TEXT that from OCR.
    2. If no matched TEXT found after all snapped files processed and you sure one of the image is containing the TEXT that needs recognized; this you would need OpenCV to further enhanced the image for more ‘finer’ before passing to doOCR().
  4. Here the list of Java imports used and you need to place all the depending JAR files imported to Katalon Project or manual copy to Drivers folder (restart Katalon) you are sorted.
    import net.sourceforge.tess4j.Tesseract as Tesseract;
    import net.sourceforge.tess4j.TesseractException as TesseractException;

Let me know any further help needed.

Cheers.

2 Likes

At least once, you would need the Mobile simulator for generating the snaps.

For the above simple OCR, you don’t need Appium or any Device-simulator to up, you can even create a simple UT just for OCR testing to one or some picture files.

You can make use, e.g., Chrome Headless for the UT.


@GeneralChingChicken, there are a few ways you can take photo of the toast message:

  1. Manually and the toast message stay ‘long’ enough for the snap, e.g., Spy Mobile.

  2. Automatically using loop, see my sample code step-1 (above).
    That to perform right after some ‘simulation’ steps of which surely will trigger the toast message to appear, you just snap for some pictures immediately after your/the submission.

Before you get to either option 1 or 2, ensure you can manually simulate the toast-message successfully for starter.

Cheers.

1 Like

@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

3 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