If you are using Cucumber to automate testing your web application, it can be useful to include a screenshot in Cucumber’s report whenever a scenario fails.
Cucumber itself doesn’t provide functionality for taking screenshots, but it does make it possible to embed them in the report. If you are using Cucumber together with a Selenium WebDriver, which can capture screens, it’s easy as pie. All you have to do is paste the following lines of Java code into your Cucumber steps definition:
1 2 3 4 5 6 7 8 |
@After public void tearDown(Scenario scenario) { if (scenario.isFailed()) { // Take a screenshot... final byte[] screenshot = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.BYTES); scenario.embed(screenshot, "image/png"); // ... and embed it in the report. } } |
Can you please provide information on what to pass to Scenario parameter in screenshot method “public void tearDown(Scenario scenario)”
Thanks for your information.
Appreciate if you can through some light on this….
********************************************************************************************************************************************************************
@Test(groups = “cucumber”, description = “Check WR report”, dataProvider = “features”)
public void feature(CucumberFeatureWrapper cucumberFeature) {
testNGCucumberRunner.runCucumber(cucumberFeature.getCucumberFeature());
Scenario scenario=(Scenario)testNGCucumberRunner.runCucumber(cucumberFeature.getCucumberFeature());
if (scenario.isFailed()) {
String screenshotName = scenario.getName().replaceAll(” “, “_”);
try {
TakesScreenshot ts = (TakesScreenshot) driver;
File sourcePath = ts.getScreenshotAs(OutputType.FILE);
File destinationPath = new File(
System.getProperty(“user.dir”) + “\\target\\cucumber-reports\\screenshots\\” + screenshotName + “.png”);
FileUtils.copyFile(sourcePath, destinationPath);
Reporter.addScreenCaptureFromPath(destinationPath.toString());
} catch (WebDriverException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
**************************************************************************************************************************************************************
Please explain my fault areas and suggest .
i am using same code in cucumber then it’s showing parameters missed
if you want at some specific location then execute code like this..
if (scenario.isFailed()) {
File scrFile = ((TakesScreenshot)dictionaryPage.getDriver()).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(scrFile, new File(“C:/Users/Public/Downloads/TestScreenshot/Error.jpg”));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
it embeds screenshot in cucumber report found in the target directory.
thanks.. it works
where i can see the screenshots after executing my feature file.
Hi, I have the same question that Anita
Where exactly it stores the screenshot?