Skip to content
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 · 25 comments
Closed

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

mattwynne opened this issue Sep 19, 2016 · 25 comments

Comments

@mattwynne
Copy link
Member

Summary

Originally raised in cucumber/common#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
Copy link
Member Author

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
Copy link
Member Author

@Lupeipei
Copy link

@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
Copy link
Member Author

👍 nice one @Lupeipei!

@sandhusatnam
Copy link

@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
Copy link
Member Author

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

@sandhusatnam
Copy link

@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
Copy link
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.

@FedericoHenze
Copy link

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
Copy link

@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
Copy link

@FedericoHenze can you share the code snippet ?

@FedericoHenze
Copy link

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
Copy link

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
Copy link

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

@aslakhellesoy
Copy link
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?

@sandhusatnam
Copy link

@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
Copy link
Member Author

@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
Copy link

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
Copy link
Member Author

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
Copy link

@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
Copy link

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 ?

@RameshGhk
Copy link

RameshGhk commented Nov 5, 2018

Hi @mattwynne
I want to print the error message after the step failed. Is there way to capture the error by using AfterConfiguration hook?

Below is the example.

AfterConfiguration do |config|
  config.on_event :test_step_finished do |event|
      if event.result.failed?
      $logger.error("Exception received as #{How can I log the error message?}")
      end
  end
end

Any suggestions ?

@mattwynne
Copy link
Member Author

Hi @RameshGhk. Please try not to use GitHub issues for support. Asking in Slack is a much better choice in future for faster response.

The event.result is a https://www.rubydoc.info/github/cucumber/cucumber-ruby-core/Cucumber/Core/Test/Result

If it's a Failed you can look at the exception property.

@RameshGhk
Copy link

RameshGhk commented Nov 9, 2018

Hi @mattwynne ,
Thanks a lot for your quick response. Yes I will follow slack going forward.

@lock
Copy link

lock bot commented Nov 9, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Nov 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants