Join our Discord Server
Adesoji Alu Adesoji brings a proven ability to apply machine learning(ML) and data science techniques to solve real-world problems. He has experience working with a variety of cloud platforms, including AWS, Azure, and Google Cloud Platform. He has a strong skills in software engineering, data science, and machine learning. He is passionate about using technology to make a positive impact on the world.

Testcontainers and Playwright

4 min read

Welcome to our comprehensive guide on Testcontainers-Playwright, a powerful library that integrates Testcontainers with Playwright for seamless browser automation and testing. Whether you’re a seasoned developer or just getting started with automated testing, this tool can significantly enhance your workflow by eliminating the need to install Playwright browsers locally.

What is Testcontainers-Playwright?

Testcontainers-Playwright is a Java library that provides a Testcontainers-based container for Playwright. It allows you to run browser automation and testing without the hassle of installing Playwright browsers on your local machine. By leveraging containers, you can ensure a consistent testing environment across different machines and CI/CD pipelines.

Why Choose Testcontainers-Playwright?

  • Isolation: Each test runs in its own container, preventing environment-related issues.
  • Scalability: Easily scale your tests across multiple containers.
  • Consistency: Ensure that all developers and CI environments use the same browser versions.

Key Features

  • Multi-Browser Support: Compatible with Chromium, Firefox, and WebKit browsers.
  • Lifecycle Management: Automatically manages the lifecycle of Playwright containers and browser instances.
  • Thread-Safe API: Provides a thread-bound wrapper around the Playwright API.
  • JUnit 5 Integration: Offers a seamless integration with your tests via a JUnit 5 extension.

Limitations

While Testcontainers-Playwright offers numerous advantages, it’s essential to be aware of its current limitations:

  1. File System Operations: Features like HAR dumps and tracing are not supported. For these capabilities, consider contributing or requesting this feature in the repository.
  2. Custom Browsers: Custom browser support isn’t available yet. If you need this feature, please create an issue or vote for an existing one.
  3. Version Compatibility: Playwright requires the client and server to be on the same minor version for remote execution. Mismatches will default to a local browser, and warnings will appear in the logs. Ensure compatibility by aligning your classpath version with the container version.

Compatibility

Ensure that your project meets the following compatibility requirements:

Getting Started

Installation

To integrate Testcontainers-Playwright into your project, add it as a dependency. Here’s how to do it with Maven and Gradle:

Maven:

<dependency>
<groupId>io.orange-buffalo</groupId>
<artifactId>testcontainers-playwright</artifactId>
<scope>test</scope>
<version>0.11.11</version>
</dependency>

Gradle:


testImplementation("io.orangebuffalo:testcontainers-playwright:0.11.11")

Basic Usage

Once installed, you can create a PlaywrightContainer instance and utilize the provided API to access browser instances.


import io.orangebuffalo.testcontainers.playwright.PlaywrightApi;

import io.orangebuffalo.testcontainers.playwright.PlaywrightContainer;

import org.junit.jupiter.api.BeforeEach;

import org.junit.jupiter.api.Test;

import org.junit.jupiter.api.extension.RegisterExtension;

class MyPlaywrightTests {

@RegisterExtension

public static final PlaywrightContainer playwrightContainer = new PlaywrightContainer();

private PlaywrightApi playwrightApi;

@BeforeEach

void setUp() {

playwrightApi = playwrightContainer.getPlaywrightApi();

}

@Test

void myTest() {

Browser browser = playwrightApi.chromium();

Page page = browser.newPage();

page.navigate("https://www.example.com");

// Perform test actions and assertions here

}

}

Advanced Usage

Using Playwright JUnit 5 Extension

The Playwright JUnit 5 Extension simplifies the integration by allowing direct injection of PlaywrightApi, Page, and BrowserContext instances into your test methods.


import io.orangebuffalo.testcontainers.playwright.junit.PlaywrightExtension;

import com.microsoft.playwright.Page;

import org.junit.jupiter.api.Test;

import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(PlaywrightExtension.class)

class MyTests {

@Test

void testWithInjectedPage(@RequiresChromium Page page) {

page.navigate("https://example.com");

// Perform your test actions

}

}

Configuring the Extension

Customize the extension using the @PlaywrightConfig annotation and the PlaywrightConfigurer interface. This allows you to set default URLs, connect containers to specific networks, and more.


import io.orangebuffalo.testcontainers.playwright.junit.*;

@PlaywrightConfig(configurer = MyPlaywrightConfigurer.class)

@ExtendWith(PlaywrightExtension.class)

class MyTests {

@Test

void testWithInjectedPage(Page page) {

// Perform your test actions

}

}

class MyPlaywrightConfigurer implements PlaywrightConfigurer {

@Override

public NewContextOptions createBrowserContextOptions() {

return new NewContextOptions().setBaseURL("https://example.com");

}

}

Local Development Experience

Enhance your local development by replacing the PlaywrightApi with a custom implementation using the createPlaywrightApiProvider method. This allows you to run tests with local browsers in non-headless mode, providing a visual overview during test execution.



import io.orangebuffalo.testcontainers.playwright.junit.*;

import com.microsoft.playwright.*;

@ExtendWith(PlaywrightExtension.class)

class MyLocalTests {

@Test

void testWithLocalBrowser(Page page) {

page.navigate("https://example.com");

// Perform your test actions

}

}

Best Practices

  1. Manage Dependencies: Regularly update your dependencies to benefit from the latest features and security patches.
  2. Handle Derived Instances: If you create instances derived from injected primitives, ensure they are closed manually to prevent resource leaks.
  3. Consistent Configuration: Use custom annotations to apply consistent configurations across multiple test classes.
  4. Monitor Logs: Pay attention to logs for any version mismatches or warnings to maintain compatibility.

Conclusion

Testcontainers-Playwright is a robust solution for developers seeking efficient and isolated browser automation and testing environments. By leveraging containers, it ensures consistency across different environments, simplifies dependency management, and integrates seamlessly with JUnit 5. While it has some limitations, its benefits make it a valuable addition to any testing toolkit.
Furthermore, to learn more about testcontainers, kindly Check out the Awesome testcontainer repository.
Ready to streamline your browser automation? Check out the Testcontainers-Playwright repository and start enhancing your testing workflow today!

References

If you found this guide helpful, feel free to share it with your network or leave a comment below! For more updates and tutorials, subscribe to our newsletter.

Have Queries? Join https://launchpass.com/collabnix

Adesoji Alu Adesoji brings a proven ability to apply machine learning(ML) and data science techniques to solve real-world problems. He has experience working with a variety of cloud platforms, including AWS, Azure, and Google Cloud Platform. He has a strong skills in software engineering, data science, and machine learning. He is passionate about using technology to make a positive impact on the world.
Join our Discord Server
Index