简体   繁体   中英

How to pass a parameter from testNG.xml file to cucumber Scenarios/steps

I am currently working on Cucumber+MVN+TestNG+selenium framework. I have an variable "browsername" which i have defined in a.properties file and I am able to access this variable inside my cucumber steps properly. But now i am trying to override this variable from my TestNG.xml file itself as a parameter like

My TestNG.xml:

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">  
<suite name="Sample Cucumber TestNG Maven project" verbose="1" thread-count="1" parallel="methods" configfailurepolicy="continue">  
     <test name="Cucumber with TestNG">  
        <parameter name="browsername" value="Firefox"></parameter>

         <classes>
             <class name="testRunner.TestRunner">  
                 <methods>
                     <include name="scenario"/>  
                 </methods>
            </class>
        </classes>
    </test>
</suite>

My Environment.properties is as below which I am trying to override using TestNG parameter

browsername=Chrome
browser.url=https://www.gmail.com

My Stepdefinition:

@Parameters("browsername")
    @Given("Launch the application")
    public void launch_the_application(String browsername) {
     Properties property = prop.getProperty();
     driver=DriverUtils.createDriver(driver, browsername);
     driver.get(property.getProperty("browser.url"));
    }

My TestRunner file

    package testRunner;

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.AbstractTestNGCucumberTests;
import cucumber.api.testng.CucumberFeatureWrapper;
import cucumber.api.testng.PickleEventWrapper;
import cucumber.api.testng.TestNGCucumberRunner;


@CucumberOptions(features=("src\\test\\resources\\features\\Navigation.feature"),
                 glue= {"com.homemadetesting.stepdefinition","utils"},
                 strict = true,
                 plugin= {"pretty","html:target/cucumber"},
                 tags= {"@testme"}
        )


public class TestRunner {
private TestNGCucumberRunner testNGCucumberRunner;

    @BeforeClass(alwaysRun = true)  
    public void setUpClass() throws Exception {  
        testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());  
  } 


     @Test(groups = "Cucumber", description = "Runs Cucumber Feature", dataProvider = "scenarios")  
        public void scenario(PickleEventWrapper pickleEvent, CucumberFeatureWrapper cucumberFeature) throws Throwable {  
            testNGCucumberRunner.runScenario(pickleEvent.getPickleEvent());  
      } 
      @DataProvider  
      public Object[][] scenarios() {  
            return testNGCucumberRunner.provideScenarios();  
      }  
      @AfterClass(alwaysRun = true)  
        public void tearDownClass() throws Exception {  
            testNGCucumberRunner.finish();  
      } 

}

I am getting below error:

[31mcucumber.runtime.CucumberException: Step [Launch the application] is defined with 1 parameters at 'com.homemadetesting.stepdefinition.ShopAssistStepdef.launch_the_application(String) in file:/C:/Users/XXXXX/eclipse/eclipse/JavaWorkspace/heb/target/test-classes/'.However, the gherkin step has 0 arguments.

Please advice how I can handle this

In your launch_the_application method you are expecting one argument but your regex does not capture any arguments. It should look something like that instead:

@When("^Launch the application \"([^\"]*)\"$")

And then in your feature file:

When Launch the application "browser"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM