4

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
Hugo Dozois
  • 8,147
  • 12
  • 54
  • 58
Sokhom Ratanak
  • 5,836
  • 4
  • 14
  • 11

6 Answers6

9

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();
}
nilesh
  • 14,131
  • 7
  • 65
  • 79
3

I am providing the code which will take the snapshot when scenario is failed, I hope you can modify according to your uses, Comment here if you can't do that. This code is in ruby with Ubuntu system

#Create a directory for storing snapshot
dir_path = "/home/vchouhan/vijay_work/snapshot"
unless Dir.exist?(dir_path)
    Dir.mkdir(dir_path,0777)
    puts "=========Directory is created at #{dir_path}"
else
    puts "=========Directory is exist at #{dir_path}"
end

#Run after each scenario
After do |scenario|
  #Check, scenario is failed?
  if(scenario.failed?)
         time = Time.now.strftime('%Y_%m_%d_%Y_%H_%M_%S_')
         name_of_scenario = time + scenario.name.gsub(/\s+/, "_").gsub("/","_")
         puts "Name of snapshot is #{name_of_scenario}"
         file_path = File.expand_path(dir_path)+'/'+name_of_scenario +'.png'
         page.driver.browser.save_screenshot file_path
         puts "Snapshot is taken"
    puts "#===========================================================#"
    puts "Scenario:: #{scenario.name}"
    puts "#===========================================================#"
  end
end
messivanio
  • 2,263
  • 18
  • 24
Vijay Chouhan
  • 4,613
  • 5
  • 29
  • 35
3
  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
    
Aravin
  • 6,605
  • 5
  • 42
  • 58
  • 3
    I think you linked canned steps for Calabash, not Cucumber – stansult Mar 02 '16 at 20:54
  • I don't think this is a good idea as it seems to introduce a synthetic step just for screenshots which is in no way related to the business scenario being tested. Better to use a Cucumber hook so screenshots can be decided to be taken after each step is executed. – Big Kahuna Mar 24 '17 at 08:44
  • @BigKahuna It depends on whether you are using this step purely for debugging purposes only - and whether you also make this a function to use when you are going through step definitions too. – KyleFairns Jun 06 '17 at 09:09
0

In Java you can implement this step like so,

@Then("^I take a screenshot$")
public void i_take_a_screenshot()
{
  // Your code goes here
}
The Cat
  • 2,375
  • 6
  • 25
  • 37
  • 1
    I don't think this is a good idea as it seems to introduce a synthetic step just for screenshots which is in no way related to the business scenario being tested. Better to use a Cucumber hook so screenshots can be decided to be taken after each step is executed. – Big Kahuna Mar 24 '17 at 08:44
0

If you are using watir-webdriver for your testing you can call the screenshot method on your browser object and save it. http://watirwebdriver.com/screenshots/

If you are doing windows controls you could use the win32/screenshot gem to achieve this.https://github.com/jarmo/win32screenshot

airazor
  • 51
  • 3
0

An improved version of the previous answer. This has error handling, writing out the URL at the failure point. Thought it might be useful.

@After("@UI" )
public void embedScreenshotOnFail(Scenario s) {
    if (s.isFailed()) {
        try {
            byte[] screenshot = ((TakesScreenshot) getDefaultDriver()).getScreenshotAs(OutputType.BYTES);
            s.embed(screenshot, "image/png" );
            s.write("URL at failure: " + getDefaultDriver().getCurrentUrl());
        } catch (WebDriverException wde) {
            s.write("Embed Failed " + wde.getMessage());
        } catch (ClassCastException cce) {
            cce.printStackTrace();
        }
    }
}
Joviano Dias
  • 1,043
  • 10
  • 13