在为我的测试套件中的失败步骤截屏时,我遇到了空指针异常。我确实在谷歌上寻找解决方案,但对我来说还没有任何效果。欣赏是否有人可以请建议。
我有 2 个 android 测试类和 2 个 iOS 测试类。android 和 iOS 都有自己的基础程序来初始化 android/iOS 驱动程序(声明为非静态)。测试类正在调用基础程序来初始化驱动程序(如 this.driver = .initiaze())以并行运行所有 4 个测试类。
我有 2 个单独的侦听器(在失败时截屏),一个用于 android,一个用于 iOS。当任何测试失败时,侦听器程序调用(android 侦听器调用 android base 和 ios 调用 ios base 程序)base 程序 getscreenshot 方法,然后失败并出现 NPE 错误。
以下是参考的一些代码示例。
(注意 - 如果我在基本程序中将驱动程序定义为公共静态,则 NPE 错误消失但并行运行失败并出现随机错误,因为一个类的驱动程序被另一类使用)
Android 基础:(iOS 基础的类似代码,返回类型为 IOSDriver)
g_MobileBase.java:
public class g_MobileBase {
@SuppressWarnings("rawtypes")
public AndroidDriver driver=null;
public DesiredCapabilities cap;
@SuppressWarnings("rawtypes")
public AndroidDriver InitializeDriver() throws IOException
{//initialization code; return driver;
}
public void getScreenshot(String classname, String testname) throws IOException
{
File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(src,new File(System.getProperty("user.dir")+"\\ErrorSnapshots\\"+classname+"_"+testname+"_"+new SimpleDateFormat("yyyy-MM-dd hh-mm-ss").format(new Date())+".png"));
}
}
Android 测试类#1:
public class G_SearchStore_LocOff extends g_MobileBase{
@BeforeTest (description="Initialize driver and install application")
public void Initialize() throws IOException
{
this.driver = InitializeDriver();
//remaining code
}
@AfterTest (description="Close application and Quit driver")
public void finish()
{
driver.closeApp();
driver.quit();
}
@Test
.................some methods
.................some methods
}
Android 监听器:(类似 iOS 监听器,只创建 ios 基础程序的对象)
public class g_testListener implements ITestListener{
g_MobileBase b = new g_MobileBase();
@Override
public void onTestFailure(ITestResult result) {
// TODO Auto-generated method stub
String[] temp = result.getInstanceName().toString().split("\\.");
String classname = temp[1];
try {
b.getScreenshot(classname,result.getName());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}