| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- using System.Text;
- using DotNet.Testcontainers.Builders;
- using DotNet.Testcontainers.Containers;
- using Microsoft.Playwright;
- namespace Tests.E2e.Infra;
- public class PlaywrightFixture : IAsyncLifetime {
- // Change this if needed
- private const string _dockerImage = "rackpeek:ci";
- private IContainer _container = default!;
- private IPlaywright _playwright = default!;
- public IBrowser Browser { get; private set; } = default!;
- public string BaseUrl { get; private set; } = default!;
- public async Task InitializeAsync() {
- _container = new ContainerBuilder(_dockerImage)
- .WithPortBinding(8080, true) // random host port
- .WithWaitStrategy(
- Wait.ForUnixContainer()
- .UntilHttpRequestIsSucceeded(r => r
- .ForPort(8080)
- .ForPath("/")))
- .Build();
- await _container.StartAsync();
- var mappedPort = _container.GetMappedPublicPort(8080);
- BaseUrl = $"http://127.0.0.1:{mappedPort}";
- Console.WriteLine($"RackPeek running at: {BaseUrl}");
- _playwright = await Playwright.CreateAsync();
- Browser = await _playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions {
- Headless = true,
- SlowMo = 400,
- //Headless = false,
- //SlowMo = 1500,
- Args = new[]
- {
- "--disable-dev-shm-usage",
- "--no-sandbox"
- }
- });
- Assertions.SetDefaultExpectTimeout(15000);
- }
- /// <summary>
- /// Replaces the container's config.yaml wholesale. Used to stage a damaged
- /// config, which cannot be produced through the UI (the editor validates
- /// before saving) but is exactly what an interrupted write leaves behind.
- /// </summary>
- public async Task WriteConfigAsync(string contents) {
- await _container.CopyAsync(
- Encoding.UTF8.GetBytes(contents),
- "/app/config/config.yaml");
- }
- public async Task<string> ReadConfigAsync() {
- var bytes = await _container.ReadFileAsync("/app/config/config.yaml");
- return Encoding.UTF8.GetString(bytes);
- }
- public async Task<(string Stdout, string Stderr)> ReadLogsAsync() => await _container.GetLogsAsync();
- public async Task DisposeAsync() {
- if (Browser != null)
- await Browser.DisposeAsync();
- _playwright?.Dispose();
- if (_container != null)
- await _container.DisposeAsync();
- }
- }
|