New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cucumber does not go to AfterStep if the step is failed! #1026

Closed
mattwynne opened this Issue Sep 19, 2016 · 21 comments

Comments

Projects
None yet
5 participants
@mattwynne
Member

mattwynne commented Sep 19, 2016

Summary

Originally raised in cucumber/cucumber#26 (comment) by @hangnguyenthuy

I'm using Cucumber 2.3.3
I want to print some log after each step so I have my code like this:

AfterStep do
  print_log_for_step(@current_step)
end

But in case the step is failed, it never goes to AfterStep, it move to After scenario so I cannot print log of the failed step.
Right now I have to work around in this case.

Anybody has same problem to me?

@mattwynne

This comment has been minimized.

Show comment
Hide comment
@mattwynne

mattwynne Sep 19, 2016

Member

@hangnguyenthuy this is by design. AfterStep hooks are compiled in as extra test steps, and run as part of the scenario.

In 3.0.0, you'll be able to use an an event, specifically the AfterTestStep event.

I'm working on the documentation for this now, and would love to have you try it out to see if it makes sense. I'll leave this ticket open and ping you when I have something.

Member

mattwynne commented Sep 19, 2016

@hangnguyenthuy this is by design. AfterStep hooks are compiled in as extra test steps, and run as part of the scenario.

In 3.0.0, you'll be able to use an an event, specifically the AfterTestStep event.

I'm working on the documentation for this now, and would love to have you try it out to see if it makes sense. I'll leave this ticket open and ping you when I have something.

@mattwynne

This comment has been minimized.

Show comment
Hide comment

@mattwynne mattwynne closed this Feb 5, 2017

@Lupeipei

This comment has been minimized.

Show comment
Hide comment
@Lupeipei

Lupeipei Jan 10, 2018

@mattwynne I have used test_step_finished event. Now I can capture page screenshot of every step even the step is failed, it is so cool! Thanks a lot.

Lupeipei commented Jan 10, 2018

@mattwynne I have used test_step_finished event. Now I can capture page screenshot of every step even the step is failed, it is so cool! Thanks a lot.

@mattwynne

This comment has been minimized.

Show comment
Hide comment
@mattwynne

mattwynne Jan 10, 2018

Member

👍 nice one @Lupeipei!

Member

mattwynne commented Jan 10, 2018

👍 nice one @Lupeipei!

@sandhusatnam

This comment has been minimized.

Show comment
Hide comment
@sandhusatnam

sandhusatnam Jul 6, 2018

@mattwynne I am trying the same thing as @Lupeipei did capturing screenshot of every step even the step is failed but it didn't work for me. Here's my piece of code.

AfterConfiguration do |config|
config.on_event :test_step_finished do |event|
puts "Failed at URL: '#{@browser.current_url}'"
embed("data:image/png;base64,#{@browser.screenshot_as(:base64)}",'image/png')
end
end

Any suggestions ?

sandhusatnam commented Jul 6, 2018

@mattwynne I am trying the same thing as @Lupeipei did capturing screenshot of every step even the step is failed but it didn't work for me. Here's my piece of code.

AfterConfiguration do |config|
config.on_event :test_step_finished do |event|
puts "Failed at URL: '#{@browser.current_url}'"
embed("data:image/png;base64,#{@browser.screenshot_as(:base64)}",'image/png')
end
end

Any suggestions ?

@mattwynne

This comment has been minimized.

Show comment
Hide comment
@mattwynne

mattwynne Jul 10, 2018

Member

@sandhusatnam it's hard to say... what didn't work? Did you see the puts output? What version of Cucumber are you running?

Member

mattwynne commented Jul 10, 2018

@sandhusatnam it's hard to say... what didn't work? Did you see the puts output? What version of Cucumber are you running?

@sandhusatnam

This comment has been minimized.

Show comment
Hide comment
@sandhusatnam

sandhusatnam Jul 11, 2018

@mattwynne Thanks for your reply.
I am using version 3.1.1.
I am not able to see even the puts output when the test case finished, looks to me the event doesn't get triggered.

sandhusatnam commented Jul 11, 2018

