Adding screen captures created by a JUnit Selenium test to execution results

When a JUnit Selenium test is executed it can create screen captures of the browser session to show error conditions. You can include these screen captures in the execution result of the testcase or testsuite run for later viewing.

About this task

When the JUnit Selenium adapter executes a test it will set a Java™ system property named "selenium.screenshot.dir" in the JVM that runs theJUnit Selenium test. The value of this Java system property is the full path to a directory on the machine running the adapter.
Note: To use commas to separate values in the Java System Properties field, add single quotes to it. For example, steps=JazzScenarioStep00_CustomSetup, tests='test01,test02,test03,test04'.
At the end of each JUnit Selenium test executed, the directory specified by "selenium.screenshot.dir" is searched for image files (.png, .jpg, .jpeg, .gif, .tif, .bmp). If any are found, file attachments are created on the Quality Management server, and references to those file attachments are included in the execution result.


The following sample JUnit code shows how to take a screen capture of the current state of the browser and place the screen capture file in the directory specified by "selenium.screenshot.dir".
Note: You can not change the default location of the screenshot directory that is specified by the Java System Properties field - "selenium.screenshot.dir".
	public void testWikipediaJUnit4_test1() throws Exception {
		driver.get(baseUrl + "/");
		takeScreenShot("blueberry2.png");	// Capture screenshot of current state
		driver.findElement(By.cssSelector("p > i > a[title=\"Vaccinium\"]")).click();
		takeScreenShot("vaccinium2.png");	// Capture screenshot of current state
		takeScreenShot("huckleberry2.png");	// Capture screenshot of current state

	private void takeScreenShot(String fname) throws Exception {
	    File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
	    String imageFileDir = System.getProperty("selenium.screenshot.dir");
	    if (imageFileDir == null)
	    	imageFileDir = System.getProperty("");
    	    FileUtils.copyFile(scrFile, new File(imageFileDir, fname));

If you have multiple screen captures, the following sample JUnit code shows how to display each screen capture on separate lines.

import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.Reporter;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.AfterClass;

public class BrowseWikipedia {
    public static WebDriver driver;
    public Boolean shouldTheTestPass = false;
    public static void setUp()throws Exception{
        driver = new FirefoxDriver();
    public void step01_HomePage() throws Exception{
          String i = driver.getCurrentUrl();
          BrowseWikipedia me = new BrowseWikipedia();
          catch(Exception e){
              System.out.println("Problem, "+e.toString());}
    public void step02_MainPage() throws Exception{
          BrowseWikipedia me = new BrowseWikipedia();
          catch(Exception e){
              System.out.println("Problem, "+e.toString()); }
    public static void tearDown()throws Exception{

video icon Video channel
Software Education channel

learn icon Courses

IoT Academy
Skills Gateway

ask icon Community forums library

support icon Support

IBM Support Community
Deployment wiki