Verify Notifications or Toast messages

Can someone help me how to capture toast message or notifications messages? I feel in most of the applications we see these notifications. But how to capture the notification text message.

I saw couple of threads on this forum and this thread “https://discuss.appium.io/t/verifying-toast/3676/12” looks very vague.

Why we are not able to capture the page source also? When i tried capturing page source also which is not showing anything related to toast message? Why driver is not able to capture that message?

Can we capture this text message using UIAutomator? If Yes, then how can we do this?

Is there any delay in between the statements executing by Appium? Though the toast message is displayed, appium is unable to identify that object and later it says that NoSuchElement.

If anybody have time, then we can see this in APIDemos sample application. App -> Notification -> NotifyWithText -> Show Long Notification

UIAutomator did not allow access to toasts, and Appium for Android was based on UIAutomator, so it would not have been possible to verify toasts using Appium. Based on the thread you posted, it looks like Android fixed this deficiency with UIAutomator2. You’d need to configure your appium installation to use UIAutomator2 to accomplish this.

Thanks for your inputs Willosser.

UIAutomator2 you mean comes up with Appium 1.6.4? How to configure UIAutomation2 with Appium? Once i configure UIAutomator2, by chance can you share any pointers to get this Toast message?

I am currently using Appium 1.6.3. How can i know which version of UIAutomator that Appium is using?

Your inputs are well appreciated.

To use UiAutomator 2, you can specify
automationName: uiautomator2
in your desired capabilities and try out the new driver.

Found with the help of Google at Appium 1.6.0 · Appium for Android

Thanks for your inputs @willosser,

I am just curious to know all the methods and functionalities available with default automationName will also work with “UIAutomator 2”?

While building test suites we cannot jump in between the drivers with different automationNames right.

So i feel these functionalities should be stabilized on the default driver itself by Appium team, because Toast notifications are common validations.

Any Appium developer have any inputs regarding this?

I fear I’m going to refer you again to the documentation as I haven’t made the jump yet to uiautomator2. You can’t change the capabilities on the server without restarting the server.

@willosser

what is the element locator for toast message in Android?

after setting the uiautomator2 as desired capability, which piece of code should find the toast message?

like driver.findElement(By…);

Kindly provide your comments.

@adarshem, as I said, i haven’t made the jump to uiautomator2, so I haven’t started reading the API documentation.

Below code worked for me, you can try at your end.
WebElement toastView = driver.findElement(By.xpath("//android.widget.Toast[1]"));
String text = toastView.getAttribute(“name”);

3 Likes

Did you get this worked with UIAutomator or UIAutomator2?

1 Like

@mashkurm It worked with UIAutomator2,
As far as my knowledge we cannot identify Toast message using UIAutomator.

Ok, thanks! I will try this.

Hi All,
We can use Tessaract to verify Toast message.
pre-requisites:

  1. Need to add Tess4J dependency to maven project.
    2.Need to add libtesseract304.dll to the System Variables.
    3.Need to add liblept172.dll to the System Variables.
    4.create one folder tessdata within your maven project. This tessdata will contain “eng.traineddata” for reading english text. This may contain other files also which read other languages.
    Please find the code snippet below for the action:

public String verifyTextUsingOCR(String screenshotName)
throws IOException, TesseractException, InterruptedException {
TimeUnit.MILLISECONDS.sleep(786);
String tessVerify = testbase.captureScreenShotWithoutStorage(driver, screenshotName); //testbase is a base class
LOGGER.info(“Screenshot path:” + tessVerify);
Tesseract tesseract = new Tesseract();
LOGGER.info(“Setting tessearact path!”);
tesseract.setDatapath(ResourceHelper.getResourcePath(“tessdata”));
String fullText = tesseract.doOCR(new File(tessVerify));
LOGGER.info(fullText);
return fullText;
}


fullText contains the extracted text from the image or screen. From this You can verify Your Text as below:
String permission = page.verifyTextUsingOCRWithoutImage(“OCRVerification”);

	if (permission.contains("ALLOW")) { //You can verify Your message as per Your requirement.
		page.allowAppPermission(); //return app permission allow button in one of the scenario
		page.returnSkipButton().click(); // returns skip button locator in one of the scenario 
		page.clickIAgreeCheckbox(); //returns I Agree checkbox in one of the scenario  
	} else {
		LOGGER.info("App Permissions are already allowed! and Page is skipped");
	}

//method in testbase
public static File captureScreenShotWithoutStorage(AppiumDriver driver, String screenshotName)
throws IOException {
File srcFile = driver.getScreenshotAs(OutputType.FILE);
return srcFile;
}


Here OCR(Optical Character Recognition) will perform its task using tess4j without storing image.

Thank You friends. I will be grateful, if this post is somehow helpful for everyone.

I’m using:
io.appium 6.1.0
selenium-java 3.11.0
guava 24.0-jre.

This works for me:
String xmlFormat = driver.getPageSource();
if(xmlFormat.contains("")){
System.out.println("Toast message displayed: "+yourToastMessage);
}

  • The first line of instruction will give the xml format of the app page. In this, I got the toast message in one of the tags. It works for me.
1 Like

Thanks to @sam69 this is how I could achieve verifying text inside toast message

  • Tried to capture element identifier through Appium Desktop but it’s not possible as Toast Message stays for very short time and it’s not possible to capture exactly in Appium Desktop inspector

  • With help of Charles, slowed down network to minimum speed

  • Later used ARC technique to get page source when toast message is shown

If you don’t know about ARC, do check my blog “https://vikramviknowledgesharing.wordpress.com/2017/01/01/appium-mobile-automation/” > section “>>> Method 2: ( Appium Ruby Console ) <<<”

  • below is the code to get page source

      public boolean isToastMessageDisplayed(){
              boolean isToastMessageSeen = false;
    
              String xml1 = getDriver().getPageSource();
              Utils.waitFor(200);
              String xml2 = getDriver().getPageSource();
              Utils.waitFor(200);
              String xml3 = getDriver().getPageSource();
              Utils.waitFor(200);
              String xml4 = getDriver().getPageSource();
    
    
              if( xml1.contains("SUCCESS") && xml1.contains("User updated successfully") ) {
                  System.out.println("Toast message displayed 1: " + "SUCCESS" + "  User updated successfully");
                  return true;
              }else if( xml2.contains("SUCCESS") && xml2.contains("User updated successfully") ){
                  System.out.println("Toast message displayed 2: " + "SUCCESS" + "  User updated successfully");
                  return true;
              }else if( xml3.contains("SUCCESS") && xml2.contains("User updated successfully") ){
                  System.out.println("Toast message displayed 3: " + "SUCCESS" + "  User updated successfully");
                  return true;
              }else if( xml4.contains("SUCCESS") && xml2.contains("User updated successfully") ){
                  System.out.println("Toast message displayed 4: " + "SUCCESS" + "  User updated successfully");
                  return true;
              }
    
              return isToastMessageSeen;
          }
    

As you don’t know exact moment of toast message arrival on screen, you need to capture page source multiple times

thanks @dinesh_chakravarthy it worked for me

WebDriverWait waitForToast = new WebDriverWait(driver,25); waitForToast.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//android.widget.Toast")));
MobileElement toastMessage = driver.findElement((By.xpath("//android.widget.Toast")));
String toaster = toastMessage.getAttribute(“name”);
System.out.println(toaster);

Can you please share the complete code on how you implemented the above code?