First time here so I'll try to be most readable possible. I have a test in a feature file which uses a datatable for sorting some data as seen below:
Currently I am using scenario.test_steps.map(&:name) to get all the steps (this is necessary because of an integration to an application lifecycle software manager) in an array and this is what I get:
Cucumber steps got in the hooks file
My question is: is it possible to get the datatable information in the Before do |scenario| hook over the hooks file?
Thanks in advance to anyone who helps!
1 Answer 1
When iterating through scenario.test_steps, each test step has an associated Cucumber::Core::Ast::Step. This contains the step specific information such as the step name, data table, etc. The associated Ast::Step will be the last element of the test step's source:
test_step.source
#=> [
#=> #<Cucumber::Core::Ast::Feature "Feature: Something" (features/something.feature:1)>,
#=> #<Cucumber::Core::Ast::Scenario "Scenario: Only a test" (features/something.feature:3)>,
#=> #<Cucumber::Core::Ast::Step "Given : the fields" (features/something.feature:4)>
#=> ]
To access the Ast::Step multi-line argument, check the multiline_arg. If a data table has been specified, an Ast::DataTable will be returned. Otherwise, an Ast::EmptyMultilineArgument will be returned. You can check if the returned value is a data table by calling data_table?.
As an example, the below would iterate through each test step and output the data table if defined:
Before do |scenario|
scenario.test_steps.each do |test_step|
multiline_arg = test_step.source.last.multiline_arg
puts multiline_arg.raw if multiline_arg.data_table?
end
end
1 Comment
begin rescue block and voilá it worked!Explore related questions
See similar questions with these tags.