如何将参数从testNG.xml文件传递到黄瓜方案/步骤

时间:2019-09-20 16:06:04

标签: java selenium cucumber testng cucumber-java

我目前正在研究Cucumber + MVN + TestNG + Selenium框架。我在.properties文件中定义了一个变量“浏览器名称”,并且能够在黄瓜步骤中正确访问此变量。 但是现在我试图从我的TestNG.xml文件本身覆盖此变量作为参数,例如

我的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>

我的Environment.properties如下所示,我正尝试使用TestNG参数覆盖

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

我的步进定义:

@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"));
    }

我的TestRunner文件

    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();  
      } 

}

我遇到以下错误:

[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.

请告知我该如何处理

1 个答案:

答案 0 :(得分:0)

在launch_the_application方法中,您需要一个参数,但是您的正则表达式不会捕获任何参数。它应该看起来像这样:

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

然后在您的功能文件中:

When Launch the application "browser"
相关问题