Reading parameter values from Testng.xml file in cucumber stepdefs

前提是你 提交于 2019-12-01 23:29:43

问题


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.

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>

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.


回答1:


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:

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



来源:https://stackoverflow.com/questions/31924000/reading-parameter-values-from-testng-xml-file-in-cucumber-stepdefs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!