截取空屏幕和配置失败时出现异常:@AfterMethod 拆解

问题描述 投票:0回答:2

我遇到了一些麻烦,从昨天开始卡住了;无法找出原因。在这里尝试解决答案。

我创建了@Test,带有用于登录和检查应用程序仪表板的代码示例。 和 @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代码

@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();

}

工具类

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();
        }
    }
 }
selenium selenium-webdriver testng
2个回答
3
投票

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

我建议你利用

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);
        }
    }

}

谢谢!


0
投票

请检查您的目标文件夹路径是否不正确,您应该包括双“\”而不是“”,这就是发生配置问题的原因。

© www.soinside.com 2019 - 2024. All rights reserved.