gpt4 book ai didi

java - 无法从 selenium 中的监听器类捕获屏幕截图,总是出现空指针异常

转载 作者:行者123 更新时间:2023-12-02 11:05:50 25 4
gpt4 key购买 nike

这是我在 Listener 类中使用范围报告的代码。

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

我什至尝试将 @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...

最佳答案

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

Common Helper类中需要添加代码:

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

关于java - 无法从 selenium 中的监听器类捕获屏幕截图,总是出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50966225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com