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.
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.
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?
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.
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”);
Hi All,
We can use Tessaract to verify Toast message.
pre-requisites:
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.
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.
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