【问题标题】:Cucumber+TestNG+Cucable plugin: How can I pass the appium driver instance that I create in the @beforemethod instance to the page objects?Cucumber+TestNG+Cucable 插件:如何将我在@beforemethod 实例中创建的appium 驱动程序实例传递给页面对象?
【发布时间】:2019-06-26 16:33:37
【问题描述】:

这是我正在尝试构建的:

  1. 使用 TestNG Executor 进行基于 Cucumber 的测试
  2. TestNG 执行器,以便我可以重新运行失败的测试
  3. Cucable 插件 - 以便将场景拆分为单独的文件,并在运行时为每个场景自动生成运行程序。 (启用并行执行)

下面是测试运行器:

@CucumberOptions(
        glue = "com.fifa.stepdefs",
        features = {"target/parallel/features/[CUCABLE:FEATURE].feature"},
        plugin = {"json:target/cucumber-report/[CUCABLE:RUNNER].json"}
)

public class CucableJavaTemplate implements IRetryAnalyzer {

    private int count = 0;
    private static int maxTry = 3;

    @Override
    public boolean retry(ITestResult iTestResult) {
        if (!iTestResult.isSuccess()) {  ;//Check if test not succeed
            if (count < maxTry) {                            //Check if maxtry count is reached
                count++;                                    //Increase the maxTry count by 1
                iTestResult.setStatus(ITestResult.FAILURE);  //Mark test as failed
                return true;                                 //Tells TestNG to re-run the test
            } else {
                iTestResult.setStatus(ITestResult.FAILURE);  //If maxCount reached,test marked as failed
            }
        } else {
            iTestResult.setStatus(ITestResult.SUCCESS);      //If test passes, TestNG marks it as passed
        }
        return false;
    }
    private TestNGCucumberRunner testNGCucumberRunner;

    @BeforeClass(alwaysRun = true)
    public void setUpClass() throws Exception {
        System.out.println("Before Scenario ****");
        testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());
    }

    @Test(groups = "cucumber", description = "Runs Cucumber Scenarios", dataProvider = "scenarios",retryAnalyzer = CucableJavaTemplate.class)
    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 {
        System.out.println("After Scenario ****");
        testNGCucumberRunner.finish();
    }
}

如果我在之前的类中创建了一个 Driver 实例:如何将它传递给页面对象文件或步骤定义文件?

【问题讨论】:

  • 你可以看看使用 DI 工具。尝试黄瓜 pico-container 实现。看this。顺便说一句,如果你使用大于 4 的版本,你可以使用内置的重新运行插件和并行执行。

标签: java cucumber cucumber-jvm cucumberjs cucumber-junit


【解决方案1】:

我在这个问题上给出了类似的答案: How to match a specific java file with specific feature file in cucumber 如果您使用 @ScenarioScoped 注释了驱动程序类,那么您可以轻松地使用 Guice 将此实例注入您的其他类中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-30
    • 2021-12-23
    • 2018-06-08
    • 2011-10-09
    相关资源
    最近更新 更多