测试失败时,我会将屏幕截图添加到侦听器中,以将其添加到报告中。我已经在utils中编写了takeScreenshot方法并返回了路径。
但是当它到达File scshot行时会抛出Null指针异常
//Screenshot method in utils class
public static String takeScreenshot() throws Exception {
File scshot= ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
String path = System.getProperty("user.dir")+ "/Reports/Screenshots/"+ System.currentTimeMillis()+".png";
System.out.println(path);
FileUtils.copyFile(scshot, new File(path));
return path;
}
** //失败
listener**
public void onTestFailure(ITestResult result) {
// TODO Auto-generated method stub
String takeScreenshot = null;
System.out.println(result.getMethod().getMethodName() +" Test Case FAILED !");
try {
takeScreenshot = utils.takeScreenshot();
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
test.get().addScreenCaptureFromPath(takeScreenshot);
} catch (IOException e) {
// TODO: handle exception
e.printStackTrace();
}
}
<-------- **************** ----------> 输出:
VerifyTitleTest Test Case FAILED !
java.lang.NullPointerException
at ExtentReport.utils.takeScreenshot(utils.java:21)
TestNgListeners.TestNGlisteners.onTestFailure(TestNGlisteners.java:56)
at org.testng.internal.TestListenerHelper.runTestListeners(TestListenerHelper.java:67)
at org.testng.internal.Invoker.runTestListeners(Invoker.java:1389)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:636)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng