【问题标题】:Issue in executing @After in Cucumber-JVM - Appium tests在 Cucumber-JVM 中执行 @After 的问题 - Appium 测试
【发布时间】:2017-08-03 20:57:21
【问题描述】:

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

以下是关于我如何实例化 appium 驱动程序的 sn-p:

     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 在 appiumdriver.WebDriverFactory.isSessionActive(WebDriverFactory.java:146) 在 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 runner 内的 @AfterClass 标记中,但它并没有每次都被触发。另外,我无法在 Cucumber Runner 类中使用 @After。

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

请多多指教

谢谢

【问题讨论】:

    标签: selenium-webdriver appium cucumber-jvm


    【解决方案1】:

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

    在订单属性值较高的情况下,在具有较低值的属性值之前先执行。 Before 注释的相反行为。

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

    【讨论】:

    • 当然。我会试试的。但是您是否在下面的代码中看到任何问题,因为它最终指向了这个地方: private boolean isSessionActive(AppiumDriver driver) { return !driver.toString().contains("(null)");
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    相关资源
    最近更新 更多