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 the
JUnit
Selenium test. The value
of this Java system property
is the full path to a directory on the machine running the adapter.
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".
@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));
}