截屏时出现异常null和FAILED CONFIGURATION:@AfterMethod拆解

时间:2017-01-17 05:57:29

标签: selenium selenium-webdriver testng

我面临一些麻烦,从昨天起就陷入困境;无法找出原因。在这里尝试了解答。

我创建了@Test,其中包含要登录的代码示例并检查应用程序的dashaboard。 和@AfterMethod,当Assert为假时,应捕获截图。

如果我评论Aftermethod代码,它可以正常工作;

它曾经运行良好,没有任何问题。

请你帮我找一些解决方案。 (对你来说这可能是非常小的事情......但请帮助我)

(已编辑)我得到的错误是

[TestNG] Running:
  C:\Users\Lenovo\AppData\Local\Temp\testng-eclipse--1410131027\testng-customsuite.xml

Exception while taking screenshot null
FAILED CONFIGURATION: @AfterMethod teardown([TestResult name=dashboardSanityTest status=FAILURE method=loginMain.dashboardSanityTest()[pri:0, instance:demotest.loginMain@1bce4f0a] output={null}])
java.lang.NullPointerException
    at demotest.loginMain.teardown(loginMain.java:144)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:510)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:211)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:703)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
    at org.testng.TestRunner.privateRun(TestRunner.java:774)
    at org.testng.TestRunner.run(TestRunner.java:624)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
    at org.testng.SuiteRunner.run(SuiteRunner.java:261)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
    at org.testng.TestNG.run(TestNG.java:1048)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)

SKIPPED CONFIGURATION: @AfterMethod teardown
FAILED: dashboardSanityTest
java.lang.NullPointerException
    at demotest.loginMain.dashboardSanityTest(loginMain.java:105)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
    at org.testng.TestRunner.privateRun(TestRunner.java:774)
    at org.testng.TestRunner.run(TestRunner.java:624)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
    at org.testng.SuiteRunner.run(SuiteRunner.java:261)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
    at org.testng.TestNG.run(TestNG.java:1048)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)

@AfterMethod Code

@AfterMethod
public void teardown(ITestResult result)
    {
  if (result.getStatus()==ITestResult.FAILURE)
  {
      String screenshotPath = Utils.captureScreenshot(driver, result.getName());
      String image = logger.addScreenCapture(screenshotPath);
  }

  report.flush();

}

Utils class

public class Utils {

public static String captureScreenshot(WebDriver driver, String ScreenshotName)
{
    try 
    {
        TakesScreenshot ts = (TakesScreenshot)driver;
        File source = ts.getScreenshotAs(OutputType.FILE);
        String dest="./screenshot/"+ScreenshotName+".png";
        File snapshotDest =new File(dest);
        FileUtils.copyFile(source, snapshotDest);

        System.out.println("Screenshot Taken at "+System.currentTimeMillis());

        return dest;

    } 

    catch (Exception e) 
    {
    System.out.println("Exception while taking screenshot "+e.getMessage());
    return e.getMessage();
        }
    }
 }

1 个答案:

答案 0 :(得分:3)

由于您没有提到完整的错误堆栈跟踪,因此确定确切的失败原因有点困难。请检查驱动程序值是否为空。

我建议您利用ITestListener而不是使用@AfterMethod方法。您只需要实现ITestListener,它会在测试失败时为您截取屏幕截图。

public class Listener implements ITestListener {
    public WebDriver driver;

    @Override
    public void onStart(ITestContext arg0) {
        Reporter.log("About to begin executing Test " + arg0.getName(), true);
    }

    @Override
    public void onFinish(ITestContext arg0) {
        Reporter.log("Completed executing test " + arg0.getName(), true);
    }

    @Override
    public void onTestFailure(ITestResult arg0) {
        try {
            String fileName = String.format("Screenshot-%s.jpg", Calendar
                    .getInstance().getTimeInMillis());
            driver = (WebDriver) arg0.getTestContext().getAttribute("WebDriver");
            TakesScreenshot ts = (TakesScreenshot)driver;
            File source = ts.getScreenshotAs(OutputType.FILE);
            String dest="./screenshot/"+ fileName;
            File snapshotDest =new File(dest);
            FileUtils.copyFile(source, snapshotDest);
            Reporter.log("Screen Shots file :  " + dest);
        } catch (Exception e) {
            throw new RuntimeException("Failed to take screenshot !", e);
        }
    }

}

谢谢!