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.

Example

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".
	@Test
	public void testWikipediaJUnit4_test1() throws Exception {
		driver.get(baseUrl + "/");
		driver.findElement(By.id("searchInput")).clear();
		driver.findElement(By.id("searchInput")).sendKeys("blueberries");
		takeScreenShot("blueberry2.png");	// Capture screenshot of current state
		driver.findElement(By.name("go")).click();
		driver.findElement(By.cssSelector("p > i > a[title=\"Vaccinium\"]")).click();
		takeScreenShot("vaccinium2.png");	// Capture screenshot of current state
		driver.findElement(By.linkText("huckleberry")).click();
		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("java.io.tmpdir");
    	    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.apache.commons.io.FileUtils;
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.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
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;
   
    @BeforeClass
    public static void setUp()throws Exception{
        driver = new FirefoxDriver();
    }
   
    @Test
    public void step01_HomePage() throws Exception{
          listEnvironmentVariables();
   
          driver.get("http://www.wikipedia.org/");
          String i = driver.getCurrentUrl();
          System.out.println(i);
          BrowseWikipedia me = new BrowseWikipedia();
          try{
              me.takeScreenShot("Step-1.png",driver);}
          catch(Exception e){
              System.out.println("Problem, "+e.toString());}
    }
   
    @Test
    public void step02_MainPage() throws Exception{
          driver.findElement(By.linkText("English")).click();
       
          BrowseWikipedia me = new BrowseWikipedia();
          try{
              me.takeScreenShot("Step-2.png",driver);}
          catch(Exception e){
              System.out.println("Problem, "+e.toString()); }
    }
   
    @AfterClass
    public static void tearDown()throws Exception{
        driver.quit();
    }        

video icon Video

Jazz.net channel
Software Education channel

learn icon Courses

IoT Academy
Skills Gateway

ask icon Community

Jazz.net
Jazz.net forums
Jazz.net library

support icon Support

IBM Support Community
Deployment wiki