Showing posts with label Playwright Video. Show all posts
Showing posts with label Playwright Video. Show all posts

Video Recording in Playwright



In Playwright Java, video recording is a feature that allows you to capture the execution of your automated browser test sessions in the form of video files. This is particularly useful for debugging, reporting, and understanding the behavior of your test scripts when they run.

Purpose:

Video recording helps testers analyze the test flow, identify failures, and verify that actions were performed as intended. Instead of only relying on logs or screenshots, a full playback video gives complete visibility of the user journey.

How It Works:

Playwright provides video recording capabilities at the context level (BrowserContext). When a new browser context is created, you can enable video recording for all pages that belong to that context. Each video records the actions performed in its corresponding page session.

Storage of Videos:

The recorded videos are typically stored in a specified directory on the local system. After test execution, the video files can be retrieved and saved for later inspection. The storage location can be configured when setting up the context.

Format:

Videos are generally saved in .webm format, which is lightweight and compatible with most browsers and media players.


Use Cases:

  • Debugging Failures – When a test fails, you can watch the recording to see the sequence of actions that led to the failure.
  • Documentation – Videos can be shared with developers, QA teams, or stakeholders to demonstrate test execution.
  • Regression Testing – Helps in validating that new changes have not broken the existing flow by visually confirming execution steps.

Integration with Reporting Tools:

The recorded videos can also be attached to automated test reports, making them valuable for test evidence in Continuous Integration/Continuous Deployment (CI/CD) pipelines.

In Playwright Java, you can perform video recording of your browser session by enabling video recording in the browser context options. Playwright will save the video of each page created from that context.












Steps to Record Video in Playwright Java:

  • Create a directory where video recordings will be saved.
  • Configure the context using Browser.NewContextOptions() and enable video recording.
  • Perform browser actions.
  • Close the page and context to ensure the video is saved.

Website to be automate: https://example.com









Code Example: Record Video in Playwright Java


import com.microsoft.playwright.*;

import java.nio.file.Paths;

public class VideoRecordingExample {
    public static void main(String[] args) {
        try (Playwright playwright = Playwright.create()) {
            // Launch browser
            Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));

            // Set video recording options
            Browser.NewContextOptions contextOptions = new Browser.NewContextOptions()
                    .setRecordVideoDir(Paths.get("videos/"))  // Directory to save videos
                    .setRecordVideoSize(1280, 720);          // Optional: set video resolution

            // Create browser context with video recording
            BrowserContext context = browser.newContext(contextOptions);

            // Create a new page (video recording will start)
            Page page = context.newPage();

            // Navigate to a website
            page.navigate("https://example.com");

            // Perform some actions
            page.click("text=More information"); // Adjust if element exists

            // Wait for a while (simulate some user interaction time)
            page.waitForTimeout(3000);

            // Close page and context (this will save the video)
            page.close();
            context.close();

            System.out.println("Video saved in: videos/ directory");
        }
    }
}


Code explanation:

(a) Launch browser
(b) Set video recording options by setRecordVideoDir() and setRecordVideoSize() methods
(c) Create BrowserContext object
(d) Create Page object
(e) Navigate to the website
(f) Perform some actions on html web elements


Code snippet responsible to record and store videos






Videos folder in Project Structure













Important Points:
  • Make sure videos/directory is writeable.
  • Video is finalized only when the page or context is closed.
  • You can customize file name using recordVideoDir and managing output.