How to run specific scenario in cucumber out of multiple scenario?
Feature file
Feature: Test Test Smoke scenario
Scenario: Test login with valid credentials
Given open firefox and start application
jhbhhjhj When I click on Login And enter valid "[email protected]" and valid "admin@123" Then Click on login and User should be able to login successfully
Scenario: Test shop for cart
Given Click on shop for carts
And select plates
When Click on Add to cart
Then product should be added in the cart successfully
And verify the product
Scenario: Test login with valid credentials1
Given open firefox and start application
When I click on Login
And enter valid "[email protected]" and valid "admin@123"
Then Click on login and User should be able to login successfully
Scenario: Test shop for cart1
Given Click on shop for carts
And select plates
When Click on Add to cart
Then product should be added in the cart successfully
And verify the product
Test Runner
package runner;
import org.junit.runner.RunWith;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@Cucumber.Options(features="features",glue={"steps"},format = {"pretty", "html:target/Destination"})
public class TestRunnr {
}
Right click the scenario line in your feature file, there will be a Run 'Scenario: My scenario' option.
Use tags future in the cucumber like below.
Feature: Test Milacron Smoke scenario
@Test1
Scenario: Test login with valid credentials
Given open firefox and start application
When I click on Login
And enter valid "[email protected]" and valid "Thought@123"
Then Click on login and User should be able to login successfully
@Test2
Scenario: Test shop for cart
Given Click on shop for carts
And select plates
When Click on Add to cart
Then product should be added in the cart successfully
And verify the product
@Test3
Scenario: Test login with valid credentials1
Given open firefox and start application
When I click on Login
And enter valid "[email protected]" and valid "Thought@123"
Then Click on login and User should be able to login successfully
@Test4
Scenario: Test shop for cart1
Given Click on shop for carts
And select plates
When Click on Add to cart
Then product should be added in the cart successfully
And verify the product
If you want to run only Test1 scenario update runner file like below.
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(features="features",glue={"steps"},format = {"pretty", "html:target/Destination"},tags={"@Test1"})
public class TestRunner {
}
If you want to execute multiple scenarios keep comma sepearated tags as mentioned below.
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(features="features",glue={"steps"},format = {"pretty", "html:target/Destination"},tags={"@Test1,@Test2"})
public class TestRunner {
}
Like the other answers suggest, use tags. If you use maven, you don't have to change the runner file - just add this to your maven call
-Dcucumber.options="--tags @Test1"
The thing I like about this method is that I don't risk committing the tags in the runner file.
Also, here is an example on running multiple tags in maven.
You can either use selective feature file or selective scenarios in the feature using tags. Please try with this solution.
Lets consider the you have n number of feature files and you need to run only selective feature from that. Then name each feature file with @tag name.
eg.: Under this folder, if you are having n number of features - "src/main/resources/publish"
Login.feature
//Inside the file start with feature tag name
@Login
Feature: To Login to Email
//Then feature name followed with scenario tag name
@User1
#Scenario1:
Scenario Outline: Navigate and logon to gmail application
Given User launches gmail application
When User updates emailID <emailID>
And User updates pwd <pwd>
Then User clicks on Login Button
Examples:
| emailID | pwd |
| [email protected]| 123 |
@User2
#Scenario2:
Scenario Outline: Navigate and logon to facebook application
//Write the code for scenario 2 as similar to above
CreateEmail.feature
@Createmail
Feature: Create email
Scenario: Blah blah blah...
//Write all Given when And Then
SendEmail.feature
@Sendemail
Feature: Send email
Scenario: Blah blah blah...
//Write all Given when And Then
So From the above Test files. Lets consider you want to test 1st and 3rd feature alone, Then you can use code as below:
eg.:
//This is to run specific feature files, which is 1 and 3. Likewise you can use the tags for scenario as well if you have n number scenario in same feature file.
@CucumberOptions(features= "src/main/resources/publish", tags="@Login, @Sendemail", format = {"pretty"} )
// This is to run specific scenario in the feature file. If you have multiple scenario, then you can write your specify your scenario tags followed by comma.
@CucumberOptions(features= "src/main/resources/publish/Login.feature", tags="@User2", format = {"pretty"} )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With