Could you please tell, how to pass parameter in Cucumber using TestNG?
I have following code, but it does not work.
Error in stepDefinition.java on the line @Parameters({"environment"})
testng.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
<parameter name="environment" value="qa"/>
<test name="Validating Login Page">
<classes>
<class name="cucumberOptions.CucumberRunner"/>
</classes>
</test>
</suite>
CucumberRunner.java
@CucumberOptions(
features = "src/test/java/features",
glue="stepDefinitions")
public class CucumberRunner extends AbstractTestNGCucumberTests {
}
login.feature
Feature: Check Login Page
Scenario: Check Login title
Given Browser is initialled
When User opens Login Page
Then Print current Page title
And Close browser
stepDefinition.java
public class stepDefinition extends TestBase {
@Parameters({"environment"})
@Given("^Browser is initialled$")
public void initBrowser(String environment) {
initialize(environment);
}
@When("^User opens Login Page$")
public void userOpensLoginPage() {
driver.get(url);
}
@Then("^Print current Page title$")
public void printCurrentTitle() {
//some code
}
@And("^Close browser$")
public void printCurrentTitle() {
tearDown();
}
}
TestBase.java
public class TestBase {
public static WebDriver driver = null;
public static String url;
public void initialize(String environment) {
if(environment.equals("qa")){url = "http://qa.com";}
else{url = "http://prod.com";}
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
}
public void tearDown() {
driver.quit();
}
}
Error:
cucumber.runtime.CucumberException: Step [^Browser is initialled$] is defined with 1 parameters at 'stepDefinitions.stepDefinition.initBrowser(String) in file:/Users/artur/IdeaProjects/testProject/target/test-classes/'.
However, the gherkin step has 0 arguments.
Step text: Browser is initialled
testng.xml
. It checks your feature file and tries to match it with method signature in your step definition code. Step in feature file does not imply any paramterization, however your step code accepts the parameter. This makes Cucumber to throw exception. How do you run your code?Given Browser is initiated in QA environment