关于创建屏幕截图:创建屏幕截图-Allure,JUnit5,Selenium

Create screenshot - Allure, JUnit5, Selenium

是否有任何相关文档? JUnit4具有@Rule并且解决方案很简单。 对于JUnit5,我做了一个扩展public class TestWatcher implements AfterTestExecutionCallback,但是我不知道在@Override方法中放什么。


我设法解决了。 屏幕捕获方法是默认方法之一:

1
2
3
4
5
@Attachment(value ="{testName} - screenshot", type ="image/png")
private byte[] makeScreenshotOnFailure(String testName) {

    return ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
}

和TestWatcher(扩展名):

1
2
3
4
5
6
7
8
9
10
11
12
13
@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());
    }
}


您可以在此处找到示例。 它是NoraUi开源框架(Java + Selenium)中的代码吗?

1
2
3
import org.openqa.selenium.TakesScreenshot;

final byte[] screenshot = ((TakesScreenshot) Context.getDriver()).getScreenshotAs(OutputType.BYTES);