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

Most upvoted comments

I think that this is the configuration

{
  "test_settings" : {
    "default" : {
      "screenshots" : {
        "enabled" : false,
        "path" : "",
        "on_failure": true
        "on_error": true
      }
    }
  }
}

@sknopf on_failure is not in the documentation. Was it removed?

I also don’t understand how this is supposed to work. Should I do this?

{
  "test_settings" : {
    "default" : {
      "screenshots" : {
        "enabled" : false,
        "path" : "",
        "on_failure": true
      }
    }
  }
}

This is added as of 0.7 - see on_failure under screenshots in http://nightwatchjs.org/guide#test-settings

Screenshots is a Selenium feature. Make sure you run via Selenium if you want screenshots.

    test_settings: {
        selenium: {
            webdriver: {
                start_process: false
            },
            desiredCapabilities: {
                browserName: "chrome",
                javascriptEnabled: true,
                acceptSslCerts: true,
                chromeOptions: {
                    w3c: false,
                    args: [
                        "--headless",
                        "--no-sandbox",
                        "--disable-gpu",
                        "--window-size=1300,800"
                    ]
                }
            },
            screenshots: {
                enabled: true,
                on_failure: true,
                on_error: true,
                path: ""
            },
            selenium: {
                start_process: true,
                host: "127.0.0.1",
                port: 4444,
                server_path: require("selenium-server").path,
                cli_args: {
                    "webdriver.chrome.driver": require("chromedriver").path
                }
            }
        }
    }

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;

@Before
public void setUp() throws Exception {
    
}

@Test
public void testMainTest() throws Exception {
    logger.info("This will be your test body ");

   }

@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(); }
}