Get all tags from a .feature file

4,762 views
Skip to first unread message

Simon P.

unread,
Apr 9, 2015, 11:21:21 AM4/9/15
to cu...@googlegroups.com
Does anyone know if there is a way to get all the tags from a .feature file and use them in my .java step definition file?

I saw there are some gherkin.formatter.model.Tag and gerkin.formatter.model.Scenario libs that exist and maybe .getTags() can be used, but how do I reference the specific Scenario I want?

If that's not possible, do I need to convert my Gherkin file into json first?

I've searched and haven't found anything about this.

example:
@HelloWorld @alive
 Scenario: Say hello
 When I say hello!
 Given the world is around
 Then the world says hi!

stepdefinition.java

// TODO: Is there a way to get all the tags in a feature file?
    @Before("@alive")
    public void beforeScenario()
    {
        // print a list of tags in this .feature file
    }
 

Eric Kessler

unread,
Apr 9, 2015, 1:05:03 PM4/9/15
to cu...@googlegroups.com
I would like to think that you would have access to some kind of scenario object in your 'before' hook and that from that object you could get access to the feature object and from there get access to all of the other scenario/outline objects, thus finally allowing you to get access to their individual tags. However, I'm only familiar with Ruby's implementation of Cucumber and maybe the Java implementation doesn't expose as much as Ruby does at runtime. That being said, I bet that I could code up something that will accomplish what you want but it likely wouldn't be as efficient as an inspection of the runtime objects.

Aslak Hellesøy

unread,
Apr 9, 2015, 1:08:49 PM4/9/15
to cu...@googlegroups.com

On Thursday, 9 April 2015 at 16:16, Simon P. wrote:

Does anyone know if there is a way to get all the tags from a .feature file and use them in my .java step definition file?

What dou you need it for? 
I saw there are some gherkin.formatter.model.Tag and gerkin.formatter.model.Scenario libs that exist and maybe .getTags() can be used, but how do I reference the specific Scenario I want?

If that's not possible, do I need to convert my Gherkin file into json first?

I've searched and haven't found anything about this.

example:
@HelloWorld @alive
 Scenario: Say hello
 When I say hello!
 Given the world is around
 Then the world says hi!

stepdefinition.java

// TODO: Is there a way to get all the tags in a feature file?
    @Before("@alive")
    public void beforeScenario()
    {
        // print a list of tags in this .feature file
    }
 

--
Posting rules: http://cukes.info/posting-rules.html
---
You received this message because you are subscribed to the Google Groups "Cukes" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cukes+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Simon P.

unread,
Apr 9, 2015, 1:28:48 PM4/9/15
to cu...@googlegroups.com
Hey,
Thanks so much for the replies.

I want to tag each of the test case ids in the Gherkin .feature file.

In the .java step definition file, I want to pass in all the tags that exist in my .feature file. Instead of having to include each of them separately, otherwise I run the risk of having to make sure to update the tags in the .java file anytime I update the .feature file.

I then want to print out all the tags (test case ids) and their requirement descriptions to the test report.

example:
@HelloWorld @alive @happy @sad
 Scenario: Say hello
 When I say hello!
 Given the world is around
 Then the world says hi!

stepdefinition.java

// TODO: Is there a way to get all the tags in a feature file?
    @Before(<a list of tags>)
    public void beforeScenario()
    {
        // print a list of tags in this .feature file 
    }
 
I found some info below but I haven't found an example of how those tags are used in the step definition file.

Tags are also a great way to “link” your Cucumber features to other documents. For example, if you have to deal with old school requirements in a different system (Word, Excel, a wiki) you can refer to numbers:

@BJ-x98.77 @BJ-z12.33
Feature: Convert transaction

Eric Kessler

unread,
Apr 9, 2015, 1:54:55 PM4/9/15
to cu...@googlegroups.com
"I want to tag each of the test case ids in the Gherkin .feature file."

Considering that you just said almost every keyword in a use case that I am very familiar with, either I am misinterpreting what your end goal is or I have written a library that handles your use case exactly. ;)


So you wish to add tags to a bunch of existing gherkin tests in your codebase?

Simon P.

unread,
Apr 10, 2015, 3:14:44 AM4/10/15
to cu...@googlegroups.com
Yeah that's right, each of the 'requirementIds' would be a tag, and that way I'd know what requirements are needed for that test case.

Do you a way to get a Scenario object so I can get all the tags for a scenario?

I saw this page as well.


which has an example of using feature.tags, but I'm not sure how to get that feature object.
 if 'browser' in feature.tags:

Björn Rasmusson

unread,
Apr 10, 2015, 6:55:27 AM4/10/15
to cu...@googlegroups.com
Simon P. wrote:
Yeah that's right, each of the 'requirementIds' would be a tag, and that way I'd know what requirements are needed for that test case.

Do you a way to get a Scenario object so I can get all the tags for a scenario?

You can get the tags that has been applied to a scenario from a before hook in Cucumber-JVM this way:

public void before(cucumber.api.Scenario scenario){
       
for(String tag : scenario.getSourceTagNames()){
           
System.out.print("Tag: " + tag);
       
}
   
}

Best regards
Björn

Simon P.

unread,
Apr 10, 2015, 7:11:27 AM4/10/15
to cu...@googlegroups.com
Thanks, I figured out that I could use cucumber.api.Scenario this morning and it's working great!
Reply all
Reply to author
Forward
0 new messages