Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to record video of tests with custom file name as test method name in playwright java and testng framework

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-

  1. Record video of test with testMethod name
  2. only keep video of the failed tests
like image 707
Rajeev Dixit Avatar asked Nov 23 '25 15:11

Rajeev Dixit


1 Answers

After the page is closed, you can using page.video().saveAs() to save the video to a custom path.

BrowserContext context = browser
                    .newContext(new Browser.NewContextOptions()
                          .setRecordVideoDir(Paths.get("videos/")));

Page page = context.newPage();


page.close(); // Make sure the page is closed before calling page.video()
page.video().saveAs(Paths.get("abc.webm")); 
like image 174
abnvanand Avatar answered Nov 25 '25 03:11

abnvanand



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!