-
-
Notifications
You must be signed in to change notification settings - Fork 353
Expand file tree
/
Copy pathPlaywrightContainerTest.cs
More file actions
89 lines (70 loc) · 2.93 KB
/
Copy pathPlaywrightContainerTest.cs
File metadata and controls
89 lines (70 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
namespace Testcontainers.Playwright;
public abstract class PlaywrightContainerTest : IAsyncLifetime
{
private readonly Uri _helloWorldBaseAddress = new UriBuilder(Uri.UriSchemeHttp, "hello-world-container", 8080).Uri;
private readonly IContainer _helloWorldContainer;
private readonly PlaywrightContainer _playwrightContainer;
private PlaywrightContainerTest(PlaywrightContainer playwrightContainer)
{
_helloWorldContainer = new ContainerBuilder(CommonImages.HelloWorld)
.WithNetwork(playwrightContainer.GetNetwork())
.WithNetworkAliases(_helloWorldBaseAddress.Host)
.WithPortBinding(_helloWorldBaseAddress.Port, true)
.WithWaitStrategy(Wait.ForUnixContainer().UntilHttpRequestIsSucceeded(request =>
request.ForPath("/").ForPort(Convert.ToUInt16(_helloWorldBaseAddress.Port))))
.Build();
_playwrightContainer = playwrightContainer;
}
public async ValueTask InitializeAsync()
{
await _playwrightContainer.StartAsync()
.ConfigureAwait(false);
await _helloWorldContainer.StartAsync()
.ConfigureAwait(false);
}
public async ValueTask DisposeAsync()
{
await DisposeAsyncCore()
.ConfigureAwait(false);
GC.SuppressFinalize(this);
}
// # --8<-- [start:UsePlaywrightContainer]
[Fact]
[Trait(nameof(DockerCli.DockerPlatform), nameof(DockerCli.DockerPlatform.Linux))]
public async Task HeadingElementReturnsHelloWorld()
{
// Given
var playwright = await Microsoft.Playwright.Playwright.CreateAsync()
.ConfigureAwait(true);
var browser = await playwright.Chromium.ConnectAsync(_playwrightContainer.GetConnectionString())
.ConfigureAwait(true);
var page = await browser.NewPageAsync()
.ConfigureAwait(true);
// When
await page.GotoAsync(_helloWorldBaseAddress.ToString())
.ConfigureAwait(true);
var headingElement = await page.QuerySelectorAsync("h1")
.ConfigureAwait(true);
var headingElementText = await headingElement.InnerTextAsync()
.ConfigureAwait(true);
// Then
Assert.Equal("Hello world", headingElementText);
Assert.Equal(_playwrightContainer.GetConnectionString(), _playwrightContainer.GetConnectionString(ConnectionMode.Host));
}
// # --8<-- [end:UsePlaywrightContainer]
protected virtual async ValueTask DisposeAsyncCore()
{
await _helloWorldContainer.DisposeAsync()
.ConfigureAwait(false);
await _playwrightContainer.DisposeAsync()
.ConfigureAwait(false);
}
[UsedImplicitly]
public sealed class PlaywrightDefaultConfiguration : PlaywrightContainerTest
{
public PlaywrightDefaultConfiguration()
: base(new PlaywrightBuilder(TestSession.GetImageFromDockerfile()).Build())
{
}
}
}