【发布时间】:2020-01-23 10:47:03
【问题描述】:
我已经运行了这段代码,并且在 chrome 浏览器关闭后截取了屏幕截图 (@After) 如果我注释掉 CloseBrowser();屏幕截图被捕获,但 chromebrowser 保持打开状态。 我希望在测试失败时捕获屏幕截图,然后关闭浏览器。
总结 当前在浏览器关闭后截取的截图,只是一个空白的.png 我希望在浏览器关闭前测试失败时捕获屏幕截图
谢谢
public class TestClass extends classHelper//has BrowserSetup(); and CloseBrowser(); {
@Rule
public ScreenshotTestRule my = new ScreenshotTestRule();
@Before
public void BeforeTest()
{
BrowserSetup();// launches chromedriver browser
}
@Test
public void ViewAssetPage()
{
//My test code here//And want to take screenshot on failure
}
@After
public void AfterTest() throws InterruptedException
{
CloseBrowser();//closes the browser after test passes or fails
}
}
class ScreenshotTestRule implements MethodRule {
public Statement apply(final Statement statement, final FrameworkMethod frameworkMethod, final Object o) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
try {
statement.evaluate();
} catch (Throwable t) {
captureScreenshot(frameworkMethod.getName());
throw t; // rethrow to allow the failure to be reported to JUnit
}
}
public void captureScreenshot(String fileName) {
try {
new File("target/surefire-reports/").mkdirs(); // Insure directory is there
FileOutputStream out = new FileOutputStream("target/surefire-reports/screenshot-" + fileName + ".png");
out.write(((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES));
out.close();
} catch (Exception e) {
// No need to crash the tests if the screenshot fails
}
}
};
}
}
【问题讨论】:
标签: java maven selenium junit selenium-chromedriver