How can I figure out which step I've just executed in Cucumber's AfterStep hook?
The AfterStep
hook only receives the scenario as parameter.
What you can do, is count the steps, and then get the current one:
AfterStep do |scenario|
@step ||= 0
p scenario.steps[@step].name
@step += 1
end
This will print, in turn, the names of each parameter
how do I extract the step name from AfterStep inside of calabash-cucumber
You may refer to this example code, which works with step indexes within the AfterStep hook.
Example:
CALABASH_COUNT = {:step_index => 0, :step_line => nil}
#TODO change this approach as it breaks scenario outlines
Before do |scenario|
begin
CALABASH_COUNT[:step_index] = 0
CALABASH_COUNT[:step_line] = scenario.raw_steps[CALABASH_COUNT[:step_index]].line
rescue Exception => e
puts "#{Time.now} - Exception:#{e}"
end
end
AfterStep do |scenario|
CALABASH_COUNT[:step_index] = CALABASH_COUNT[:step_index] + 1
raw = scenario.raw_steps[CALABASH_COUNT[:step_index]]
CALABASH_COUNT[:step_line] = raw.line unless raw.nil?
end
The Behave BDD framework, in Python allows a more simple step.name
type accessor, but others seem more difficult, requiring the above technique of counting the current step and then using the index to find the name from the raw steps text.
Execute Cucumber step before/after a specific feature
Do you use cucumber-jvm? I found an article that fits your requirement.
http://zsoltfabok.com/blog/2012/09/cucumber-jvm-hooks/
Basically, do not use JUnit @BeforeClass and @AfterClass for this, as they are unaware of Cucumber Hook Tags. You would want Init and Teardown methods to run for certain scenarios only right?
@Before doesn't execute in java Cucumber Step
Make sure you are using
cucumber.annotation.Before
rather thanorg.junit.Before
. Cucumber will not process JUnit annotations. (More information in the Scenario Hooks section of this blog post.)Make sure your @Before method is
public
, notprivate
.
Can't implement two step definitions individually in cucumber
You can do in a table see the following:
Background: Member should open homepage
Given member goes to home page
@javascript
Scenario: Member should go to correct url by clicking links in head menu
Then member should ensure that links in the table go to correct url
| Spor Giyim | /spor-giyim |
| Kadın | /kadin |
| Erkek | /erkek |
| Ayakkabı & Çanta | /ayakkabi-canta |
Ruby part should be like this:
When(/^member should ensure that links in the table go to correct url$/) do |table|
# table is a Cucumber::Core::Ast::DataTable
links = table.raw
links.each do |line|
link = line[0]
target_url = line[1]
click_link(link)
url = page.current_url
expect(url).to include target_url
end
end
Cucumber puts in After hook not outputting anything
Cucumber overrides the puts
message in the RbWorld
class so that anything written with puts
gets properly broadcasted to all formatters. In the case of the pretty
formatter, these go into a delayed_messages
collection, until it calls print_messages
, which it appears to do after each step name has been printed, presumably so that messages appear to be nested under the step in which they were generated.
For some reason there is no final call to print_messages
in the pretty formatter, I'm not sure if it's an omission or deliberate since it would look less 'pretty' to have spurious messages in the output.
Interestingly if you add a second scenario, you'll see 'after the scenario' printed as the first message when the second scenario gets run, that's the delayed_messages
collection in action.
In summary, you're doing nothing wrong, it's just how Cucumber hijacks the puts
method. If you're not too bothered about these messages being nicely formatted, then you can just replace puts
with STDOUT.puts
to bypass Cucumber's formatting.
Related Topics
Store Image in Database Using Rails Paperclip Plugin
State MAChine, Model Validations and Rspec
How Are Respond_To and Respond_To_Missing Different
Installing Dm-Types on Windows. (Win7 X64)
How to Create a Custom Method for the Rails Console
Pass Command Line Argument to Vagrant Shell Script Provisioner
Error Building Ruby in Sublime Text Editor
Fileutils.Mv Throwing Invalid Char \302 and \255 Exception
Mongodb Group Using Ruby Driver
Rails Not Rolling Back Transaction After Failed Save()
Sort an Array in Ruby Ignoring Articles ("The", "A", "An")
How to Specify a Struct as the Return Value of a Function in Rubyffi
Rails Activeadmin Drop Down Menu on New and Edit Forms
Is It a Good Idea to Pass a Huge String as an Argument to Sidekiq Worker
Google Analytics API Error "Selected Dimensions and Metrics Cannot Be Queried Together."
How to Run Perl and Ruby Scripts as Tasks in Ant