Tagging Basics

Cucumber provides a simple method to organize features and scenarios by user determined classifications. This is implemented using the convention that any space delimited string found in a feature file that is prefaced with the commercial at (@) symbol is considered a tag. Any string may be used as a tag and any scenario or entire feature can have multiple tags associated with it. Be aware that tags are heritable within Feature files.

  • Scenarios inherit tags from the Feature statement
  • Examples inherit tags from the Feature and Scenario statements

Utilizing Tagging

If multiple databases or environments are expected to be exercised with Cucumber tests, tagging is an excellent way to make reuse of test code. It’s easiest and simplest to design each feature file (think test suite) with at least one specific data-environment in mind. If multiple data-environments share common functionality, design the tests to run over as many as possible. The purpose is to indicate valid tests for each of the desired sets of data or environments. Each feature file should then be tagged with the intended data-environment. Including these tags utilizes Cucumber’s built in feature for narrowing down tests, and if the Java code can also be setup to switch data-environments based on the tag. If scenarios in the feature file are specific to different data-environments, then it should be considered to break them into different features. If not broken into features, then include tagging as the scenario level, instead of the feature level.

In addition to indicating tags for data-environments, tags should also be utilized for testing types. Scenario Outlines contain data tables to run over, and it’s an effective use of these tables to divide the tests run into Smoke and Regression tests. Create smaller subsets of the data for a Smoke test, and tag the larger tables as Regression. Additional testing types can also be rolling into this structure, with little effect on the tests. Consider adding Acceptance or Integration tests.
Now let’s take a look at our previous cucumber tests, and roll in some useful tagging. In our below example, we have two different environments to test over: our CosmicComix.org website, and our CosmicComix.net website, each with slightly different functionality. Our login test applies to both sites, and so we apply tags for both at the Feature level. Additionally, we want to indicate which of our test run for the full regression, and which is appropriate for a smoke test.

	@CCOrg @CCNet
	Feature: Testing for login page

	Scenario Outline: Bad login

		Given I want to use the browser Firefox
		When I set the username to [username]
		When I set the password to [password]
		When I login to CosmicComix
		Then I see the error message "[message]"
		And I am on the login page

		@Regression
		Examples:
		|	username		|	password		|		message		|
		|	testuser1 		|				|	Please provide a password.	|
		|				|	testuser1		|	Please provide a username.	|
		|	testuser		|	testuser		|	That username does not match anything in our records.		|
		|	testuser1		|	testuser2		|	The password provided does not match the username entered.	|

	Scenario Outline: Successful login

		Given I want to use the browser [browser]
		When I set the username to testuser1
		And I set the password to testuser1
		When I login to CosmicComix
		Then I am on the launcher page

		@Regression
		Examples:
				|	    browser	 |
				|	    Firefox	 |
				|	    Chrome       |
				|     InternetExplorer   |
		@Smoke
		Examples:
				|	browser		|
				|	Firefox		|

In the above example, if we ran providing the tag @Smoke we would only run the Successful login test for the Firefox browser. If instead we ran providing the tag @Regression we would instead run all of the Bad login tests, and the Successful login test over all three browsers. Providing no tag would run all tests, both Smoke and Regression. Alternatively a tilde (~) could be added before the desired tag to negate the tag. E.g. if we ran with the tag ~@Regression all tests NOT tagged as Regression would be executed.

Java Code Tie In

As we discussed previously, we can run our Cucumber tests with a multitude of runners. If we use something like Ant as our JUnit runner, we can simply pass in our tags, and then further access them from the java code. We just need to add a few arguments under our java run target. For example:

<arg value="--tags"/>
	<arg value="${dataenvironment}"/>
	<arg value="--tags"/>
	<arg value="${testtype}"/>
	<sysproperty key="dataenvironment" value="${dataenvironment}"/>

