Generally, People use an interface called TakeScreenshot to capture a snapshot with Selenium WebDriver. But the only limitation I see here that it captures only the visible area of the screen but what happened if the requirement is to capture the snapshot from top to bottom in one go like in Amazon.com where multiple products are listed and we need to capture the snapshot from top to bottom.
In this scenario TakeScreenshot will fail as this will capture only the visible area not top to bottom in one go. Here, in this scenarion we can take help from AShot which is a third party API which can be integrated with Selenium API to capture the screenshot from top to botton.
First, we will see that how we capture a snapshot using TakeScreenshot interface. The below code will help to capture the visible area of the screen.
import io.github.bonigarcia.wdm.WebDriverManager;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class ScreenShot
{
static WebDriver driver;
public static void main(String args[])
{
WebDriverManager.chromedriver().setup();
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://www.google.co.in");
File Src=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(Src, new File("c:/vikas/img.jpg"));
}
catch(IOException io)
{
System.out.println(io.getMessage());
}
}
}
or the above same thing we can do with the help of AShot. Please see the below code where I have shown that how we can capture the snapshot using AShot.
First, download the dependency for AShot from MVN Repository and add it to your Maven or Gradle project.
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class ScreenShot
{
static WebDriver driver;
public static void main(String args[])
{
WebDriverManager.chromedriver().setup();
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://www.google.co.in");
Screenshot screenshot=new AShot().takeScreenshot(driver);
try {
ImageIO.write(screenshot.getImage(), "PNG", new File("C:/Vikas/img.png"));
}
catch(IOException io){
System.out.println(io.getMessage());
}
}
}
Now, how we can utilize AShot in case the full screenshot needs to be captured from top to bottom. Please see the below code.
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class ScreenShot
{
static WebDriver driver;
public static void main(String args[])
{
WebDriverManager.chromedriver().setup();
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://www.google.co.in");
driver.findElement(By.name("q")).sendKeys("StapleAutomationSolutions");
driver.findElement(By.name("q")).sendKeys(Keys.ENTER);
Screenshot screenshot=new AShot().shootingStrategy(ShootingStrategies.viewportPasting(10)).takeScreenshot(driver);
try {
ImageIO.write(screenshot.getImage(), "PNG", new File("C:/Vikas/img.png"));
}
catch(IOException io)
{
System.out.println(io.getMessage());
}
}
}
Now, If there is need to capture the snapshot for a specific element or an object. Please use the below code.
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.coordinates.WebDriverCoordsProvider;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class ScreenShot
{
static WebDriver driver;
public static void main(String args[])
{
WebDriverManager.chromedriver().setup();
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://www.yahoo.com");
WebElement Yahoologo=driver.findElement(By.id("header-logo"));
Screenshot screenshot=new AShot().coordsProvider(new WebDriverCoordsProvider()).takeScreenshot(driver,Yahoologo);
try {
ImageIO.write(screenshot.getImage(), "PNG", new File("C:/Vikas/img.png"));
}
catch(IOException io)
{
System.out.println(io.getMessage());
}
}
}
OR
I will keep adding more automation concepts in my upcoming posts. Please keep me posted if you have any questions and concerns.import io.github.bonigarcia.wdm.WebDriverManager;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class ScreenShot {
static WebDriver driver;
public static void main(String args[]) {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://www.yahoo.com");
WebElement Yahoologo = driver.findElement(By.id("header-logo"));
File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
Point location = Yahoologo.getLocation();
int width = Yahoologo.getSize().getWidth();
int height = Yahoologo.getSize().getHeight();
try {
BufferedImage fullimage = ImageIO.read(src);
BufferedImage subImage = fullimage.getSubimage(location.getX(), location.getY(), width, height);
ImageIO.write(subImage, "PNG", src);
FileUtils.copyFile(src, new File("C:/Vikas/img.png"));
} catch (IOException io) {
System.out.println(io.getMessage());
}
}
}
Best of Luck.