问题描述
我遇到了一些麻烦,从昨天开始就卡住了;无法找出原因.在这里尝试了答案的解决方案.
I am facing some trouble and got stuck since yesterday; unable to figure out the cause for it. Tried solution of answers here.
我创建了@Test,其中包含用于登录和检查应用程序仪表板的代码示例.和@AfterMethod,当 Assert 为 false 时,应捕获屏幕截图.
I created @Test, with sample of code to login and check dashaboard of application.and @AfterMethod, for when Assert is false, should capture a screenshot.
如果我评论 Aftermethod 代码,它可以正常工作,没有任何问题;
If i comment the Aftermethod code it works fine without any issue;
它以前运行良好,没有任何问题.
It used to run fine w/o any problem earlier.
你能帮我找到一些解决方案吗?(这对你来说可能是很小的事情......但请帮助我)
Could you please help me in finding some solution. (it may be very small thing for you.. but pls do help me)
(已编辑)我得到的错误是
(EDITED) Error i am getting is
[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 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();
}
实用类
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();
}
}
}
推荐答案
由于您没有提到完整的错误堆栈跟踪,因此确定失败的确切原因有点困难.请检查驱动程序值是否为空.
Since you have not mentioned complete error stacktrace, it is a bit difficult to identify exact cause of failure. Please check whether driver value is getting null.
我建议您利用 ITestListener
而不是使用带有 @AfterMethod
的方法.您只需要实现 ITestListener
,它会在测试失败时为您截取屏幕截图.
I would suggest you to leverage ITestListener
instead of using method with @AfterMethod
. You simply need to implement ITestListener
which will take the screenshot for you on test failure.
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);
}
}
}
谢谢!
这篇关于截取屏幕截图为空且配置失败时出现异常:@AfterMethod 拆解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!