@mattwynne Thanks for your reply.
I am using version 3.1.1.
I am not able to see even the puts output when the test case finished, looks to me the event doesn't get triggered.

@aslakhellesoy

This comment has been minimized.

Show comment
Hide comment
@aslakhellesoy

aslakhellesoy Jul 11, 2018

Contributor

@sandhusatnam try replacing puts with Kernel.puts.

I think the bug here is that Cucumber’s own puts doesn’t print what’s printed in AfterStep, not that the hook doesn’t run.

Contributor

aslakhellesoy commented Jul 11, 2018

@sandhusatnam try replacing puts with Kernel.puts.

I think the bug here is that Cucumber’s own puts doesn’t print what’s printed in AfterStep, not that the hook doesn’t run.

@FedericoHenze

This comment has been minimized.

Show comment
Hide comment
@FedericoHenze

FedericoHenze Jul 11, 2018

@sandhusatnam @mattwynne I'm having the same problem.
In my case, the self instance is nil, therefore, @browser is nil and that raise a "nil:NilClass (NoMethodError)" exception.
any suggestions?

FedericoHenze commented Jul 11, 2018

@sandhusatnam @mattwynne I'm having the same problem.
In my case, the self instance is nil, therefore, @browser is nil and that raise a "nil:NilClass (NoMethodError)" exception.
any suggestions?

@sandhusatnam

This comment has been minimized.

Show comment
Hide comment
@sandhusatnam

sandhusatnam Jul 11, 2018

@aslakhellesoy I am not using the AfterStep hook, I am trying to add a event in AfterConfiguration hook.
Though I tried Kernel.puts in this but doesn't work.

sandhusatnam commented Jul 11, 2018

@aslakhellesoy I am not using the AfterStep hook, I am trying to add a event in AfterConfiguration hook.
Though I tried Kernel.puts in this but doesn't work.

@sandhusatnam

This comment has been minimized.

Show comment
Hide comment
@sandhusatnam

sandhusatnam Jul 11, 2018

@FedericoHenze can you share the code snippet ?

sandhusatnam commented Jul 11, 2018

@FedericoHenze can you share the code snippet ?

@FedericoHenze

This comment has been minimized.

Show comment
Hide comment
@FedericoHenze

FedericoHenze Jul 11, 2018

@sandhusatnam

#hooks.rb
AfterConfiguration do |config|
  config.on_event :test_step_finished do |event|
    if event.result.failed? || event.result.passed?
      screenshotName = getScreenshotName(@scenario, event.test_step, event.result)
      @driver.save_screenshot(screenshotName) unless screenshotName.nil?
    end
  end
end

getScreenshotName() is in env.rb and added to the cucumber world.
@driver instance is created like this:

#MyFeature_steps.rb
Given('I am on {string} page') do |page|
  @driver = Selenium::WebDriver.for :chrome
  @driver.get page
end

screen shot 2018-07-11 at 17 37 00

FedericoHenze commented Jul 11, 2018

@sandhusatnam

#hooks.rb
AfterConfiguration do |config|
  config.on_event :test_step_finished do |event|
    if event.result.failed? || event.result.passed?
      screenshotName = getScreenshotName(@scenario, event.test_step, event.result)
      @driver.save_screenshot(screenshotName) unless screenshotName.nil?
    end
  end
end

getScreenshotName() is in env.rb and added to the cucumber world.
@driver instance is created like this:

#MyFeature_steps.rb
Given('I am on {string} page') do |page|
  @driver = Selenium::WebDriver.for :chrome
  @driver.get page
end

screen shot 2018-07-11 at 17 37 00

@sandhusatnam

This comment has been minimized.

Show comment
Hide comment
@sandhusatnam

sandhusatnam Jul 11, 2018

@FedericoHenze try defining your webdriver instance in env.rb file and then access it in your hooks and step file. Let me know if that works

sandhusatnam commented Jul 11, 2018

@FedericoHenze try defining your webdriver instance in env.rb file and then access it in your hooks and step file. Let me know if that works

@FedericoHenze

This comment has been minimized.

Show comment
Hide comment
@FedericoHenze

