parallel-processing - 移动自动化 - Testng 非静态驱动程序 - TakeScreenshot 方法使用并行线程引发 NPE 错误
问题描述
在为我的测试套件中的失败步骤截屏时,我遇到了空指针异常。我确实在谷歌上寻找解决方案,但对我来说还没有任何效果。欣赏是否有人可以请建议。
我有 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();
}
解决方案
问题在于您的测试代码。
TestNG 按功能仅调用@BeforeTest
每个<test>
标签一次。因此,如果在您的<test>
标签中有多个测试类正在尝试使用 webdriver 实例,并且该 webdriver 实例正在通过一种@BeforeTest
方法进行初始化,那么对于第二个实例,webdriver 实例将为空。
要解决此问题,请将@BeforeTest
注释替换为@BeforeClass
注释。
推荐阅读
- flutter - 找不到 Chrome。尝试将 CHROME_EXECUTABLE 设置为 Chrome 可执行文件。颤振 2.0
- python - 当机器人应用程序是在单独的帐户下创建时,如何从我的机器人帐户发送推文?
- spring-boot - 用户缺少权限或找不到对象:
我正在尝试使用如下存储过程填充 Spring Boot 嵌入式 HSQLDB 中的数据。
schema.sql 文件内容:
CREATE TABLE COUNTRY (NAME CHAR(6) NOT NULL, ID INT NOT NULL, PRIMARY KEY (ID)); CREATE PROCEDURE ACC_DETAILS(OUT newid INT, IN firstn
- flutter - 颤振菜单导航器循环
- php - 如何显示我已经从 PHP 的下拉菜单中选择的数据?
- maven - 如何通过 maven 依赖项下载 Jfrog 工件
- c - 在 CodeWare.com 上运行测试的问题
- python - 使用字符串作为python中的输入计算比较矩阵 - 硬
- flutter - Modal BottomSheet scrollControlled 不适用于列表
- kde - 无法更改 KDE 等离子快捷方式