You will want to add a TestNG listener that takes a screenshot when the test fails. Here is some code for a listener taken from my Selenium Maven Template:
package com.lazerycode.selenium.listeners; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.remote.Augmenter; import org.testng.ITestResult; import org.testng.TestListenerAdapter; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import static com.lazerycode.selenium.DriverFactory.getDriver; public class ScreenshotListener extends TestListenerAdapter { private boolean createFile(File screenshot) { boolean fileCreated = false; if (screenshot.exists()) { fileCreated = true; } else { File parentDirectory = new File(screenshot.getParent()); if (parentDirectory.exists() || parentDirectory.mkdirs()) { try { fileCreated = screenshot.createNewFile(); } catch (IOException errorCreatingScreenshot) { errorCreatingScreenshot.printStackTrace(); } } } return fileCreated; } private void writeScreenshotToFile(WebDriver driver, File screenshot) { try { FileOutputStream screenshotStream = new FileOutputStream(screenshot); screenshotStream.write(((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES)); screenshotStream.close(); } catch (IOException unableToWriteScreenshot) { System.err.println("Unable to write " + screenshot.getAbsolutePath()); unableToWriteScreenshot.printStackTrace(); } } @Override public void onTestFailure(ITestResult failingTest) { try { WebDriver driver = getDriver(); String screenshotDirectory = System.getProperty("screenshotDirectory"); String screenshotAbsolutePath = screenshotDirectory + File.separator + System.currentTimeMillis() + "_" + failingTest.getName() + ".png"; File screenshot = new File(screenshotAbsolutePath); if (createFile(screenshot)) { try { writeScreenshotToFile(driver, screenshot); } catch (ClassCastException weNeedToAugmentOurDriverObject) { writeScreenshotToFile(new Augmenter().augment(driver), screenshot); } System.out.println("Written screenshot to " + screenshotAbsolutePath); } else { System.err.println("Unable to create " + screenshotAbsolutePath); } } catch (Exception ex) { System.err.println("Unable to capture screenshot..."); ex.printStackTrace(); } } }
The bit you will probably be most interested in is the method called onTestFailure. This is the part that will be triggered when a test fails. I have a driver factory that provides my access to my driver object, the call to getDriver is getting my driver object from the factory. If you have just got a statically defined driver object you can probably ignore the line:
WebDriver driver = getDriver();
The other methods are just convenience methods to create a file and write the screenshot to it. You'll obviously need to tweak this a bit to allow it to take the location that the screenshot has been written and pass it into your reported log.
I would suggest giving the listener access to your Reporter object and changing:
System.out.println("Written screenshot to " + screenshotAbsolutePath);
to:
Reporter.log("<a href=\"" + screenshotAbsolutePath + "\"><p align=\"left\">Add New PR screenshot at " + new Date()+ "</p>");
In the code above, the directory that the screenshots are saved into is set using a system property called "screenshotDirectory". You will either need to set his system property, or change the following line to a hard coded location where you would like to save your screenshots. To do that this line:
String screenshotDirectory = System.getProperty("screenshotDirectory");
Will need to change to something like:
String screenshotDirectory = "/tmp/screenshots";
or if you use windows, something like:
String screenshotDirectory = "C:\\tmp\\screenshots";