我正在开发一个使用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 @跟在下面的钩子下面,但它偶尔给我带来了Nullpointerexecption。
错误: appiumdriver.WebDriverFactory.isSessionActive(WebDriverFactory.java:146) at appiumdriver.WebDriverFactory.closeAppiumDriver(WebDriverFactory.java:159) at stepDefs.AndroidTestsCommonStepDefs_usingFactory.teardown(AndroidTestsCommonStepDefs_usingFactory.java:140)的java.lang.NullPointerException
@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();&设置driver =null对我来说至关重要。
请告知
谢谢
发布于 2017-08-03 12:57:21
目前,这两个后置钩子可以以任何顺序运行。因此,您正在断断续续地获得异常。您需要在后置注释中使用order属性,以确保最后执行驱动程序闭包。
在订单属性值较高的情况下,先执行具有较低值的属性值。注释前的相反行为。
对于嵌入方法,可以使用@After(order=20)和驱动程序闭包@After(order=10)。
https://stackoverflow.com/questions/45479866
复制相似问题