【发布时间】: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