从黄瓜 stepdefs 中的 Testng.xml 文件中读取参数值黄瓜、参数、文件、Testng

2023-09-08 00:06:04 作者:挽手说梦话

我可以在与 cucumber 集成后运行 testng 脚本.我已按照 http://automatictester.co.uk/2015/06/11/basic-cucumberjvm-selenium-webdriver-test-automation-framework/链接.

I'm able to run testng scripts upon integrating with cucumber. I've followed the exact steps defined in http://automatictester.co.uk/2015/06/11/basic-cucumberjvm-selenium-webdriver-test-automation-framework/ link.

现在我还有一个要求.你能解释一下如何从 testng.xml 的参数标签中读取值吗?见下例:

Now I’ve one more requirement. can you explain me how to read values from parameters tag of testng.xml. See below example:

<test name="ascentis.LoginDemo.Firefox">
    <parameter name="BrowserName" value="Firefox" />
    <parameter name="Environment" value="local" />  
    <packages>
        <package name="runnerFiles.*"/>
    </packages>
</test>

我必须从参数标签中读取 BrowserName 和 Environment 值.我尝试将@parameters 用于黄瓜的@Before 方法,但没有成功,并给出了@Before 钩子只接受一个参数类型的异常.你能解释一下如何从参数标签中读取值以在黄瓜的 stepDefinations 中访问.

I’ve to read BrowserName and Environment values from parameters tag. I’ve tried to use @parameters for @Before method of cucumber but it didn’t work out and gave exception that @Before hook only accepts one parameter that too of type scenario. can you explain me how to read values from parameters tag to access in stepDefinations of cucumber.

推荐答案

好吧,我不确定 testng.xml 级别的 CucumberJVM 测试的参数化是否是您真正想要的.但是,如果您确实需要从 CucumberJVM 框架中的 testng.xml 文件中读取参数,这里有一个(肮脏的)解决方案:

Well, I'm not sure if parametrisation of CucumberJVM tests on testng.xml level is what you are really looking for. However, if you really need to read parameters from testng.xml file in your CucumberJVM framework, here is a (dirty) solution for you:

使 DownloadFeatureRunner 扩展 CustomRunner 而不是 AbstractTestNGCucumberTeststestng.xml 文件中包含参数:<parameter name="someParam" value="someValue"/>

并为你实现新的父类: make DownloadFeatureRunner extend CustomRunner instead of AbstractTestNGCucumberTests include parameter in yout testng.xml file: <parameter name="someParam" value="someValue"/>

and also implement you new parent class:

public class CustomRunner implements IHookable {
    public CustomRunner() {
    }

    @Parameters("someParam")
    @Test(
            groups = {"cucumber"},
            description = "Runs Cucumber Features"
    )
    public void run_cukes(String someParam) throws IOException {

        System.out.println(someParam);
        (new TestNGCucumberRunner(this.getClass())).runCukes();
    }

    public void run(IHookCallBack iHookCallBack, ITestResult iTestResult) {
        iHookCallBack.runTestMethod(iTestResult);
    }

}

如您所见,您可以访问参数的值.你现在想用它做什么取决于你.

As you can see, you can access value of the parameter. It's up to you what you want to do with it now.