在 Cucumber-JVM - Appium 测试中执行@After时出现问题



我正在研究一个框架,使用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中,我像下面这样将黄瓜放在钩子@After,但它偶尔会给我Nullpointerexecption

错误:java.lang.NullPointerException 在Appiumdriver。WebDriverFactory.isSessionActive(WebDriverFactory.java:146( 在Appiumdriver。WebDriverFactory.closeAppiumDriver(WebDriverFactory.java:159( 在stepDefs.AndroidTestsCommonStepDefs_usingFactory.拆解(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(( 代码放入黄瓜运行器中的@AfterClass标签中,但它不是每次都被触发。另外,我无法在黄瓜跑步者课程中使用@After。

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

请告知

谢谢

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

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

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

相关内容

最新更新