是否有关于此的文档? JUnit4具有@Rule并且解决方案很简单。对于JUnit5,我做了一个扩展public class TestWatcher implements AfterTestExecutionCallback
,但是我不知道在@Override方法中放什么。
答案 0 :(得分:0)
您可以找到示例here。它是来自NoraUi开源框架(Java + Selenium)的代码。
import org.openqa.selenium.TakesScreenshot;
final byte[] screenshot = ((TakesScreenshot) Context.getDriver()).getScreenshotAs(OutputType.BYTES);
答案 1 :(得分:0)
我设法解决了。屏幕捕获方法是默认方法之一:
@Attachment(value = "{testName} - screenshot", type = "image/png")
private byte[] makeScreenshotOnFailure(String testName) {
return ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
}
和TestWatcher(扩展名):
@Override
public void afterTestExecution(ExtensionContext extensionContext) throws Exception {
Object test = extensionContext.getRequiredTestInstance();
Field a = test.getClass().getDeclaredField("driver");
a.setAccessible(true);
driver = (WebDriver) a.get(test);
Method method = extensionContext.getRequiredTestMethod();
if (extensionContext.getExecutionException().isPresent()) {
makeScreenshotOnFailure(method.getName());
}
}