0

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:

Current cucumber test example

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!

asked Feb 13, 2017 at 13:10

1 Answer 1

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
answered Feb 22, 2017 at 6:54
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply @Justin Ko. I copy and paste exactly as you put it but didn't work so I did some changes using a begin rescue block and voilá it worked!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.