在Cucumber-JVM中执行@After的问题 - Appium测试

时间:2017-08-03 09:15:30

标签: selenium-webdriver appium cucumber-jvm

我正在开发一个使用Appium,Cucumber-JVM构建的框架。

下面是我如何实例化appium驱动程序的代码:

     private WebDriverFactory() {
    }

    /**
     * Gets the factory instance.
     *
     * @return
     */
    public static WebDriverFactory getInstance() {
        if (factory == null)
            factory = new WebDriverFactory();
        return factory;
    }


    public AppiumDriver getAppiumDriver() throws IOException, NoSuchFieldException {

        if (appiumDriver == null || !isSessionActive(appiumDriver)) {


......instantiate driver......

}return appiumDriver;
}

   */
    private boolean isSessionActive(AppiumDriver driver) {
        return !driver.toString().contains("(null)");
//        return driver.getCapabilities()!=null?true:false;
    }


    public void closeAppiumDriver() {
        if ( (appiumDriver != null || isSessionActive(appiumDriver)) ) {
            appiumDriver.closeApp();
            appiumDriver.quit();
            if (appiumService != null)
                if (appiumService.isRunning())
                    appiumService.stop();
        }
        factory = null;
        appiumDriver = null;
    }

现在,在我的stepDefs中,我放置了Cucumber @After,如下所示,但它偶尔会给我Nullpointerexecption

  

错误:java.lang.NullPointerException           at appiumdriver.WebDriverFactory.isSessionActive(WebDriverFactory.java:146)           at appiumdriver.WebDriverFactory.closeAppiumDriver(WebDriverFactory.java:159)           在stepDefs.AndroidTestsCommonStepDefs_usingFactory.teardown(AndroidTestsCommonStepDefs_usingFactory.java:140)

    @After
public void embedScreenshot(Scenario scenario) throws IOException, NoSuchFieldException {
    try {
        byte[] screenshot = WebDriverFactory.getInstance().getAppiumDriver().getScreenshotAs(OutputType.BYTES);
        scenario.embed(screenshot, "image/png");

    } catch (WebDriverException somePlatformDontsupportSnapshot) {
        System.err.println(somePlatformDontsupportSnapshot.getMessage());
    }
}


@After
public void teardown() throws IOException, NoSuchFieldException {
    System.out.println("Ran the tearDown.");
    WebDriverFactory.getInstance().closeAppiumDriver();
}

我已经尝试将上面的teardown()代码放在Cucumber转轮内的@AfterClass标签上,但它并没有每次都被触发。另外,我无法在Cucumber Runner课程中使用@After。

如何处理这种情况? 此外,将来我可能想在驱动程序中实例化不同的设备,作为测试套件的一部分,因此触发driver.closeApp(); &安培;设置驱动程序= null;对我来说至关重要。

请建议

由于

1 个答案:

答案 0 :(得分:0)

目前,两个After钩子都可以按任何顺序运行。因此,您间歇性地获得异常。您需要使用After注释中的order属性来确保最后执行驱动程序关闭。

如果在具有较低值的订货属性值之后首先执行订单属性值。前注释的相反行为。

对于embed方法,您可以使用@After(order=20)和驱动程序关闭@After(order=10)