Cucumber + TestNG + Cucable插件:如何将在@beforemethod实例中创建的appium驱动程序实例传递给页面对象?

时间:2019-06-19 22:45:01

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

这是我要构建的内容:

  1. 使用TestNG Executor进行基于黄瓜的测试
  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();
    }
}

如果我在before类中创建一个Driver实例:如何将其传递给页面目标文件或step def文件?

1 个答案:

答案 0 :(得分:0)

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