-
-
Notifications
You must be signed in to change notification settings - Fork 342
feat: Add Playwright module #1288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
HofmeisterAn
merged 19 commits into
testcontainers:develop
from
alimahboubi:feature/PlaywrightModule
Nov 7, 2025
Merged
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
1586132
Add Testcontainers.Playwright library
alimahboubi f7e2e31
Add Playwright test setup and initial tests
alimahboubi 9a0800a
Update xunit package reference to latest version
alimahboubi c4bea33
Refactor test project settings
alimahboubi 70018cb
Add missing System and threading imports
alimahboubi 3733a88
Refactor usings in test files
alimahboubi b93c4a6
Merge remote-tracking branch 'origin/develop' into feature/Playwright…
alimahboubi 6c55ebf
Fix merge conflicts
alimahboubi f805a4f
Add Playwright test to CiCD
alimahboubi 26c6749
Merge branch 'develop' into feature/PlaywrightModule
alimahboubi f1e6d08
Merge remote-tracking branch 'origin/develop' into fork/alimahboubi/f…
HofmeisterAn b877abc
chore: Update project configurations
HofmeisterAn fc50cad
fix: Implement XunitV3 interface
HofmeisterAn 2e037be
chore: Align with the WebDriver module
HofmeisterAn 2534b90
chore: Remove BOM
HofmeisterAn e2effe8
docs: Add Playwright module docs
HofmeisterAn d7ce6ac
chore: Add some more info
HofmeisterAn 3d1d913
chore: Use MS image
HofmeisterAn 1a29df6
Merge branch 'develop' into feature/PlaywrightModule
HofmeisterAn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| # Playwright | ||
|
|
||
| [Playwright](https://playwright.dev/) is a framework for web testing and automation. It allows testing across all modern rendering engines including Chromium, WebKit, and Firefox with a single API. This module provides pre-configured browser containers for automated testing. | ||
|
|
||
| Add the following dependency to your project file: | ||
|
|
||
| ```shell title="NuGet" | ||
| dotnet add package Testcontainers.Playwright | ||
| ``` | ||
|
|
||
| You can start a Playwright container instance from any .NET application. To create and start a container instance with the default configuration, use the module-specific builder as shown below: | ||
|
|
||
| === "Start a Playwright container" | ||
| ```csharp | ||
| var playwrightContainer = new PlaywrightBuilder().Build(); | ||
| await playwrightContainer.StartAsync(); | ||
| ``` | ||
|
|
||
| This example uses xUnit.net's `IAsyncLifetime` interface to manage the lifecycle of the container. The container is started in the `InitializeAsync` method before the test method runs, ensuring that the environment is ready for testing. After the test completes, the container is removed in the `DisposeAsync` method. | ||
|
|
||
| This example demonstrates the Playwright container accessing a web site running inside another container (using the [`testcontainers/helloworld`](https://github.com/testcontainers/helloworld) image). Both containers are assigned to a shared network (see the [Network configuration](#network-configuration) section) to enable communication between them. | ||
|
|
||
| === "Usage Example" | ||
| ```csharp | ||
| --8<-- "tests/Testcontainers.Playwright.Tests/PlaywrightContainerTest.cs:UsePlaywrightContainer" | ||
| ``` | ||
|
|
||
| The test example uses the following NuGet dependencies: | ||
|
|
||
| === "Package References" | ||
| ```xml | ||
| --8<-- "tests/Testcontainers.Playwright.Tests/Testcontainers.Playwright.Tests.csproj:PackageReferences" | ||
| ``` | ||
|
|
||
| To execute the tests, use the command `dotnet test` from a terminal. | ||
|
|
||
| --8<-- "docs/modules/_call_out_test_projects.txt" | ||
|
|
||
| ## Network configuration | ||
|
|
||
| The Playwright container is configured with a network that can be shared with other containers. This is useful when testing applications that need to communicate with other services. Use the `GetNetwork()` method to access the container's network: | ||
|
|
||
| ```csharp | ||
| var helloWorldContainer = new ContainerBuilder() | ||
| .WithNetwork(playwrightContainer.GetNetwork()) | ||
| .Build(); | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| root = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| namespace Testcontainers.Playwright; | ||
|
|
||
| /// <inheritdoc cref="ContainerBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" /> | ||
| [PublicAPI] | ||
| public sealed class PlaywrightBuilder : ContainerBuilder<PlaywrightBuilder, PlaywrightContainer, PlaywrightConfiguration> | ||
| { | ||
| public const string PlaywrightNetworkAlias = "standalone-container"; | ||
|
|
||
| public const string PlaywrightImage = "mcr.microsoft.com/playwright:v1.55.1"; | ||
|
|
||
| public const ushort PlaywrightPort = 8080; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="PlaywrightBuilder" /> class. | ||
| /// </summary> | ||
| public PlaywrightBuilder() | ||
| : this(new PlaywrightConfiguration()) | ||
| { | ||
| DockerResourceConfiguration = Init().DockerResourceConfiguration; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="PlaywrightBuilder" /> class. | ||
| /// </summary> | ||
| /// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
| private PlaywrightBuilder(PlaywrightConfiguration resourceConfiguration) | ||
| : base(resourceConfiguration) | ||
| { | ||
| DockerResourceConfiguration = resourceConfiguration; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override PlaywrightConfiguration DockerResourceConfiguration { get; } | ||
|
|
||
| /// <inheritdoc /> | ||
| public override PlaywrightContainer Build() | ||
| { | ||
| Validate(); | ||
| return new PlaywrightContainer(DockerResourceConfiguration); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override PlaywrightBuilder Init() | ||
| { | ||
| return base.Init() | ||
| .WithImage(PlaywrightImage) | ||
| .WithNetwork(new NetworkBuilder().Build()) | ||
| .WithNetworkAliases(PlaywrightNetworkAlias) | ||
| .WithPortBinding(PlaywrightPort, true) | ||
| .WithEntrypoint("/bin/sh", "-c") | ||
| // Extract the Playwright version from the container at startup. | ||
| .WithCommand("npx -y playwright@$(sed --quiet 's/.*\\\"driverVersion\\\": *\"\\([^\"]*\\)\".*/\\1/p' ms-playwright/.docker-info) run-server --port " + PlaywrightPort) | ||
| .WithWaitStrategy(Wait.ForUnixContainer().UntilMessageIsLogged("Listening on ws://localhost:8080/")); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override PlaywrightBuilder Clone(IResourceConfiguration<CreateContainerParameters> resourceConfiguration) | ||
| { | ||
| return Merge(DockerResourceConfiguration, new PlaywrightConfiguration(resourceConfiguration)); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override PlaywrightBuilder Clone(IContainerConfiguration resourceConfiguration) | ||
| { | ||
| return Merge(DockerResourceConfiguration, new PlaywrightConfiguration(resourceConfiguration)); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override PlaywrightBuilder Merge(PlaywrightConfiguration oldValue, PlaywrightConfiguration newValue) | ||
| { | ||
| return new PlaywrightBuilder(new PlaywrightConfiguration(oldValue, newValue)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| namespace Testcontainers.Playwright; | ||
|
|
||
| /// <inheritdoc cref="ContainerConfiguration" /> | ||
| [PublicAPI] | ||
| public sealed class PlaywrightConfiguration : ContainerConfiguration | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="PlaywrightConfiguration" /> class. | ||
| /// </summary> | ||
| public PlaywrightConfiguration() | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="PlaywrightConfiguration" /> class. | ||
| /// </summary> | ||
| /// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
| public PlaywrightConfiguration(IResourceConfiguration<CreateContainerParameters> resourceConfiguration) | ||
| : base(resourceConfiguration) | ||
| { | ||
| // Passes the configuration upwards to the base implementations to create an updated immutable copy. | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="PlaywrightConfiguration" /> class. | ||
| /// </summary> | ||
| /// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
| public PlaywrightConfiguration(IContainerConfiguration resourceConfiguration) | ||
| : base(resourceConfiguration) | ||
| { | ||
| // Passes the configuration upwards to the base implementations to create an updated immutable copy. | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="PlaywrightConfiguration" /> class. | ||
| /// </summary> | ||
| /// <param name="resourceConfiguration">The Docker resource configuration.</param> | ||
| public PlaywrightConfiguration(PlaywrightConfiguration resourceConfiguration) | ||
| : this(new PlaywrightConfiguration(), resourceConfiguration) | ||
| { | ||
| // Passes the configuration upwards to the base implementations to create an updated immutable copy. | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="PlaywrightConfiguration" /> class. | ||
| /// </summary> | ||
| /// <param name="oldValue">The old Docker resource configuration.</param> | ||
| /// <param name="newValue">The new Docker resource configuration.</param> | ||
| public PlaywrightConfiguration(PlaywrightConfiguration oldValue, PlaywrightConfiguration newValue) | ||
| : base(oldValue, newValue) | ||
| { | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| namespace Testcontainers.Playwright; | ||
|
|
||
| /// <inheritdoc cref="DockerContainer" /> | ||
| [PublicAPI] | ||
| public sealed class PlaywrightContainer : DockerContainer | ||
| { | ||
| private readonly PlaywrightConfiguration _configuration; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="PlaywrightContainer" /> class. | ||
| /// </summary> | ||
| /// <param name="configuration">The container configuration.</param> | ||
| public PlaywrightContainer(PlaywrightConfiguration configuration) | ||
| : base(configuration) | ||
| { | ||
| _configuration = configuration; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the Playwright connection string. | ||
| /// </summary> | ||
| /// <returns>The Playwright connection string.</returns> | ||
| public string GetConnectionString() | ||
| { | ||
| return new UriBuilder("ws", Hostname, GetMappedPublicPort(PlaywrightBuilder.PlaywrightPort)).ToString(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the Playwright network. | ||
| /// </summary> | ||
| /// <returns>The Playwright network.</returns> | ||
| public INetwork GetNetwork() | ||
| { | ||
| return _configuration.Networks.Single(); | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/Testcontainers.Playwright/Testcontainers.Playwright.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <TargetFrameworks>net8.0;net9.0;netstandard2.0;netstandard2.1</TargetFrameworks> | ||
| <LangVersion>latest</LangVersion> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <PackageReference Include="JetBrains.Annotations" VersionOverride="2023.3.0" PrivateAssets="All"/> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <ProjectReference Include="../Testcontainers/Testcontainers.csproj"/> | ||
| </ItemGroup> | ||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| global using System; | ||
| global using System.Linq; | ||
| global using Docker.DotNet.Models; | ||
| global using DotNet.Testcontainers.Builders; | ||
| global using DotNet.Testcontainers.Configurations; | ||
| global using DotNet.Testcontainers.Containers; | ||
| global using DotNet.Testcontainers.Images; | ||
| global using DotNet.Testcontainers.Networks; | ||
| global using JetBrains.Annotations; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| root = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ubuntu-24.04 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.