首页 > 解决方案 > 如何从 Excel 文件中获取 @tag 到 Selenium java 中的 CucumberOptions

问题描述

通常我会写一个黄瓜选项如下:

@CucumberOptions(
        features = "src\\main\\java\\feature"
            , glue= "stepdefination",
            plugin= {"com.cucumber.listener.ExtentCucumberFormatter:Report/Report.html"}
            tags="@tag, @tag1, @sort" 
           )

公共类 TestRunner 扩展 TestFunction {

@Test
public void runcukes( ) {
    new TestNGCucumberRunner(getClass()).runCukes();
    
}


@BeforeClass
public void tags() {

}

@AfterClass
public void writeExtentReport() {
    Reporter.loadXMLConfig("extent-config.xml");
}

}

我的问题是:如何从 excel 文件中获取 @tag、@tag1、@sort 到 @cucmberoptions 并在 Selenium Java 中运行程序?

标签: javaseleniumselenium-webdriverselenium-chromedrivercucumber-java

解决方案


我不确定是否使用黄瓜选项,但通过使用黄瓜RuntimeOptions类你可以实现它。下面的方法需要在循环中调用,这意味着您有 10 个标签要执行,然后在for循环中调用此方法。

public static boolean cucumberRun(String tag) {
        try {
            ClassLoader classLoader = CustomClass.class.getClassLoader();
            ResourceLoader resourceLoader = new MultiLoader(classLoader);
            ClassFinder classFinder = new ResourceLoaderClassFinder(resourceLoader, classLoader);

            /* Adding cucumber plugins */
            List<String> pluginList = new ArrayList<String>();
            pluginList.add("--plugin");
            pluginList.add("html:target/cucumber-html-report");
            pluginList.add("--plugin");
            pluginList.add("json:target/cucumber.json");
            pluginList.add("--plugin");
            pluginList.add("com.cucumber.listener.ExtentCucumberFormatter:");
            pluginList.add("--plugin");
            pluginList.add("rerun:target/failedScenarios.txt");

            /* Location for BDD extent report. */
            ExtentProperties extentProperties = ExtentProperties.INSTANCE;
            extentProperties.setReportPath("location/Extent_report.html");

            /*
             * Adding cucumberOptions.
             * 
             * You can get the tag value from excel and pass it here. We need to add @ before tag value.
             */
            RuntimeOptions ro = new RuntimeOptions(pluginList);
            ro.getFilters().add("@"+tag);
            ro.getFeaturePaths().add("location of feature files");
            ro.getGlue().add("location of glue code");

            /* Loads all the resources into Cucumber RunTime class and execute. */
            cucumber.runtime.Runtime runtime = new cucumber.runtime.Runtime(resourceLoader, classFinder, classLoader,
                    ro);
            runtime.run();
}catch(Exception e){
// Handle exception
}

推荐阅读