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