I have test class defined as this-
public class Sample extends BaseTest {
private LoginPage loginPage;
@Override
public void initialize() {
loginPage = createInstance(LoginPage.class)
browserContext = getBrowserContext();
}
@Test(testName = "sampleTestName", retryAnalyzer = RerunFailedTestCases.class)
public void sampleTestName() {
loginPage.performLogin();
loginPage.validateLogInSuccessful();
}
In BaseTest I am initializing BrowserContext and enabling video recording for the tests-
public abstract class BaseTest {
protected BrowserContext browserContext = browser.newContext(new Browser.NewContextOptions()
.setIgnoreHTTPSErrors(true)
.setPermissions(Arrays.asList("geolocation"))
.setRecordVideoDir(Paths.get(VIDEOS_DIR)));
}
My requirement is-
- Record video of test with testMethod name
- only keep video of the failed tests
Here is the Playwright for Java doc on the Video class, which shows how to get the video associated with the page, deleting a video (or deciding it should be), and saving as or telling it what file name to use.
So basically at some point in your test or a before/after hook, even potentially in your base class, you can tell the video to save as the test name (couple resources below if unsure how to obtain that), and to delete when successful/doesn’t fail (couple resources below on determining test result).
Hope that helps!
Some Resources
I’m not as familiar with Java and its runners, but you may already know how to do some of these. If not, here’s a few resources I found:
Getting test result
Finding test name