-
-
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 13 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
Some comments aren't visible on the classic Files Changed page.
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
| 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,52 @@ | ||
| namespace Testcontainers.Playwright; | ||
|
|
||
| /// <summary> | ||
| /// Playwright browser configuration. | ||
| /// </summary> | ||
| [PublicAPI] | ||
| public readonly struct PlaywrightBrowser | ||
| { | ||
| /// <summary> | ||
| /// Gets the Playwright standalone Chrome configuration. | ||
| /// </summary> | ||
| public static readonly PlaywrightBrowser Chrome = new PlaywrightBrowser("jacoblincool/playwright:chrome-server"); | ||
|
|
||
| /// <summary> | ||
| /// Gets the Playwright standalone Chromium configuration. | ||
| /// </summary> | ||
| public static readonly PlaywrightBrowser Chromium = new PlaywrightBrowser("jacoblincool/playwright:chromium-server"); | ||
|
|
||
| /// <summary> | ||
| /// Gets the Playwright standalone Firefox configuration. | ||
| /// </summary> | ||
| public static readonly PlaywrightBrowser Firefox = new PlaywrightBrowser("jacoblincool/playwright:firefox-server"); | ||
|
|
||
| /// <summary> | ||
| /// Gets the Playwright standalone Edge configuration. | ||
| /// </summary> | ||
| public static readonly PlaywrightBrowser Edge = new PlaywrightBrowser("jacoblincool/playwright:msedge-server"); | ||
|
|
||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="PlaywrightBrowser" /> struct. | ||
| /// </summary> | ||
| /// <param name="image">The Playwright standalone Docker image.</param> | ||
| public PlaywrightBrowser(string image) | ||
| : this(new DockerImage(image)) | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="PlaywrightBrowser" /> struct. | ||
| /// </summary> | ||
| /// <param name="image">The Playwright standalone Docker image.</param> | ||
| public PlaywrightBrowser(IImage image) | ||
| { | ||
| Image = image; | ||
| } | ||
| /// <summary> | ||
| /// Gets the Playwright standalone Docker image. | ||
| /// </summary> | ||
| [NotNull] | ||
| public IImage Image { get; } | ||
| } |
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,98 @@ | ||
| namespace Testcontainers.Playwright; | ||
|
|
||
| /// <inheritdoc cref="PlaywrightBuilder{TBuilderEntity, TContainerEntity, TConfigurationEntity}" /> | ||
| /// <remarks> | ||
| /// Find further information about the Playwright image, here: https://playwright.dev/dotnet/docs/docker. | ||
| /// </remarks> | ||
| [PublicAPI] | ||
| public class PlaywrightBuilder : ContainerBuilder<PlaywrightBuilder, PlaywrightContainer, PlaywrightConfiguration> | ||
| { | ||
| private const ushort PlaywrightPort = 53333; | ||
| private const string PlaywrightEndpointPath = "/playwright"; | ||
|
|
||
| /// <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; } | ||
|
|
||
| public override PlaywrightContainer Build() | ||
| { | ||
| Validate(); | ||
| return new PlaywrightContainer(DockerResourceConfiguration); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override PlaywrightBuilder Init() | ||
| { | ||
| return base.Init() | ||
| .WithBrowser(PlaywrightBrowser.Chrome) | ||
| .WithEndpointPath(PlaywrightEndpointPath) | ||
| .WithBrowserPort(PlaywrightPort) | ||
| .WithWaitStrategy(Wait.ForUnixContainer() | ||
| .UntilMessageIsLogged($"ws://.*:{PlaywrightPort}{PlaywrightEndpointPath}")); | ||
| } | ||
|
|
||
| public PlaywrightBuilder WithBrowser(PlaywrightBrowser playwrightBrowser) | ||
| { | ||
| return WithImage(playwrightBrowser.Image); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Sets the BROWSER WS ENDPOINT. | ||
| /// </summary> | ||
| /// <param name="endpointPath">The BROWSER WS ENDPOINT.</param> | ||
| /// <returns>A configured instance of <see cref="PlaywrightBuilder" />.</returns> | ||
| public PlaywrightBuilder WithEndpointPath(string endpointPath) | ||
| { | ||
| return Merge(DockerResourceConfiguration, new PlaywrightConfiguration(endpoint: endpointPath)) | ||
| .WithEnvironment("BROWSER_WS_ENDPOINT", endpointPath); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Sets the BROWSER WS PORT. | ||
| /// </summary> | ||
| /// <param name="port">The BROWSER WS PORT.</param> | ||
| /// <returns>A configured instance of <see cref="PlaywrightBuilder" />.</returns> | ||
| public PlaywrightBuilder WithBrowserPort(int port, bool assignRandomHostPort=false) | ||
| { | ||
| return Merge(DockerResourceConfiguration, new PlaywrightConfiguration(port: port)) | ||
| .WithEnvironment("BROWSER_PORT", port.ToString()) | ||
| .WithPortBinding(port, assignRandomHostPort); | ||
| } | ||
HofmeisterAn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
|
|
||
| /// <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,70 @@ | ||
| namespace Testcontainers.Playwright; | ||
|
|
||
| /// <inheritdoc cref="PlaywrightConfiguration" /> | ||
| [PublicAPI] | ||
| public class PlaywrightConfiguration : ContainerConfiguration | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="PlaywrightConfiguration" /> class. | ||
| /// </summary> | ||
| /// <param name="endpoint">The Playwright endpoint.</param> | ||
| public PlaywrightConfiguration(string endpoint = null, | ||
| int? port = null) | ||
| { | ||
| Endpoint = endpoint; | ||
| Port = port; | ||
| } | ||
|
|
||
| /// <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) | ||
| { | ||
| Endpoint = BuildConfiguration.Combine(oldValue.Endpoint, newValue.Endpoint); | ||
| } | ||
HofmeisterAn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| /// <summary> | ||
| /// Gets the Playwright endpoint. | ||
| /// </summary> | ||
| public string Endpoint { get; } | ||
|
|
||
|
|
||
| /// <summary> | ||
| /// Gets the Playwright port. | ||
| /// </summary> | ||
| public int? Port { get; } | ||
| } | ||
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,14 @@ | ||
| namespace Testcontainers.Playwright; | ||
|
|
||
| /// <inheritdoc cref="DockerContainer" /> | ||
| [PublicAPI] | ||
| public class PlaywrightContainer : DockerContainer | ||
| { | ||
| /// <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) | ||
| { | ||
| } | ||
| } |
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,16 @@ | ||
| global using System; | ||
| global using System.IO; | ||
| global using System.Linq; | ||
| global using System.Net.Http; | ||
| global using System.Text.Json; | ||
| global using System.Threading; | ||
| global using System.Threading.Tasks; | ||
| global using Docker.DotNet.Models; | ||
| global using DotNet.Testcontainers; | ||
| 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; | ||
| global using Microsoft.Extensions.Logging.Abstractions; |
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 |
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,3 @@ | ||
| global using Xunit; | ||
| global using System; | ||
| global using System.Threading.Tasks; |
28 changes: 28 additions & 0 deletions
28
tests/Testcontainers.Playwright.Tests/PlaywrightContainerTest.cs
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,28 @@ | ||
| namespace Testcontainers.Playwright.Tests; | ||
|
|
||
| public class PlaywrightContainerTest : IClassFixture<TestInitializer> | ||
| { | ||
| private readonly Uri _helloWorldBaseAddress; | ||
|
|
||
| public PlaywrightContainerTest(TestInitializer testInitializer) | ||
| { | ||
| _helloWorldBaseAddress = testInitializer._helloWorldBaseAddress; | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task HeadingElementReturnsHelloWorld() | ||
| { | ||
| // Given | ||
| var playwright = await Microsoft.Playwright.Playwright.CreateAsync(); | ||
| var browser = await playwright.Chromium.ConnectAsync($"ws://localhost:63333/playwright"); | ||
| var page = await browser.NewPageAsync(); | ||
|
|
||
| // When | ||
| await page.GotoAsync(_helloWorldBaseAddress.ToString()); | ||
| var headingElement = await page.QuerySelectorAsync("h1"); | ||
| var headingElementText = await headingElement.InnerTextAsync(); | ||
|
|
||
| // Then | ||
| Assert.Equal("Hello world", headingElementText); | ||
HofmeisterAn marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
Oops, something went wrong.
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.