How to read toast message in android

Hi all,
How to read toast message in android version 7.
Thanks

1 Like

Hi,
I am also facing the same issue. Any Solution?
Thanks

Solution found.

Use mobile screen(s) captured, then using the approach here to automate:

Do you have any sample scripts here?

@Chew Mong Leng do you have any samples?

Hi U.G.,

You can try this for Android toasts:

import com.kms.katalon.core.mobile.keyword.internal.MobileDriverFactory
import io.appium.java_client.AppiumDriver

AppiumDriver<?> driver = MobileDriverFactory.getDriver()
def toast = driver.findElementByXPath("//android.widget.Toast[@text='Added to cart']")

Hope this helps,

Chris

3 Likes

@Chris_Trevarthen This code didnā€™t work.

Can not capturer Toast message on Android. were you able to capture Toast message?

Hi @GeneralChingChicken,

Iā€™m able to capture the toast message on an Android emulator using API v27. Note that my tests are using ā€œuiautomator2ā€, which I think is whatā€™s required for capturing the toasts. I didnā€™t have to do anything special to set that up, though, it seemed to be the default. You should be able to see in your logs the automationName property of Desired Capabilities whether uiautomator 2 is being used.

I wonder if you could log out what Appium thinks is on the screen at the time when the toast appears. If you want to see some more detail about exactly what Appium/Katalon detects on the screen you can log out the XML contents of the screen. You should be able to do that by putting the following import statements at the top of your test case:

import com.kms.katalon.core.logging.KeywordLogger
import com.kms.katalon.core.mobile.keyword.internal.MobileDriverFactory
import io.appium.java_client.AppiumDriver

Then in the code for your test, add the following right after the toast is expected to appear:

AppiumDriver<?> driver = MobileDriverFactory.getDriver()
KeywordLogger log = new KeywordLogger()
log.logInfo(driver.getPageSource())

When the test runs, you should see an entry in the Log Viewer for ā€œStatement - log.logInfo(driver.getPageSource())ā€ that shows the start of the XML for the screen. If you tap on it, youā€™ll see all of the XML for the screen on the right-hand pane.

I would check that XML to see if the toast shows up there. If not, then you might consider adding a MobileBuiltInKeywords.delay() of a second and try printing the screen again to see if the test check is firing before the toast appears.

Hope this helps,

Chris

2 Likes

@Chris_Trevarthen,

My XML isnā€™t picking up the toast message. I try adding the delay but nothing is showing up.
is your value for automationName - ā€œuiautomator 2ā€ with a space or ā€œUiAutomator2ā€?

AppiumDriver<?> what goes inside the "<?>"
ā€“What version of Appium do you have?

Hi @GeneralChingChicken,

When logging my Desired Capabilities, the automationName shows as uiautomator2 (all one word, all lower case). Hereā€™s what my full Desired Capabilities output looks like:

{"desiredCapabilities":
  {"app":"/Users/chris/mobile-beta.apk",
  "autoGrantPermissions":true,
  "automationName":"uiautomator2",
  "bootstrapPath":"/Users/chris/WebDriverAgent",
  "deviceId":"emulator-5554",
  "deviceName":"emulator-5554 (Android SDK built for x86 - Android 8.1.0)",
  "fullReset":false,
  "newCommandTimeout":1800,
  "noReset":true,
  "platform":"ANDROID",
  "platformName":"Android",
  "udid":"emulator-5554",
  "usePrebuiltWDA":true,
  "useXctestrunFile":true
}

If you walk through your app with the Katalon Mobile Spy, are you able to see the toast, especially are you able to capture it as a Test Object when it shows on your screen? If not, that might be a sign of some other incompatibility with an Android SDK version.

ā€“ Chris

2 Likes

@Chris_Trevarthen

Object Spy Isnā€™t seeing Toast message object. ā€“ I will continue my investigation and see what I can find and up this this forum

Hi @GeneralChingChicken,

Iā€™ve been reading some Appium github posts from people who were unable to view toast messages, and it seems to point to having older versions of Android SDK. I wonder if youā€™re able to update your version of Android Studio to the latest, could you see the toast messages in the Spy Mobile then?

ā€“ Chris

Did that yesterday but I think I uninstall my VDs and reinstall them and try again.

1 Like

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