[Ruby] in cucumber 2.0 how tell if failing scenario is an outline row, or regular scenario?

112 views
Skip to first unread message

Chuck van der Linden

unread,
Apr 14, 2015, 6:14:55 PM4/14/15
to cu...@googlegroups.com
So, I have code that worked great in cucumber 1, that runs in the after hook to embed a bunch of stuff into test results, and generate screenshots etc when a scenario fails.   I had code to generate filenames from scenario names, but to properly name things, that code detected if the scenario was an instance of ExampleRow.   but ExampleRow seems gone/moved/deprecated with 2.0 and I'm not even sure if the scenario would be an instance of that or not.   So I am wondering what would be the recommended way to refactor the following code for running in cucumber 2.x

From env.rb  (the second conditional fails on 2.x, and is my main concern at this point.. what would be the best way to re-factor the first 8 lines or so for cucumber 2.x ? )

After do |scenario|
if scenario.failed?
# create a name from outline+example, or scenario name to use when saving artifacts
if scenario.instance_of?(Cucumber::Ast::OutlineTable::ExampleRow)
scenario_name = scenario.scenario_outline.name.gsub(/[^\w\-]/, ' ')
scenario_name += "-Example#{scenario.name.gsub(/\s*\|\s*/, '-')}".chop
else
scenario_name = scenario.name.gsub(/[^\w\-]/, ' ')
end

# save screenshots and embed in reports
@browser.driver.save_screenshot("./results/#{scenario_name}_screenshot.png")
embed "./results/#{scenario_name}_screenshot.png", "image/png"
# below works for html reports, generates error when json reports enabled
### TO-DO retest this on cucumber 2.x to see if it ever got fixed
## create fully embedded (not just linked) screenshot
#encoded_img = @browser.driver.screenshot_as(:base64)
#embed("data:image/png;base64,#{encoded_img}",'image/png')

# for brevity I have snipped out further code that saves files holding page html, 
    # cookies, checks if there was a JS error and reports that if found, etc.

end

Björn Rasmusson

unread,
Apr 15, 2015, 2:14:25 AM4/15/15
to cu...@googlegroups.com
 
Hi,

The first thing to try in v2.0 is to simply use:
  scenario_name = scenario.name

The scenario.name for a scenario from a scenario outline is in v2.0: "<outline name>, <examples table name> (#<row number)", so that might work for you.

If you absolutely want the cell values in the scenario_name, then you will have to use:
  scenario.outline?
to check if the scenario is from a scenario outline, and then use
  scenario.cell_values
to get the cell values.

Best Regards
Björn

Reply all
Reply to author
Forward
0 new messages