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.