I have implemented Testng and Cucumber for my selenium Project. I am running my Test Cases by running the Testng.xml where I have redirected them to Testng Runner File. The only difference in each Runner file is the @tag which is linked to a Cucumber feature scenario hence I have created separate runner files with @sanity @regression etc. I want to avoid creating so many runner files as only the @tag is a variable. I want to parameterize the annotation so that I just have one runner file.
package com.ibm.wce.scbn.cc.runner;
import org.apache.log4j.Logger;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import cucumber.api.CucumberOptions;
import cucumber.api.testng.CucumberFeatureWrapper;
import cucumber.api.testng.TestNGCucumberRunner;
@CucumberOptions(features = "./Features", glue = { "com.ibm.wce.scbn.cc.stepdefinitions" }, tags = { "@Sanity" }
)
public class Sanity {
private TestNGCucumberRunner testNGCucumberRunner;
private static final Logger logger = Logger.getLogger(Sanity.class.getName());
@BeforeClass(alwaysRun = true)
public void setUpClass() throws Exception {
logger.info("initialize TestNG cucumber runner");
testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());
}
@Test(dataProvider = "features")
public void feature(CucumberFeatureWrapper cucumberFeature) {
logger.info("Running Cucumber features in loop");
testNGCucumberRunner.runCucumber(cucumberFeature.getCucumberFeature());
}
@DataProvider
public Object[][] features() {
logger.info("inside data provider");
return testNGCucumberRunner.provideFeatures();
}
@AfterClass(alwaysRun = true)
public void tearDownClass() throws Exception {
logger.info("close cucumber runner");
testNGCucumberRunner.finish();
}
}
Feature: Validating Mailbox response using Json data provider
@Sanity
Scenario: Validate Mailbox Bad Request response
Given We have an ENDPOINT_baseURI "api.baseUrl"
Given I set test report name "Mailbox API-Without required fields(Bad Request)"
And We declare a new Request
And We have Json request "bad_request" in file "api.mailboxJsonPayLoads"
And We have below Header parameters
| Content-Type | application/json |
When We send post request to service "api.mailboxService"
Then The response status code should be Bad Request
<suite name="Suite1">
<test name="Sanity">
<classes>
<class name="com.ibm.wce.scbn.cc.runner.Sanity" />
</classes>
</test> <!-- Test -->
<test name="Regression">
<classes>
<class name="com.ibm.wce.scbn.cc.runner.Regression" />
</classes>
</test> <!-- Test -->
</suite> <!-- Sanity1 -->