How to read toast message in android


#1

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


Mobile Object Spy Unable Detect Toast Message For Automation, Workaround Available?
#2

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


#3

Solution found.

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


#4

Do you have any sample scripts here?


#5

@Chew Mong Leng do you have any samples?


#6

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


#7

@Chris_Trevarthen This code didn’t work.

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


#8

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


#9

@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?


#10

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


#11

@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


#12

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


#13

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


#14

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


#15

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


#16

Sorry guys was not on this forum actively.

@Vinh_Nguyen, @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.


Mobile Object Spy Unable Detect Toast Message For Automation, Workaround Available?
How to capture toast message in mobile?
#17

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.


#18

@Chris_Trevarthen,

Still no luck,

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


#19

@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.


#20

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