FedericoHenze Jul 11, 2018

@sandhusatnam same result, I guess is not a @driver problem because everything is nil also the self instance

FedericoHenze commented Jul 11, 2018

@sandhusatnam same result, I guess is not a @driver problem because everything is nil also the self instance

@aslakhellesoy

This comment has been minimized.

Show comment
Hide comment
@aslakhellesoy

aslakhellesoy Jul 11, 2018

Contributor

I tried Kernel.puts in this but doesn't work.

Can you please be more specific than "doesn't work"? Whnat are you seeing and what did you expect to see instead?

Contributor

aslakhellesoy commented Jul 11, 2018

I tried Kernel.puts in this but doesn't work.

Can you please be more specific than "doesn't work"? Whnat are you seeing and what did you expect to see instead?

@sandhusatnam

This comment has been minimized.

Show comment
Hide comment
@sandhusatnam

sandhusatnam Jul 11, 2018

@aslakhellesoy well I am not able to see anything neither with puts nor with Kernel.puts.
I am expecting puts or Kernel.puts to print a string.

sandhusatnam commented Jul 11, 2018

@aslakhellesoy well I am not able to see anything neither with puts nor with Kernel.puts.
I am expecting puts or Kernel.puts to print a string.

@mattwynne

This comment has been minimized.

Show comment
Hide comment
@mattwynne

mattwynne Jul 12, 2018

Member

@sandhusatnam here are the scenarios for this feature:

https://app.cucumber.pro/projects/cucumber-ruby/documents/branch/master/features/docs/events/test_step_finished_event.feature

these are tested and passing. Can you see what's different between your code and the examples?

Member

mattwynne commented Jul 12, 2018

@sandhusatnam here are the scenarios for this feature:

https://app.cucumber.pro/projects/cucumber-ruby/documents/branch/master/features/docs/events/test_step_finished_event.feature

these are tested and passing. Can you see what's different between your code and the examples?

@sandhusatnam

This comment has been minimized.

Show comment
Hide comment
@sandhusatnam

sandhusatnam Jul 12, 2018

@mattwynne well I am able to print puts statement using the code snippet you mentioned above, but the test_step_results are weird.

1

Also I am not able to attach the screenshot using event.

sandhusatnam commented Jul 12, 2018

@mattwynne well I am able to print puts statement using the code snippet you mentioned above, but the test_step_results are weird.

1

Also I am not able to attach the screenshot using event.

@mattwynne

This comment has been minimized.

Show comment
Hide comment
@mattwynne

mattwynne Jul 13, 2018

Member

Ha! I think that's because they use unicode characters for check-marks and crosses to indicate passes or failure. They're probably not coming through on your terminal for some reason. Try printing result.class.name instead.

Member

mattwynne commented Jul 13, 2018

Ha! I think that's because they use unicode characters for check-marks and crosses to indicate passes or failure. They're probably not coming through on your terminal for some reason. Try printing result.class.name instead.

@sandhusatnam

This comment has been minimized.

Show comment
Hide comment
@sandhusatnam

sandhusatnam Jul 13, 2018

@mattwynne Great that works but what about the screenshot ?
I am to trying to capture a screenshot and embedd it into the cucumber report but the json report doesn't have anything in there related to screenshot.

Any suggestion ?

sandhusatnam commented Jul 13, 2018

@mattwynne Great that works but what about the screenshot ?
I am to trying to capture a screenshot and embedd it into the cucumber report but the json report doesn't have anything in there related to screenshot.

Any suggestion ?

@sandhusatnam

This comment has been minimized.

Show comment
Hide comment
@sandhusatnam

sandhusatnam Jul 13, 2018

@mattwynne I got it its not able to add screenshot because the web driver instance is nil, not sure why so as I am able to run the test case.

Also, the puts statements are not being included in the json_report.
Is that the default behavior of AfterConfiguration hook ?

sandhusatnam commented Jul 13, 2018

@mattwynne I got it its not able to add screenshot because the web driver instance is nil, not sure why so as I am able to run the test case.

Also, the puts statements are not being included in the json_report.
Is that the default behavior of AfterConfiguration hook ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment