Skip to content

This library is dedicated for testing projects utilizing Microsoft's Playwright for Java automation tool. It provides easy way to manage Playwright resources in a thread safe manner.

License

Notifications You must be signed in to change notification settings

rohit-walia/playwright-manager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🎭 Playwright Resource Manager

The Playwright Resource Manager is a library dedicated to testing-oriented projects that utilize Microsoft's Playwright for Java automation tool. It provides a convenient way to manage Playwright resources, making it easier to work with Playwright in your testing projects. The PlaywrightManager class is specifically designed to help you efficiently manage the lifecycle of Playwright resources.

With PlaywrightManager, you can easily create, get, and close Playwright resources. These resources can be created with default options or customized to your specific configuration. Focus more on writing your test code and less on managing the underlying Playwright resources.

Installation

To use this library in your project, add below Maven dependency to your pom.xml file. Make sure to use the latest version available. This project is deployed to both the Maven Central Repository and theGitHub Package Registry

<dependency>
    <groupId>io.github.rohit-walia</groupId>
    <artifactId>playwright</artifactId>
    <version>${playwright-manager.version}</version>
</dependency>

Usage Examples

Creating resources with default options

Below example demonstrates how you can create Playwright resources by invoking the create() function, automate browser actions like navigating to a URL, and finally closing those resources.

void resourcesWithDefaultOptions() {
  //create resources w/ default options
  Playwright playwright = PlaywrightManager.create(PlaywrightResource.PLAYWRIGHT);
  Browser browser = PlaywrightManager.create(PlaywrightResource.BROWSER);
  BrowserContext browserContext = PlaywrightManager.create(PlaywrightResource.BROWSER_CONTEXT);

  //open browser page and execute automation
  Page page = browserContext.newPage();
  page.navigate("https://github.com/rohit-walia/playwright-manager");

  //remember to close the resources when you are finished with them!
  PlaywrightManager.close(browserContext);
  PlaywrightManager.close(browser);
  PlaywrightManager.close(playwright);
}

Creating resources with custom options

You can pass Options to the create() function to customize these resources. This gives users more control over the behavior of Playwright resources. All Options and their default

void resourcesWithCustomOptions() {
  //enable debug mode and verbose api logging
  PlaywrightOption playwrightOption = PlaywrightOption.builder()
      .enableDebugMode(true)
      .enableVerboseApiLogs(true)
      .build();

  Playwright playwright = PlaywrightManager.create(PlaywrightResource.PLAYWRIGHT, playwrightOption);

  //custom BrowserLaunchOption
  BrowserLaunchOption browserLaunchOption = BrowserLaunchOption.builder()
      .headless(false)
      .slowmo(300)
      .browser("chrome")
      .browserStartTimeout(30000)
      .build();

  //create Browser w/ custom BrowserLaunchOption
  Browser browser = PlaywrightManager.create(PlaywrightResource.BROWSER, browserLaunchOption);

  //create BrowserContext w/ custom BrowserContextOption
  BrowserContextOption browserContextOption = BrowserContextOption.builder().recordVideoDir("target/video").build();
  BrowserContext browserContext = PlaywrightManager.create(PlaywrightResource.BROWSER_CONTEXT, browserContextOption);

  //remember to close the resources when you are finished with them!
  PlaywrightManager.close(browserContext);
  PlaywrightManager.close(browser);
  PlaywrightManager.close(playwright);
}

Simulating multi-browser isolated scenario

In some cases, you might need to stand up multiple independent browser sessions. Since BrowserContext instances are isolated and don't share cookies/cache with each other, creating multiple BrowserContexts is one way to approach these types of scenarios. Alternatively, you can also create multiple Browser instances and then multiple BrowserContext per Browser instance. This resource creations strategy should be carefully thought out.

In addition to resource Options, the create() function can further be controlled by passing extra arguments such as resource instances. Below example demonstrates how we can automate two isolated browser actions against a single Playwright connection.

void multiBrowserExample() {
  Playwright playwright = PlaywrightManager.create(PlaywrightResource.PLAYWRIGHT);
  Browser browser = PlaywrightManager.create(PlaywrightResource.BROWSER);

  //create multiple BrowserContext
  BrowserContext browserContext1 = PlaywrightManager.create(PlaywrightResource.BROWSER_CONTEXT);
  BrowserContext browserContext2 = PlaywrightManager.create(PlaywrightResource.BROWSER_CONTEXT);

  //browser1 will navigate to github
  Page page1 = browserContext1.newPage();
  page1.navigate("https://github.com/rohit-walia/playwright-manager");

  //browser2 will navigate to maven
  Page page2 = browserContext2.newPage();
  page2.navigate("https://central.sonatype.com/artifact/io.github.rohit-walia/playwright");

  //remember to close the resources when you are finished with them!
  PlaywrightManager.close(browserContext1);
  PlaywrightManager.close(browserContext2);
  PlaywrightManager.close(browser);
  PlaywrightManager.close(playwright);
}

Running in parallel

Playwright for Java out of the box is not thread safe. All Playwright resources are expected to be called on the same thread where the Playwright object was created or proper synchronization should be implemented to ensure only one thread calls Playwright resources at any given time. More details here

Below example demonstrates how this library can provide out of the box solution for achieving parallelism with Playwright.

void parallelismExample() {
  //create two Playwright connections
  Playwright playwright1 = PlaywrightManager.create(PlaywrightResource.PLAYWRIGHT);
  Playwright playwright2 = PlaywrightManager.create(PlaywrightResource.PLAYWRIGHT);

  //create two Browsers
  Browser browser1 = PlaywrightManager.create(PlaywrightResource.BROWSER, playwright1);
  Browser browser2 = PlaywrightManager.create(PlaywrightResource.BROWSER, playwright2);

  //create 1 BrowserContext for each Browser instance
  BrowserContext browserContext1 = PlaywrightManager.create(PlaywrightResource.BROWSER_CONTEXT, browser1);
  BrowserContext browserContext2 = PlaywrightManager.create(PlaywrightResource.BROWSER_CONTEXT, browser2);

  //remember to close the resources when you are finished with them!
  PlaywrightManager.close(playwright2);
  PlaywrightManager.close(browserContext2);
  PlaywrightManager.close(browser2);

  PlaywrightManager.close(browserContext1);
  PlaywrightManager.close(browser1);
  PlaywrightManager.close(playwright1);
}

Tools, libraries, and technologies

JUnit5

This project uses JUnit5 for testing. Tests can be found here. See here for more information on JUnit5.

Lombok

This project uses lombok to decrease boilerplate code. If you are using Intellij please install the Lombok Plugin. If you are using Eclipse STS follow the instructions here. If you are using another IDE you can see if it is supported on the Lombok website here.

Failsafe

This project is leveraging the Failsafe Helper library for its error handling, retry and fallback capabilities.

Jackson

This project is leveraging the Jackson Helper library for its convenient serialization and deserialization capabilities.

Code Quality

As part of the build, there are several code quality checks running against the code base. All code quality files can be found in the root of the project under the codequality directory.

About

This library is dedicated for testing projects utilizing Microsoft's Playwright for Java automation tool. It provides easy way to manage Playwright resources in a thread safe manner.

Topics

Resources

License

Stars

Watchers

Forks

Languages