0

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
8
  • Can you elaborate more on your question? Which error do you see?
    – Alexey R.
    Commented Apr 3, 2019 at 11:58
  • @AlexeyR. done!
    – Artur
    Commented Apr 3, 2019 at 12:12
  • I believe Cucumber runner ignores what you have in 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?
    – Alexey R.
    Commented Apr 3, 2019 at 12:37
  • I run testng.xml
    – Artur
    Commented Apr 3, 2019 at 12:53
  • Then it seems you have a conflict with how TestNG interprets parameters and how Cucmber treats its part. Why dont you include the env definition to your Given step? like Given Browser is initiated in QA environment
    – Alexey R.
    Commented Apr 3, 2019 at 12:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.