nightwatch: Take a screenshot when a test fails
I’m aware of the screenshot option when selenium errors, but I’m trying to take a screenshot when a test fails.
There’s no method (as far as I can tell) to fire a function when the test fails. There is no way to pass browser to tearDown (source).
The test doesn’t throw on error, so there’s no way I can use a try catch as far as I can tell.
Does anyone have any suggestions?
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Comments: 19
I think that this is the configuration
@sknopf
on_failureis not in the documentation. Was it removed?I also don’t understand how this is supposed to work. Should I do this?
This is added as of 0.7 - see
on_failureunder screenshots in http://nightwatchjs.org/guide#test-settingsScreenshots is a Selenium feature. Make sure you run via Selenium if you want screenshots.
If you are using TestNG and trying to capture screenshots when your script fails try this **The @AfterMethod is what you will need.
public class CaptureScreenshotsWhenTestFails extends TestCase { private static final Logger logger = LogManager.getLogge(CaptureScreenshotsWhenTestFails.class.getName()); private static WebDriver webDriver;
@AfterMethod public void tearDown(ITestResult result) { // This condition will verify if the test fails and then screenshot will be taken if(ITestResult.FAILURE==result.getStatus()) { try { //refernce of TakesScreenshot TakesScreenshot ts=(TakesScreenshot)driver; // Calling screenshot method File source=ts.getScreenshotAs(OutputType.FILE); // Copy files to your location, better to save all screenshot in ourour project home directory and // result.getName() will return name of test case so that screenshot name will be same FileUtils.copyFile(source, new File(“./Screenshots/”+result.getName()+“.png”)); System.out.println(“Screenshot taken”); } catch (Exception e) { System.out.println("Exception while taking screenshot "+e.getMessage()); }
} //end test driver.quit(); }
}