失败测试用例的屏幕截图和获取屏幕截图

时间:2019-08-11 23:39:07

标签: nullpointerexception testng screenshot

当我尝试运行失败的测试用例并尝试截屏时,我得到了nullpointer异常。下面的代码来自我的Listener类:

@Override public void onTestFailure(ITestResult arg0) { System.out.println("Test Fail->"+ getTestMethodName(arg0)); String fileName = String.format("Screenshot-%s.jpg", Calendar.getInstance().getTimeInMillis()); WebDriver driver = (WebDriver)arg0.getTestContext().getAttribute("webDriver"); TakesScreenshot ts = (TakesScreenshot)driver; File srcFile = ts.getScreenshotAs(OutputType.FILE); File destFile = new File("./screenshots/" + fileName); try{ FileUtils.copyFile(srcFile, destFile); System.out.println("Successfully captured a screenshot"); }catch (Exception e){ System.out.println("Exception while taking screenshot "+e.getMessage()); }
}

我遇到以下错误: java.lang.NullPointerException     在utils.Listener.onTestFailure(Listener.java:43)     在org.testng.internal.TestListenerHelper.runTestListeners(TestListenerHelper.java:67)     在org.testng.internal.Invoker.runTestListeners(Invoker.java:1389)     在org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1042)     在org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)     在org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)     在org.testng.TestRunner.privateRun(TestRunner.java:648)     在org.testng.TestRunner.run(TestRunner.java:505)     在org.testng.SuiteRunner.runTest(SuiteRunner.java:455)     在org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)     在org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)     在org.testng.SuiteRunner.run(SuiteRunner.java:364)     在org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)     在org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)     在org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)     在org.testng.TestNG.runSuitesLocally(TestNG.java:1137)     在org.testng.TestNG.runSuites(TestNG.java:1049)     在org.testng.TestNG.run(TestNG.java:1017)     在org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)     在org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)     在org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

0 个答案:

没有答案