How to Take a Screenshot With Selenium, TestNG and Maven

Leyla GORMEL
2 min readMay 11, 2021

Most of the time, we may need to take screenshots. We can take screenshots during test execution. This is how we can check UI errors or bugs. For example; If we need to take screenshots of elements or if we are using headless mode selenium, we may need to take and store screenshots and use these to check or compare with the correct ones.

I am going to show you three different types of taking a screenshot.

First one: To take a screenshot of the page that you see on the browser’s screen.

We need to add apache.commons.io’s source code to pom.xml file so we can use FileUtils options.

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>

— —

public void TakeScreenShot() throws Exception{
TakesScreenshot scrShot =((TakesScreenshot)driver);
File SourceFile=scrShot.getScreenshotAs(OutputType.FILE);
File DestFile=new File("yourpath/screenshot1.png");
FileUtils.copyFile(SourceFile, DestFile);
}

Second one: To take a screenshot of all pages. Scroll down and take screenshots every second

We need to add ru.yandex.qatool’s source code to pom.xml file and add these libraries.

<dependency>
<groupId>ru.yandex.qatools.ashot</groupId>
<artifactId>ashot</artifactId>
<version>1.5.4</version>
</dependency>

— -

import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;

— -

public void TakeScreenShotAllPage() throws Exception{
Screenshot s=new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
ImageIO.write(s.getImage(),"PNG",new File("ypurpath/screenshot2.png"));
}

Last one: To find an element and take its screenshot

public void OneElementScreenShot() throws Exception{
WebElement oneImage = driver.findElement(By.xpath(IMAGE_PATH));
File sourceFile = oneImage.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(sourceFile, new File("yourpath/screenshot3.png"));
}

All code:

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.apache.commons.io.FileUtils;
import java.io.File;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;
import javax.imageio.ImageIO;


public class ScreenShot {
public final String DRIVER_PATH = "Drivers/chromedriver";
public final String DRIVER_TYPE = "webdriver.chrome.driver";
public WebDriver driver;
public final String BASE_URL = "https://www.bbc.com/";
public final String FILE_PATH = "path/screenshot1.png";
public final String IMAGE_PATH = "//*[@id=\"page\"]/section[3]/div/ul/li[1]/div/div[2]/h3/a";

@BeforeTest
public void beforeTest(){
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications","--ignore-certificate-errors","--disable-extensions");
System.setProperty(DRIVER_TYPE,DRIVER_PATH);
driver = new ChromeDriver(options);
driver.manage().window().maximize();
driver.get(BASE_URL);
}

@Test (priority = 0)
public void TakeScreenShot() throws Exception{
TakesScreenshot scrShot =((TakesScreenshot)driver);
File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);
File DestFile=new File(FILE_PATH);
FileUtils.copyFile(SrcFile, DestFile);

}

@Test (priority = 1)
public void TakeScreenShotAllPage() throws Exception{
Screenshot s=new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
ImageIO.write(s.getImage(),"PNG",new File("path/screenshot2.png"));

}

@Test (priority = 2)
public void OneElementScreenShot() throws Exception{
WebElement oneImage = driver.findElement(By.xpath(IMAGE_PATH));
File sourceFile = oneImage.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(sourceFile, new File("yourpath/screenshot3.png"));

}

@AfterClass
public void CloseDriver(){
driver.close();

}
}

I gave priority to these Tests. Its means these test cases run in order to priority number. Priority 1 is first, priority 2 is second…

--

--