Open Jacobvu84 opened 5 years ago
Add this dependency to the pom.xml
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>4.2.2</version>
</dependency>
create resource with path src/test/resources/tessdata
then copy language data that you want https://github.com/tesseract-ocr/tessdata to put in there
/**
*
*
/
package net.poc.questions;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import net.poc.driver.AndroidObject;
import net.poc.interactions.Wait;
import net.serenitybdd.screenplay.Actor;
import net.serenitybdd.screenplay.Question;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
/*
@author vuthelinh@gmail.com
*/
public class TextOnScreen extends AndroidObject implements Question<String> {
@Override
public String answeredBy(Actor actor) {
String content = "";
File targetFile = null;
Wait.aBit(10);
try {
File screen = getAndroidDriver(actor).getScreenshotAs(OutputType.FILE);
String file = UUID.randomUUID().toString();
targetFile = new File("./target/" + file + ".png");
FileUtils.copyFile(screen, targetFile);
ITesseract tesseract = new Tesseract();
tesseract.setDatapath("src/test/resources/tessdata");
tesseract.setLanguage("eng");
content = tesseract.doOCR(screen);
} catch (TesseractException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return content;
}
public static Question<String> displays() {
return new TextOnScreen();
}
}
The sample scripts
package serenitybdd.appiumtest.features;
import static net.serenitybdd.screenplay.GivenWhenThen.seeThat;
/**
* @author vuthelinh@gmail.com
*
*/
import static net.serenitybdd.screenplay.actors.OnStage.theActorCalled;
import static net.serenitybdd.screenplay.actors.OnStage.theActorInTheSpotlight;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import org.junit.Test;
import org.junit.runner.RunWith;
import net.poc.questions.TextOnScreen;
import net.poc.questions.factory.Message;
import net.serenitybdd.junit.runners.SerenityRunner;
import net.serenitybdd.screenplay.abilities.BrowseTheWeb;
@RunWith(SerenityRunner.class)
public class DevDebugMode extends Hook {
@Test
public void verify_text_using_tesseract_ocr() {
theActorCalled(jacob).can(BrowseTheWeb.with(hisMobileDevice));
theActorInTheSpotlight().should(seeThat(Message.showTheNotification(), equalTo("Open a demo account")));
// TextOnScreen.displays() to get text on the image. Use to verify toast message
theActorInTheSpotlight().should(seeThat(TextOnScreen.displays(), containsString("Open a demo account")));
}
}
We can not directly used Appium Client API to test toast messages. So here we will import Tess4j jar in our project and their APIs to Read Toast Messages From our Screenshot . Once we have the text on the screen we can verify the toast message text in our Appium Test Case via various assertions .