How to Take specific Element screenshot in Appium | Mobile Automation:
Lets us learn how to take screenshot for the specific element, following method i’m using to get screenshot:
public static String elementScreenshot(AppiumDriver driver, MobileElement ele)
{
File screenshotLocation = null;
try{
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = ImageIO.read(scrFile);
//Get the location of element on the page
Point point = ele.getLocation();
//Get width and height of the element
int eleWidth = ele.getSize().getWidth();
int eleHeight = ele.getSize().getHeight();
//Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(), eleWidth,
eleHeight);
ImageIO.write(eleScreenshot, “png”, scrFile);
String path = “screenshots/” + UUID.randomUUID() + “” + “.png”;
screenshotLocation = new File(System.getProperty(“user.dir”) + “/” + path);
FileUtils.copyFile(scrFile, screenshotLocation);
System.out.println(screenshotLocation.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return screenshotLocation.toString();
}
it will get the element width and height then takes the element screenshot then crop the entire page screenshot to get only element screenshot.
Complete code for taking screenshot of specific element:
package practise;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import javax.imageio.ImageIO;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
public class TakeElementScreenshot {
AppiumDriver driver;
@BeforeClass
public void setUp() throws MalformedURLException
{
DesiredCapabilities cap=new DesiredCapabilities();
cap.setCapability(“deviceName”, “emulator-5554”);
cap.setCapability(“udid”, “emulator-5554”);
cap.setCapability(“appActivity”, “com.android.calculator2.Calculator”);
cap.setCapability(“appPackage”, “com.android.calculator2”);
cap.setCapability(“platformName”, “Android”);
cap.setCapability(“platformVersion”, “9.0”);
driver=new AppiumDriver(new URL(“http://0.0.0.0:4723/wd/hub”), cap);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
}
@Test
public void testExample(){
MobileElement oneBtn=driver.findElement(By.id(“com.android.calculator2:id/digit_1”));
oneBtn.click();
MobileElement plusBtn=driver.findElement(By.id(“com.android.calculator2:id/op_add”));
plusBtn.click();
MobileElement fiveBtn=driver.findElement(By.id(“com.android.calculator2:id/digit_5”));
fiveBtn.click();
MobileElement equalToBtn=driver.findElement(By.id(“com.android.calculator2:id/eq”));
equalToBtn.click();
//passing equalToBtn element to take screenshot
elementScreenshot(driver,equalToBtn);
}
@AfterClass
public void tearDown(){
driver.quit();
}
public static String elementScreenshot(AppiumDriver driver,MobileElement ele)
{
File screenshotLocation = null;
try{
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = ImageIO.read(scrFile);
//Get the location of element on the page
Point point = ele.getLocation();
//Get width and height of the element
int eleWidth = ele.getSize().getWidth();
int eleHeight = ele.getSize().getHeight();
//Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(), eleWidth,
eleHeight);
ImageIO.write(eleScreenshot, “png”, scrFile);
String path = “screenshots/” + UUID.randomUUID() + “” + “.png”;
screenshotLocation = new File(System.getProperty(“user.dir”) + “/” + path);
FileUtils.copyFile(scrFile, screenshotLocation);
System.out.println(screenshotLocation.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return screenshotLocation.toString();
}
}
Taken Screenshot:
Happy Automation. Cheers !