Passing the dataenvironment as a tag allows Cucumber to recognize it as a tag to be run against. Additionally you’ll notice the –tags value in their twice. That is to ensure we get the proper logic selected. This means we’ll run anything tagged as dataenvironment AND testtype. If we had placed them in the same tag, we’d be running anything tagged as dataenvironment OR testtype. Finally, by adding the value as a sysproperty, our Java code can access the value, and make the desired changes in the code based on the database being run against. To access this value, setup your JUnit framework to read in this value before any of the tests execute as below.

	@Before	//performed before anything is done
	public void setup() throws SAXException, IOException {
		if( System.getProperty( "dataenvironment") != null ) {
			database = Databases.valueOf( System.getProperty( "dataenvironment" ).substring(1) );
		}
	}

And then later determine how to connect to the specific data-environment within the database code set.
Finally, to properly execute this code, you just kick off Ant with the below command.

	ant -Dtesttype=@Smoke -Ddataenvironment=@CCOrg

Want to learn more? You can read all of the tagging specifics on github

71 thoughts to “How to Get the Most Out of Cucumber Tags

  • suneel

    Hi, Is it possible to use selenium grid with the cucumber framework ?

    Reply
    • Max Saperstone

      Absolutely. Selenium Grid has fantastic support out there.
      A 3 sentence assist on running Selenium Grid with Webdriver:

          Write your Selenium Tests as usual, but ensure they are thread safe.
          Modify how you define your remote webdriver to point to grid
          Launch A Selenium Grid 2 hub, and a node

      Then just run your tests as normal. The best documentation on this is the official one

      Reply
  • Sunil

    Hi Max,
    Is it possible to pass tags along with system environment variable

    Eg:
    In cucumber runner can i define tags @SuiteA with Host name of that machine

    If yes please give me some sample examples.

    Reply
  • Max Saperstone

    Sunil, look at the example from under the Java Code Tie In section. It shows exactly what you are asking

    Reply
  • safe work method statement template

    It’s a pity you don’t have a donate button! I’d without a doubt donate to this outstanding blog!
    I guess for now i’ll settle for book-marking and adding your RSS feed to
    my Google account. I look forward to fresh updates and will share this blog with my Facebook group.
    Talk soon!

    Reply
  • New Comer

    Hello Max,

    I have two issues. . but for now looking solution for the first issue

    Is it possible to run multiple scenarios with different user defined tags as per required order in same feature file?

    For Example: I have a feature file “Test”

    Feature: Test

    @NeedsToBeRunFirst
    Scenario: Scenario One

    @NeedsToBeRunThird
    Scenario: Scenario Two

    @NeedsToBeRunSecond
    Scenario: Scenario Three

    @ so on , , ,

    Now I want to run the scenario in an order as the tag given to each scenario

    Thanks,
    In Advance

    Reply
  • Max Saperstone

    For running Cucumber tests in a specific order, it’s important to keep in mind that by default, Cucumber features/scenarios are run in a very specific order. First each directory of feature files is run alphabetically, then each feature file alphabetically from the directory, and then each scenario in order it is listed.
    Cucumber does not support modifying this order, and I haven’t seen any good hacks for it either. If you want to specify the features specifically, then they will run in that order. If you know that your tests will always run in the order that you listed above, simply re-order your tests in the feature file.
    I’ve read comments from Aslak Hellesøy (creator of Cucumber) stating that relying on specific ordering of testing is discouraged and will never be supported. This is a very understandable statement, as good tests should be independent, and setup their own data, and remove their own data when completed.
    If ordering your tests within your feature files doesn’t do the trick, then you might want to consider trimming down your tests, and rethinking their implementation

    Reply
  • test

    hey how can a apply for loop in cucumber??

    Reply
    • Max Saperstone

      You’ll have to be more specific as to how/where you want to apply the loop.
      Gherkin does not support looping, but if you’re looking to re-run the same steps with different data, consider using a Scenario Outline, different data sets that will loop through the same steps.

      Reply
  • test

    THNX

    Reply
  • Jon Malachowski

    You can call step definitions from inside step definitions. So, in code you can create your own loop around another step definition and pass in a variable to a capture group like you would normally.

    Reply
  • Max Saperstone

    Yes, thanks for clarifying Jon. Within java code, there is no problem with self reference, but you want to be careful of self reference within the Gherkin code itself

    Reply
  • RPD

    Sir, I need to fill a value in textbox,then click on search and then click on the link, to see the details of the value filled in textbox. I need to check for around 100 values.

    2) Once i click on the link a new page will be displayed and i need to verify all the labels in the page. I use excel for mapping the labels witth the application

    please guide me how to proceed

    Reply
    • Max Saperstone

      I’m not certain for where you are looking for guidance.
      Are you trying to get tooling nailed down for interacting with your application, or how to integrate steps such as these into the Gherkin language?

      Reply
  • Koteswara

    Hi max ,

    As far i know the difference between Scenario Template and Scenario Outline both are same ,
    can you please clarify me?

    Second thing, Tags and prioritizing tests so far it was not working effectivily in Specflow… do i need to add any library files in the stepdefination??

    Ex :- @ignore– it never ignore the test
    ~ignore it also the same result like above
    @High @Medium @Low all are not working as per priority, will you please clarify me …

    Reply
  • Meena

    Is there a way to configure in an xml file what tags to run and what tags not to?

    I have a feature file with 50 scenarios, but would need to segregate those into a small, medium and large suites. Like testng.xml file, whatever class/method is configured to run, is there a way in cucumber ?

    Reply
    • Max Saperstone

      Yes, if you look at the example provided, you can see I’m passing a value into the Ant build file, but it could also be hard coded like the below

      <arg value="--tags"/>
      <arg value="mytag"/>

      What build tool are you using? Maven? Ant? Gradle? You can set the tags in any of these

      Reply
  • Gopinath

    HI Max, I need to get the tags at run time which we are planning to give as input to run our regression suite.We are using JUnit Runner and cucumber options to mention the features path and definition.
    Any idea on how to inject the tags at run time. I am attaching the sample test runner.java

    import org.junit.runner.RunWith;
    import cucumber.api.junit.Cucumber;
    import cucumber.api.CucumberOptions;

    @RunWith(Cucumber.class)
    @CucumberOptions
    (
    features={“src/test/resources/Features”},glue = “com.xfinity.stepDefinition”,
    //tags = {“”},
    format={ “pretty”,”html:target/site/cucumber-pretty”,”json:target/cucumber.json”}
    )

    public class RunTest {

    }

    Reply
    • Max Saperstone

      When you say get the tags at run time, do you mean dynamically determine what tags exist, or just indicate which tags to execute on at run time?
      If the former, I’d wonder why, as it seems you may be over complicating the issue. If the latter, you can just pass in the tags as system parameters. I have provided an example using ant in the post, but depending on how you kick off the tests, that would vary. How are you launching your tests?

      Reply
  • Gopinath

    Hi Max,
    I need to just indicate the tags to execute at runtime.I am launching my test using JUnit by using Cucumber options.I need to get and set the tags (using variable) before launching the test.how and which method can be used for that.Adding to that, I am running via Maven command to execute my test from command line.If you have the answer,please quote with example.
    Sorry for the late reply

    Reply
    • Max Saperstone

      Hi Gopinath,
      If you’re running from maven, then all you need to do is pass in the cucumber tags as maven command line variables.
      It would look something like this:
      mvn test -Dcucumber.options="--tags @runThisTag --tags ~@ignoreThisTag"
      You could even set some default tags in your maven pom file, which could be overridden by these variables, but by leaving that blank, by default all of your tests would run.
      If you wanted to set additional options from the command line, you could do that as well. These can set/override anything declared in your @CucumberOptions annotation
      mvn install -Dcucumber.options="--tags @runThisTag --tags ~@ignoreThisTag --format json-pretty:target/cucumber-report.json --format html:target/cucumber-html-report"

      Reply
  • anand

    Hi Max,

    I have totally different requirement coming to me. I need to pass the tags name from xml file but not from maven or ant built file. Basically i dont need to compile the code to change the tags name

    Reply
    • Max Saperstone

      So, maven should be used for more than just compiling, it can execute the tests as well. How do you currently kick off your tests? That method will depending greatly on how you can/should pass in the tags you want to run. Additionally, what framework are you running cucumber in? TestNG? JUnit? RSpec?

      Reply
  • Anand

    Hi Max,
    I kickoff my test from TestNG file using TESTNG framework. I have my runner class like below: sorry for bad formatting:

    @RunWith(Cucumber.class)
    @CucumberOptions(
    features={“src/main/resources”},glue = “feature”
    )
    public class RunnerClass {
    private TestNGCucumberRunner testNGCucumberRunner=null;
    @BeforeClass(alwaysRun = true)
    public void setUpClass() throws Exception {
    testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());
    }
    @Test(groups=”cucumber”, description=”Runs CucumberFeature”,dataProvider = “features”)
    public void feature(CucumberFeatureWrapper cucumberFeature) {
    testNGCucumberRunner.runCucumber(cucumberFeature.getCucumberFeature())
    }

    Reply
    • Max Saperstone

      Gotcha, but how are you actually getting the tests to run? Are you running them within eclipse? If so, then changing the tags shouldn’t be a problem.
      Are you running them from the command line? If so, what are the commands you are using? You could easily be using maven or ant to run the tests, in which case it’s simple to pass tags. Even if you are running the java compile and execute commands directly, it is simple enough to provide tags.

      Reply
  • Anand

    I have developed a Web Application where user selects the Features to run
    ( each feature has one one tag ). All the Step definitions are completely implemented. So i just need to execute one runner file when user clicks on Execute button in UI without Changing the Runner file.
    The code that executes the testNG file is below: Please help me how can i pass command line arg to it and what are the command line args

    TestNG mysuite = new TestNG();
    List suite = Lists.newArrayList();
    suite.add(“testng.xml”);
    mysuite .setTestSuites(suite);
    mysuite .setOutputDirectory(“C:\\testOutput”);
    mysuite .run();

    Reply
    • Max Saperstone

      I’d have to know more about your setup, unfortunately, this sounds more complicated than something with a quick answer. You can always get commandline args from the System.getProperty() method in Java. Hopefully, by using that, you can set your params, and read them in, and set them to your suite.setGroups params.
      A simpler solution would be to have one runner, kicked off from maven or ant, where you provide the group directly, instead of calling code to call code.

      Reply
  • Anand

    I need something like in the below thread:

    https://stackoverflow.com/questions/40348984/cucumber-junit-vs-cucumber-testng/45932201#45932201

    Reply
    • Max Saperstone

      I don’t think I follow your question. Are you just looking for a good comparison between junit and testng? Cucumber sitting on top of those tools shouldn’t make any difference. A lot of it is preference, but Guru99 has a good summary of using one vs the other. I hope this helps.

      Reply
  • Pingback: Behavior Driven Development ( BDD ) , Cucumber – VikramVI Knowledge Sharing

  • Sanket

    Hi Max,

    Let me explain my setup first. I have created 4 packs for my automation runs. Each pack individually contains around 20 feature files and each feature file in turn has multiple scenarios. I execute these packs each Friday to run over the weekend and get logs Monday morning. I then wanted to broaden the use of the pack and thought about re-running the failed scenarios from each feature file. To accomplish this I used the tags “-f pretty -f rerun –out .txt appended to each feature file name. So it would look something like this:
    ruby -S bundle exec cucumber -p default –require features features/regression_pack/part_3/<script name.feature -f pretty -f rerun –out .txt
    and so on for each of those 20 feature files in the pack.
    But what it does is, it executes the failed scenarios form one feature file right after it has completed execution the first time. I need it to execute the failed scenarios after all the feature files have been executed and then execute the failed scenarios.
    Can you help me out with some suggestions on how this could be achieved? Please let me know if you need any more information.

    Reply
    • Max Saperstone

      That sounds like a very interesting issue. I’m not familiar enough with the cucumber ruby runner to be able to give you a good answer, as I’ve spent most of my time on the java side (cucumber-jvm).
      This does bring up an interesting question to my mind. Why do you want to re-run failing tests? I would it is because the expectation is they would pass the second time. My follow-up question to answer I just gave myself is this: why are your tests failing sometimes and passing sometimes. Inconsistently reporting tests is the bane of testing, and if they pass sometimes and fail others, how can they ever be trusted?
      If it were up to me, I would instead focus my efforts on getting the tests to run consistently each and every time, instead of getting the re-run to work.
      Good luck!

      Reply
      • Sanket

        Well, you’ve got that spot on! The tests are inconsistently failing or passing which puts me in a spot when I try to report the failures to the higher-ups.
        I would try and put in the suggestion you’ve made and look at more robust executions.

        Cheers Mate

        Reply
  • Thirugnanam

    Hi Max,

    Is it possible to implement ‘AND’ function for tags such as only the scenarios that have both tags mentioned should be taken into account?

    Reply
    • Max Saperstone

      Absolutely, it just depends on how you want to implement it. As I mentioned in my post, if you want to AND your tags, they need to be provided via ant as duplicate inputs, similarly if you use maven or groovy as your runner. That means providing these as hardcoded elements in your build file (build or pom) is relatively simple, but makes it more difficult to do from the commandline.
      I would suggest instead of providing your tags as directly indicated in the post, setup an options input field which will grab parameters, and then explicitly provide the tag parameters you want. It’s not as clean, but will accomplish what you want.
      Instead of the snippet I provided above, instead, include the following for your build.xml file:

      <arg value="${options}"/>

      Then, instead of setting your testtype in the commandline parameters, instead set your desired cucumber options, which in this case would be your tags.
      For OR, provide the below

      -Doptions="--tags @tag1,@tag2"
      

      For AND, provide the below

      -Doptions="--tags @tag1 --tags @tag2"
      

      I hope this helps

      Reply
  • anisha

    Hi Max,

    is it possible from maven as well to configure the input data as per the region.

    I would like to configure similar way of input data based on the region from my feature file, and i am sending the property value either Regression or Smoke from jenkins maven goal.

    But the input data is not getting loaded as per the values provided from maven goal.

    Please help me with configuration changes i need to do for reading the input data from features based on the maven goal provided.
    Maven goal :
    clean install -Dtesttype=@Regression

    Scenario Outline: Successful login

    Given I want to use the browser [browser]
    When I set the username to testuser1
    And I set the password to testuser1
    When I login to CosmicComix
    Then I am on the launcher page

    @Regression
    Examples:
    | browser |
    | Firefox |
    | Chrome |
    | InternetExplorer |

    @Smoke
    Examples:
    | browser |
    | Firefox |

    Thanks,
    Anisha

    Reply
    • Max Saperstone

      How do you have your maven pom setup? Included in these instructions are how to setup your ant file to pass in the needed tags, and you need to do the same thing for your pom. Are you executing your tests via surefire or failsafe? Are you including any other plugins for your test execution?
      This link provides a good example of setting up tags within maven

      Reply
  • anisha

    Hi Max,

    Thank for the information provided.

    I am using sure fire and Cucumber jvm parallel plugin to run cucumber features.

    I am providing the input data in the feature file for the different regions, and i would like to read the corresponding input data as per the region mentioned from the Jenkins maven goal

    Maven goal : clean install -Dregion=@test

    Input data in the Feature file :

    @dev
    Examples:
    | browser |
    | Firefox |
    | Chrome |
    | InternetExplorer |

    @test
    Examples:
    | browser |
    | Firefox |

    But its reading the first input data(i.e dev input data) irrespective of the option/region i provided from the jenkins.

    Max, please help what the configuration changes i have missed to configure for the cucumber to read the input data as per the region from the jenkins maven goal

    Note: I am using different tag name for the all features to run which i am giving it in the tags for cucumber jvm parallel plugin configuration in the pom.xml

    Thanks,
    Anisha

    Reply
    • Max Saperstone

      What does your pom look like. It appears the tags might not be passed in properly

      Reply
  • anisha

    Max, I think tags got removed from my previous chat i am adding again here.

    ======================

    com.github.temyers
    cucumber-jvm-parallel-plugin
    1.3.0

    generateRunners
    validate

    generateRunners

    xxx.xxx.stepdef
    ${project.build.directory}/generated-test-sources/cucumber

    src/test/resources/features
    target/cucumber
    json
    true
    $(region)
    “@Complete”
    true
    true
    simple
    Parallel{c}IT

    =========================

    *****

    Reply
    • Max Saperstone

      region should be in {} not in ()

      Reply
  • anisha

    i am not able to share the plugin info here max, its getting removed by browser looks.

    Reply
  • anisha

    Hi Max,

    Please share me if you have any sample pom.xml configuration to get the required functionality work.
    I will incorporate the changes in case not done already.

    Thanks,
    Anisha

    Reply
  • Pinakin

    Hi,

    I have assigned tags to features like @ScRejectA,@ScRejectB,@ScRejectC and @ScRejectD. But the features are getting executed in the order of @ScRejectA(Feature file StopChequeRequestRejection),@ScRejectB(Feature file StopChequeRequestCheckerRejection),@ScRejectD(Feature file EditRejectedScChecker) and @ScRejectC(Feature file EditRejectedScMaker). Each feature file contains one scenario outline only with Examples tag. Please assist me. I need to run in order of @ScRejectA,@ScRejectB,@ScRejectC,@ScRejectD

    Reply
    • Max Saperstone

      Unfortunately, Cucumber doesn’t provide the capability to run tests in the order that the tags are specified in. Best practice is to make your test atomic, not-dependent on each other.
      If you need to set dependencies, look at doing that within your defined Cucumber hooks

      Reply
  • Sofia

    Hello, I’m trying to do the same thing. How did you end up working ?

    Reply
  • Suraj

    Hello Max,

    Thanks for information provided,
    i have multiple features file and each files contains different name(@name1 is for name1.feature and@name2 is for name2.feature ), i need to run the @name1 first and after @name2, Please can you provide me solution

    Note: I dont have single featue file its has multiple

    Reply
    • Max Saperstone

      As I mentioned in a previous post, Cucumber doesn’t support running tests in a specific order. Having tests depend on others is bad practice

      Reply
  • Jorge

    Hi Max,

    Thanks for the great article.

    I am running Cucumber using JUnit using the below:
    @RunWith(Cucumber.class)
    @CucumberOptions(
    plugin = {“usage”, “json:target/cucumber.json”, “html:target/cucumber”, “com.cucumber.listener.ExtentCucumberFormatter:”},
    glue = {“BDD.StepDefinitions”},
    features = {“UnitTest\\BDD\\Features”}
    )

    I use AntBuild to kick off my tests through Jenkins and would like to pass the tags dynamically as parameters through Jenkins itself.

    My AntBuild is setup as follows to run the test. I have tried including the arguments as you have mentioned on your article here but get the error, “junit doesn’t support the nested “arg” element”

    Any advice would be appreciated. I am not using Maven at all.

    Reply
    • Max Saperstone

      What does your ant build file look like? The JUnit block does support the arg element, if you structure it properly

      Reply
  • Pingback: Using Dependency Injectors to Simplify Your Code in Cucumber

  • Monali

    Hi Max,

    We are using Cucumber framework for our Android Application UI testing.
    We have written 10 feature files for 10 index each and 1 common step definition Java file.
    Index is changing at run time on android device and we wanted to select specific feature file accordingly.
    For e.g. If Index is 3, then only Index_3 feature file should be run and all other feature file should be skipped without throwing error.
    Is it possible in Cucumber?

    Reply
    • Max Saperstone

      Sure thing, I’d suggest setting it up using tags.
      Add the index at the top of each feature, and then pass in your index as a tag

      Reply
  • Monali

    Hi Max,

    We are using Cucumber framework for our Android Application UI testing.
    Currently we are facing below issue:

    In our gradle preject, under androidTest package 2 runner class are used
    In one, AndroidJUnit4 runner is used
    @RunWith(AndroidJUnit4.class)
    public class ClassA {

    }
    In 2nd one, Customized cucumber runner is used
    @CucumberOptions(features = “features”, glue = “android.runner”)
    public class Instrumentation extends MonitoringInstrumentation {
    ..
    }
    In build.gradle, we have added below code part.

    if(project.hasProperty(‘Espresso’))
    {
    testInstrumentationRunner “android.support.test.runner.AndroidJUnitRunner”
    }
    else {
    testApplicationId “android.runner”
    testInstrumentationRunner “android.runner.Instrumentation”
    }
    So to run Espresso test we are using “./gradlew connectedandroidTest -PEspresso” command and to run cucumber test “./gradlew connectedandroidTest” command is used.
    At a time only one runner can be use.
    We don’t want to give this explicit command.
    So is it possible to run both the test runner sequentially?

    Reply
    • Max Saperstone

      I think this is a simple matter of realigning your gradle file.
      You should create two separate tasks one for each runner, and then you can have on task which runs one, and then the next.
      That latter task can then be the one your call from the commandline

      Reply
  • Monali

    Ok.. Thanks Max..
    But where we can check the Index value which is updated at run time?

    Reply
    • Max Saperstone

      I’m honestly not certain what index value you are referring to. Can you provide a concrete example?

      Reply
  • Monali

    Can you please provide any example from sequential run of multiple runner?

    Reply
    • Max Saperstone

      task taskOne(type: Test) {
      //do some things
      }
      task taskTwo(type: Test) {
      //do some things
      }
      task Test(type: Test) {
      taskOne
      taskTwo
      }

      Reply
  • Monali

    I would briefly try to explain..
    We read index value from device.
    Based on the index value, all the scenarios in the corresponding feature file should run.
    Now let’s take an example…
    Index value is 3; feature files 1 & 2 should be check and skip.
    So this skipping part I am not able to implement..
    Could you please guide me?

    Reply
    • Max Saperstone

      How do you retrieve the index value?
      Swing over to Techwell Hub, and ping me, maybe we can talk through this some
      @max on https://hub.techwell.com/

      Reply
  • Anto Kiran

    Hi Max
    I want to do the following:
    I have the performance tests under one feature file. This feature has a tag name called “@performance”.

    I have an implementation to throttle the web speed but I want to to apply throttling only when the tag is “@performance”.

    I am looking for a good resource to guide me as to how to retrieve the tag name of a feature file.

    If you could help me find this, it will be great!!

    Thanks in advance:-)

    Reply
    • Max Saperstone

      Ignoring that fact that performance tests probably shouldn’t be done using Cucumber, you can set @Before annotations for specific tags. You could set one for performance like so
      @Before('@performance')
      and then run your implementation to throttle the web speed.
      You can read more about that here: https://www.coveros.com/background-and-hooks-for-cucumber-jvm/

      Reply
  • Aimee

    Hi Max,
    Thanks for this in depth example, it’s really helpful!

    I have a slightly different scenario where I have a single tagged with @mock and with @system.
    The reasoning behind this is to have different @Before steps run depending on the tag being used.

    @Before(value=”@mock”, order=1)
    fun initialise_mocked_tests(scenario: Scenario) {
    //setup wiremock server here
    //setup variables here
    }

    @Before(value=”@system”, order=1)
    fun initialise_system_tests(scenario: Scenario) {
    //setup variables here
    }

    So when I run with the @mock tag, it will run against a mock server and if I run with @system, it will run against the real thing.

    When I do this, both @Before steps get run, which causes a bit of a mess in my tests. Is there a way to achieve what I’m looking for using tags?

    Thanks,
    Aimee

    Reply
    • Max Saperstone

      Hey Aimee,
      Due to how it seems like you setup your file, I don’t think tags are the solution for your problem. It sounds like you have tagged your tests with both @mock and @system, and so yes, both before methods would run, because they both apply to the test. What you could/should do instead is pass in a separate variable for the system you are testing on. Then have one before method check what that variable is set to, and do the correct setup. System variables might be good for this, or maybe create a properties file. Maybe you want both, with a default, but an easy way to override from the cmd if needed.
      Good luck!

      Reply
  • Praneeth

    Hi Max,
    If I want to pass the tags from excel sheet like if I have 10 scenarios with 10 different tags,
    I have to mark 5 of them as Execure = Yes in excel then only those five tags should be taken in to cucumber options as tags. Is this possible with Cucumber. Can I add these tags in runtime from excel?

    Reply
    • Max Saperstone

      What exactly are you trying to accomplish Praneeth? It’s bad practice to change tags on the fly, you want to change what tags you’ve selected to run on the fly.
      You’ll want to figure out how to tag your tests to be able to properly select what you want to run

      Reply
  • Nirmal

    package com.reportdesign.web.runner.sprintrun;

    import org.testng.annotations.DataProvider;

    import cucumber.api.CucumberOptions;
    import cucumber.api.testng.AbstractTestNGCucumberTests;

    @CucumberOptions( features = “src/test/resources/com/reportdesign”,
    glue = “com.reportdesign.web”,
    tags= “@SanityTest”,
    plugin = {
    “html:target/cucumber-html-report”,
    “json:target/cucumber.json”,
    “pretty:target/cucumber-pretty.txt”,
    “usage:target/cucumber-usage.json”,
    “junit:target/cucumber-results.xml”,
    “com.altimetrik.qe.slingshot.listener.ConfigurationCucumberListener”,
    “com.altimetrik.qe.slingshot.listener.CucumberLogger”,
    “com.altimetrik.qe.slingshot.reporter.SlingshotCucumberReporter”,
    “rerun:target/rerun.txt”
    },
    monochrome = true
    )

    public class SprintTestRun extends AbstractTestNGCucumberTests{

    }

    HI Max,
    above is my runner file, what changes do I need to make to get tag value from environment variable instead of runner file.

    Reply
  • rajesh saini

    Hi Max,

    Thanks for the deep dive.

    I am facing an weird error wherein my below scenario works fine when run from local machine but not working when tried from jenkins.

    Example-
    Feature: Login Test
    Scenario Outline: Test Login
    WHEN User entered and
    @Staging
    Examples:
    | uname | pwd |
    | s_uname | s_pwd |
    @Prod
    Examples:
    | uname | pwd |
    | p_uname | p_pwd |

    So as can be seen, i have put different tags on top the Examples. Using this i want to run the same step with different test data as per the environment.
    This is working fine on local. But when trying from Jenkins, I can only run using the tag of the First Examples. When tried using the 2nd Examples tag, it somehow not recognising any such tag, it seems. Request you to help me, as if this not allowed only in Jenkins or am i missing something here

    Jenkins error when tried using the 2nd Tag(@Prod):

    0 Scenarios
    0 Steps
    Om0. 322s
    [ERROR] Tests run:
    Failures: 0, Errors:
    1.
    [ERROR] Feature: tat
    java.util.NoSuchElementException: No value present
    Skipped:
    0,
    Time elapsed:
    4.47 s
    <<< FAILURE!
    – in to s
    Time elapsed: 4.469 s
    <<< ERROR!
    [INFO]
    [INFO1 Results:
    TINFOI
    [ERROR1 Errors:
    [ERROR ]
    No value present
    [INFO1
    [ERROR] Tests run: 1,
    Failures: 0, Errors: 1,
    Skipped: 0
    [INFO]
    [INFO1
    [INFO1 BUILD FAILURE
    [INFO]
    [INFO1 Total time:
    12.314
    [INFO] Finished at:
    2022-09-29T12:29:08Z

    Reply
  • Srujan

    Hi. I want to implement this in cucumber feature file where
    @Given I pass “Multiple arguments, the number of arguments being variable”

    But my step definition file should not be changed if I change the feature file from one to two arguments, or two to three arguments, or three to four. Can this be done in any way?

    Reply

Leave a comment

Your email address will not be published. Required fields are marked *

X