Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Take a screenshot with Cucumber

I just learn how to use cucumber. Can you tell me how to complete this code?

You can implement step definitions for undefined steps with these snippets:

Then /^I take a screenshot$/ do
    pending # express the regexp above with the code you wish you had
end
like image 865
Sokhom Ratanak Avatar asked Sep 19 '25 07:09

Sokhom Ratanak


2 Answers

Screenshots in general are taken when something unexpected occurs. You may also want to capture a screenshot to report when a test case fails. In this particular case, you should have screenshot capture logic in an @After method that will be executed for every scenario. A Java, selenium version,

@After("@browser")
public void tearDown(Scenario scenario) {
    if (scenario.isFailed()) {
            final byte[] screenshot = ((TakesScreenshot) driver)
                        .getScreenshotAs(OutputType.BYTES);
            scenario.embed(screenshot, "image/png"); //stick it in the report
    }
    driver.close();
}
like image 82
nilesh Avatar answered Sep 23 '25 10:09

nilesh


  1. You can use canned steps (pre-defined) to take screenshot.

    Then take picture
    

There is not need for any step definition. Cucumber also comes with many other pre-defined steps. See other canned steps

  1. If you still need to write step definition.

    Then /^I take a screenshot$/ do
      page.save_screenshot('image_name.png')
    end
    
like image 20
Aravin Avatar answered Sep 23 '25 12:09

Aravin