尝试在testNG Selenium中制作屏幕截图时为空指针

时间:2014-07-30 12:40:25

标签: selenium jenkins testng

当我尝试进行屏幕截图时,出现以下错误:

org.apache.maven.surefire.util.SurefireReflectionException: java.lang.reflect.InvocationTargetException; nested exception is java.lang.reflect.InvocationTargetException: null
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
    at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)
Caused by: java.lang.NullPointerException
    at com.volvo.prompt.uitests.selenium.util.ScreenshotOnFailureListener.onConfigurationFailure(ScreenshotOnFailureListener.java:51)
    at org.testng.internal.Invoker.runConfigurationListeners(Invoker.java:1868)
    at org.testng.internal.Invoker.handleConfigurationFailure(Invoker.java:334)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:237)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
    at org.testng.TestRunner.beforeRun(TestRunner.java:641)
    at org.testng.TestRunner.run(TestRunner.java:609)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:122)
    at org.apache.maven.surefire.testng.TestNGXmlTestSuite.execute(TestNGXmlTestSuite.java:92)
    at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:101)
    ... 9 more

这是代码:(第51行导致问题)

public class ScreenshotOnFailureListener extends TestListenerAdapter {

    @Override
    public void onTestFailure(ITestResult tr) {

        File screenshotFile = ((TakesScreenshot)TestBase.driver).getScreenshotAs(OutputType.FILE);
        String fileName = DateFormatter.getTimestamp(Calendar.getInstance()) + "."
                + System.getProperty("browser") + "."
                + tr.getMethod().getMethodName() 
                + ".png";
        String path = System.getProperty("webdriver.screenshots");
        File targetFile = new File(path + "/" + fileName);

        if ( !targetFile.getParentFile().exists() ) {
            targetFile.getParentFile().mkdirs();
        }

        try {
            FileUtils.copyFile(screenshotFile, targetFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        Reporter.log("<a href=\"..\\..\\" + targetFile.getPath() + "\">Failed Screenshot</a>");
    }

测试从Jenkins运行。在本地工作:/

使用的浏览器:Chrome。

感谢您提前重播

BR 的Jakub

0 个答案:

没有答案