无法从硒中的侦听器类捕获屏幕快照,始终获取Null Pointer异常

时间:2018-06-21 10:26:59

标签: java selenium selenium-webdriver testng

这是我在使用扩展区报告的侦听器类中的代码。

我正在使用Common Helper类,其中包含@Beforetest和@After测试...等。

我什至尝试将@Beforetest放在此侦听器类中。但是错误来了。

还有其他不用驱动程序来捕获屏幕快照的方法吗?

public class CommonITestNGListener implements ITestListener{

    //Extent Report Declarations
    private static ExtentReports extent = ExtentManager.createInstance();
    private static ThreadLocal<ExtentTest> test = new ThreadLocal<>();

    //Other Declartions
    WebDriver driver = commonhelper.driver;
    public static DateFormat DF = new SimpleDateFormat("dd-MM-yyyy_HH_mm_ss");
    public static Date D = new Date();
    public static String time = DF.format(D);
    public String ErrSS = System.getProperty("user.dir")+"\\Screenshots\\";


    @Override
    public synchronized void onStart(ITestContext context) {
        System.out.println("...Test Suite Execution started...");
    }

    @Override
    public synchronized void onFinish(ITestContext context) {
        System.out.println("...Test Suite Execution Ended...");
        File f = new File(System.getProperty("user.dir")+"\\TestReport\\Test_Automaton_Report.html");
        if (f.exists()) {
            String oldtDir = System.getProperty("user.dir") + "\\TestReport\\Old\\";
            File fold = new File(oldtDir);
            String rn = "Test_Automaton_Report_bkp_"+time+".html";
            System.out.println("A New Report Generated with name  : Test_Automaton_Report.html"+ "\n" +"Existing Old Report will moved to : TestReport\\Old and Renamed as = " + rn);
            File nf = new File(rn);
            f.renameTo(nf);
            try {
                FileUtils.moveFileToDirectory(nf, fold, true);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        extent.flush();
    }

    @Override
    public synchronized void onTestStart(ITestResult result) {
        System.out.println(result.getMethod().getMethodName() + " Started!");
        ExtentTest extentTest = extent.createTest(result.getMethod().getMethodName(), result.getMethod().getDescription());
        test.set(extentTest);
    }

    @Override
    public void onTestSuccess(ITestResult result) {
        System.out.println(result.getMethod().getMethodName() + " Passed!");
        test.get().pass("... Test Passed ...");
    }

    @Override
    public void onTestFailure(ITestResult result) {
        System.out.println(result.getMethod().getMethodName() + " Failed!");
        test.get().fail(result.getThrowable());
        try {
            test.get().addScreenCaptureFromPath(CaptureScreenShot(result.getMethod().getMethodName()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onTestSkipped(ITestResult result) {
        System.out.println(result.getMethod().getMethodName() + " Skipped!");
        test.get().skip(result.getThrowable());
    }

    @Override
    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
        System.out.println("onTestFailedButWithinSuccessPercentage for" + result.getMethod().getMethodName());
    }

    //Capture Screen shot (with Normal Java Utility)
    public String CaptureScreenShot(String screenshotname) throws Exception{
        TakesScreenshot ts = (TakesScreenshot)driver;
        File Source = ts.getScreenshotAs(OutputType.FILE);
        String dest = ErrSS + screenshotname+ "_"+time+".png";
        File destination = new File(dest);
        FileUtils.copyFile(Source, destination);
        System.out.println("Screen shot captured for the error and saved");
        return dest;
    }

}

没有显示语法错误。但是在运行脚本时,它无法进行屏幕截图。

以下是输出。

NavtoWQ Started!
NavtoWQ Passed!
NavtoWQ2 Started!
NavtoWQ2 Failed!
java.lang.NullPointerException
    at common.CommonITestNGListener.CaptureScreenShot(CommonITestNGListener.java:100)
    at common.CommonITestNGListener.onTestFailure(CommonITestNGListener.java:79)
    at org.testng.internal.TestListenerHelper.runTestListeners(TestListenerHelper.java:67)
    at org.testng.internal.Invoker.runTestListeners(Invoker.java:1388)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:633)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:716)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:988)
    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.SuiteRunner.runTest(SuiteRunner.java:455)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
    at org.testng.SuiteRunner.run(SuiteRunner.java:364)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
    at org.testng.TestNG.runSuites(TestNG.java:1049)
    at org.testng.TestNG.run(TestNG.java:1017)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
...Test Suite Execution Ended...

2 个答案:

答案 0 :(得分:0)

由于驱动程序,您将收到NULL指针异常。我建议使用After测试方法来捕获故障场景的屏幕截图。请在您的Common Helper类中添加以下方法(我假设您将在Common Helper类中具有驱动程序实例)。请确保注释掉onTestFailure方法中的try catch块。

需要在“通用帮助程序类”中添加代码:

@AfterMethod(alwaysRun = true)
public void captureScreenshot(ITestResult result){
    //Take the Screenshot Only, If the Test is failed.
    // Change the condition , If the screenshot needs to be taken for other status as well
    if(ITestResult.FAILURE==result.getStatus()){
        System.out.println("Failed Status Check");
        File temp= ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        String dest = ErrSS + screenshotname+ "_"+time+".png";
        try{
            FileUtils.copyFile(temp,new File(dest));
        }
        catch(Exception e){
            System.out.println(e.getStackTrace());
        }
    }
}

答案 1 :(得分:0)

这是您要寻找的答案(2-3天前由我回答):
Extent Report 3 Add Screenshot

我认为您在这里遇到了同样的问题,即驱动程序没有进入侦听器。