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

Add step to AfterStep #450

Closed
drekka opened this issue May 9, 2013 · 14 comments
Closed

Add step to AfterStep #450

drekka opened this issue May 9, 2013 · 14 comments
Labels
good first issue Good for newcomers ⚡ enhancement Request for new functionality

Comments

@drekka
Copy link

drekka commented May 9, 2013

We are using frank-cucumber to run tests against an iOS app. As this app is heavily animated we need to pause after each step to ensure the animations are finished. However there are a couple of steps where we do not want to pause. We have tried the AfterStep hook however we have a significant issue with it.

AfterStep do |scenario|
   ...
end

The issue is that it does not appear to be passed the text of the step. This makes it impossible to tell what step was being executed. It seems strange to us that an AfterStep hook would be created without being passed the step that was executed. Could we please have a version like this:

AfterStep do |scenario, step|
   ...
end
@mattwynne
Copy link
Member

Hi @drekka,

This sounds sensible. We're focussed on other things right now, so if you'd like this released soon I suggest you work on a pull request. We'll be happy to coach you.

@drekka
Copy link
Author

drekka commented May 13, 2013

Hi Matt,

I've cloned the cucumber repo and browsed through the code. But my knowledge of Ruby is holding me up. I currently know about enough to cause havoc and not much more :-) I could see several spots in the code where the AfterStep code is called and a scenario passed. But I could not figure out how this is initiated (essentially how the cucumber code works). The sticking point seems to be that various invokes pass the scenario as an argument which is then passed onto to other code which I cannot identify. My first thought was to add another parameter to the calls which contained the step (text?) or nil. But now I'm wondering if the argument should be some sort of class instance containing the "state" at that point in the story execution. This state would of course contain the scenario, current step and any other useful info.

@mattwynne
Copy link
Member

Hi @drekka,

Thanks for looking into it. The best contribution you could make would be to fork the Cucumber repo and try to write a new scenario somewhere in features/docs that describes the behaviour you'd like. Have a look around the features for other examples, e.g. https://github.com/cucumber/cucumber/blob/master/features/docs/defining_steps/transforms.feature

Once you have a failing scenario, send it in a pull request and we can go from there.

@chewi
Copy link

chewi commented Sep 3, 2013

If (like me) you need this quickly, this seems to work. You may need to adjust it for Cucumber 2.

AfterStep do |scenario|
  step = scenario.steps.find { |s| s.status == :skipped }

  if step.name.include?("I peel a banana")
    # Do stuff!
  end
end

Note that step.name doesn't include Given/When/Then. This is stored in step.keyword.

@mattwynne
Copy link
Member

Sadly, @chewi's hack will not work on 2.0 as things stand. We will aim to support this properly through the API during the 2.0 series. I'll keep the ticket open.

It would be awesome if someone wanted to start a PR with a failing test for this.

@mattwynne mattwynne added the good first issue Good for newcomers label Mar 13, 2015
@mattwynne mattwynne modified the milestones: 2.0, 2.1 Mar 13, 2015
@Carlbc18
Copy link

I am also interested in this functionality.

@djwgit
Copy link

djwgit commented Oct 21, 2015

like to have this back too, as in 1.3.x
in 2.x, scenario reference in the hook is a different one... hope you guys could fix it soon, thanks
and it does make sense to be able to access the steps within the hook...

@mattwynne
Copy link
Member

If you're interested in this, please start with a failing scenario that describes what you want.

@jdougit
Copy link

jdougit commented Oct 21, 2015

@mattwynne, thanks for your reply...

basically, I run cucumber test on jenkins, by default, the output flushed out to jenkins console after a scenario is finished. but I'd like to have the output send to jenkins console immediately,

  • before a step executed, print out the step to console.
  • after that, there are more console output during the test code of the step,
    that makes the console output more readable and easier to located an issue if fails)
    to have it this way, I print out steps in the Before and AfterStep hooks... (might not be a common usage for others...)

after reading the cucuber-ruby code, seems,

  • in 1.3.8 the scenario instance in the hook is the "real" scenario class instance, it has raw_steps attributes.
  • but after 2.x, the scenario passed into hook is not that one anymore... instead, it is
    Cucumber::RunningTestCase::Scenario, and from this, not sure how to access its steps...

I have this below in my env.rb

# env.rb
# don't buffer the console output. get real-time update on jenkins console page
$stdout.sync = true

# run before each Scenario
Before do |scenario|
  print("Scenario: #{scenario.title}\n")
  $stop_count = scenario.raw_steps.count
  $step_index = 0
  # print out the first step if exists
  if $step_index < $stop_count
    print("    #{scenario.raw_steps[$step_index].keyword}#{scenario.raw_steps[$step_index].name} \n")
  end
end

# after each step
AfterStep do |scenario|
  # print out next step if exists
  $step_index += 1
  if $step_index < $stop_count
    print("    #{scenario.raw_steps[$step_index].keyword}#{scenario.raw_steps[$step_index].name} \n")
  end
end

@mattwynne
Copy link
Member

Hi @jdougit

Try calling #test_steps on that object you get in the block. That should give you what you need.

@t-morgan
Copy link
Contributor

Just want to verify what I am seeing with Cucumber 2.1.0 is the same as what others see...

Given I have an env.rb file with:

AfterStep do |argument|
  puts argument.class
  argument.each { |element| puts element.class }
end

When I run a feature
Then the output is:

Array

Cucumber::Core::Test::Result::Passed
1 scenario (1 passed)
1 step (1 passed)

@pirelenito
Copy link

@t-morgan I can verify the same behavior with Cucumber 2.1.0.

@mattwynne
Copy link
Member

Closed by 8b347ca

@lock
Copy link

lock bot commented Oct 25, 2018

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 Oct 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
good first issue Good for newcomers ⚡ enhancement Request for new functionality
Projects
None yet
Development

No branches or pull requests

9 participants