Step 1:
Setup the appium client local server to run the Android Device's Default System Chrome Application.
Step 2:
Now create a public java class in eclipse to initiate and launch the Android chrome app. And call the other class to capture the current mobile app screen and save it to Eclipse's workspace path.
CLASS 1: screenshotDemo
package com;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import net.sourceforge.tess4j.TesseractException;
@TestMethodOrder(OrderAnnotation.class)
public class screenCapture {
public static AndroidDriver<AndroidElement> driver;
String url="http://127.0.0.1:4723/wd/hub";
@Test
@Order(1)
public void setup() {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("platformName", "Android");
caps.setCapability("platformVersion", "10.0");
caps.setCapability("deviceName", "emulator");
caps.setCapability("automationName","UiAutomator2");
caps.setCapability("appPackage","com.android.chrome");
caps.setCapability("appActivity","com.google.android.apps.chrome.Main");
try {
driver = new AndroidDriver<AndroidElement>(new URL(url), caps);
// driver.resetApp();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
@Test
@Order(2)
public void captureSs() throws TesseractException{
saveAndReadImage srimage= new saveAndReadImage();
srimage.readToastMessages(driver);
}
@Test
@AfterAll
public static void Khallas() {
driver.quit();
}
}
CLASS 2: saveAndReadImage
package com;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.imageio.ImageIO;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import net.sourceforge.tess4j.util.LoadLibs;
public class saveAndReadImage {
static String screenShotDir = "screenshots";
File scrFile;
static File screenShotDirPath = new java.io.File("./"+ screenShotDir+ "//");
String destFile;
public String result=null;
public int r = 0;
public int g = 0;
public int b = 0;
public int a = 0;
public int gr = 0;
public String readToastMessages(AndroidDriver<AndroidElement> driver) throws TesseractException {
String imgName = takeScreenShot(driver);
File imageFile = new File(screenShotDirPath, imgName);
System.out.println("Image name is :" + imageFile.toString());
ITesseract instance = new Tesseract();
instance.setTessVariable("user_defined_dpi", "270");
File tessDataFolder = LoadLibs.extractTessResources("tessdata");
// Extracts Tessdata folder from referenced tess4j jar for language support
instance.setDatapath(tessDataFolder.getAbsolutePath()); // sets tessData // path
result = instance.doOCR(imageFile);
System.out.println(result);
return result;
}
/**
* Takes screenshot of active screen
*
* @return ImageFileName
* @throws InterruptedException
*/
public String takeScreenShot(AndroidDriver<AndroidElement> driver){
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy__hh_mm_ssaa");
new File(screenShotDir).mkdirs(); // Create folder under project with name
// "screenshots" if doesn't exist
destFile = dateFormat.format(new Date()) + ".png"; // Set file name using current date time.
BufferedImage img=null;
try {
img=ImageIO.read(scrFile);
BufferedImage colorImage=new BufferedImage(img.getWidth(),img.getHeight(),BufferedImage.TYPE_INT_ARGB);
for(int i=0; i<img.getHeight();i++){
for(int j=0; j<img.getWidth();j++){
Color c=new Color(img.getRGB(j, i));
this.r=c.getRed();
this.g=c.getGreen();
this.b=c.getBlue();
this.a=c.getAlpha();
Color cColor=new Color(r,g,b,a);
colorImage.setRGB(j, i, cColor.getRGB());
}
}
ImageIO.write(colorImage,"png",new File(screenShotDir + "/" + destFile));
} catch (IOException e) {
System.out.println("Image not transfered to screenshot folder");
e.printStackTrace();
}
return destFile ;
}
}