Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access the scenario and example names in Cucumber?

Tags:

cucumber

I'm using cucumber to generate test scripts that can be executed by a tool or human... so not the standard use.

However I would like to pass through the scenario and example names through to my output.

Is this possible?

like image 937
Nigel Thorne Avatar asked Mar 19 '09 23:03

Nigel Thorne


People also ask

How do you get the scenario name in Cucumber step?

Inside the step definition, you can use CucumberHelper. scenario. getName() . Based on this API you can use getID , getSourceTagNames , getStatus and getClass methods.

How do you use Cucumber scenarios?

The Scenario Outline keyword can be used to run the same Scenario multiple times, with different combinations of values. The keyword Scenario Template is a synonym of the keyword Scenario Outline . We can collapse these two similar scenarios into a Scenario Outline .

Can we parameterize scenario name in Cucumber?

Parametrization in CucumberCucumber supports Data Driven Testing using Scenario Outline and Examples keywords. Creating a feature file with Scenario Outline and Example keywords will help to reduce the code and testing multiple scenarios with different values.

Why do we use examples keyword in Cucumber?

We can perform data-driven testing with the help of keyword Examples. We shall also take the help of keyword Scenario Outline to execute the same Scenario over multiple values. The data sets to be taken into consideration shall be passed below the Examples section one after another separated by | symbol.


2 Answers

Found it.. (with some help from Tim Walker)

Before do |scenario|
 puts "Before Scenario: #{scenario.to_sexp[2]}"
 .
 .
 .
end

Your SExpression may differ, so it's worth doing a scenario.to_sexp.inspect to see what that tree is.

Aslak is keen to avoid exposing properties on his classes (which is a decision I happen to agree with, so I'm happy to do this work around).

like image 162
Nigel Thorne Avatar answered Oct 23 '22 03:10

Nigel Thorne


A more serious answer (or at least, suggestion): make use of ruby's reflection to try to find what you are looking for. Grab likely objects, find out what methods they have, and see if you can find it. For example:

File.open('happy_hunting.log','a') { |f|
    f.print "Scenario supports: #{(scenario.methods - Object.methods).inspect}\n"
    }

and then repeat it to figure out whats where.

Another suggestion, look at the source.

like image 41
MarkusQ Avatar answered Oct 23 '22 02:10

MarkusQ