测试失败时尝试截图。实际发生的情况是,一旦 intellij 到达我的 @AfterMethod,它会再次启动应用程序并截取主屏幕的屏幕截图。
我尝试过放置extent.flush();进入 @AfterMethod 并将当前的 @AfterMethod 更改为 @AfterTest
@AfterMethod
public synchronized void afterMethod(ITestResult result) throws IOException {
AppiumDriver<MobileElement> driver = MetricellTest.setupTests();
String screenShot = CaptureScreenShot.captureScreen(driver, CaptureScreenShot.generateFileName(result));
if (result.getStatus() == ITestResult.FAILURE) {
test.get().log(Status.FAIL, result.getName());
test.get().log(Status.FAIL, result.getThrowable());
test.get().fail("Screen Shot : " + test.get().addScreenCaptureFromPath(screenShot));
test.get().fail(result.getThrowable());
} else if (result.getStatus() == ITestResult.SKIP) {
test.get().skip("Test Case : " + result.getName() + " has been skipped");
test.get().skip(result.getThrowable());
} else
test.get().pass("Test passed");
extent.flush();
}
}
我希望它能够在测试失败时截取屏幕截图。目前它只是在测试结束时打开应用程序并截取主屏幕的屏幕截图。
最佳答案
在 @AfterMethod
,您正在初始化 driver
再次调用 setupTests
方法和你的setupTests
方法正在对应用程序进行初始化,因此应用程序将再次打开。
因此,您需要在代码中进行以下更改,这样它就可以正常工作了:
声明
AppiumDriver<MobileElement> driver
全局而不是 在setupTests
中声明它方法,以便可以使用 整个类。删除代码行
AppiumDriver<MobileElement> driver = MetricellTest.setupTests();
来自您的@AfterMethod
因为它是 初始化driver
再次打开应用程序。
更新了答案并添加了额外的解释:
您可以声明driver
全局范围内喜欢:
所以,假设你的类(class)名称是 testClass
,您应该声明 driver
就在类的代码开始之后,例如:
public class testClass{
AppiumDriver<MobileElement> driver;
// Add Rest of the code here
}
关于java - 如何修复测试后打开报告然后截图的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55341559/