blob: de316dee6e78c274992d755a53605f286bcb2c86 (
plain) (
blame)
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
|
using System.Net;
using System.Net.Sockets;
using Microsoft.AspNetCore.Builder;
using Microsoft.Playwright;
using Xunit;
using Program = IOL.GreatOffice.Api.Program;
namespace IOL.GreatOffice.IntegrationTests.Helpers;
public class WebServerFixture : IAsyncLifetime, IDisposable
{
private readonly WebApplication Host;
private IPlaywright Playwright { get; set; }
public IBrowser Browser { get; private set; }
public string BaseUrl { get; } = $"https://localhost:{GetRandomUnusedPort()}";
public WebServerFixture()
{
Host = Program.CreateWebApplication(Program.CreateAppBuilder(default));
}
public async Task InitializeAsync()
{
Playwright = await Microsoft.Playwright.Playwright.CreateAsync();
Browser = await Playwright.Chromium.LaunchAsync();
await Host.StartAsync();
}
public async Task DisposeAsync()
{
await Host.StopAsync();
await Host.DisposeAsync();
Playwright?.Dispose();
}
public void Dispose()
{
Host.StopAsync();
Host.DisposeAsync();
Playwright?.Dispose();
GC.SuppressFinalize(this);
}
private static int GetRandomUnusedPort()
{
var listener = new TcpListener(IPAddress.Any, 0);
listener.Start();
var port = ((IPEndPoint)listener.LocalEndpoint).Port;
listener.Stop();
return port;
}
}
|