PlaywrightFixture.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using DotNet.Testcontainers.Builders;
  2. using DotNet.Testcontainers.Containers;
  3. using Microsoft.Playwright;
  4. namespace Tests.E2e.Infra;
  5. public class PlaywrightFixture : IAsyncLifetime
  6. {
  7. // Change this if needed
  8. private const string DockerImage = "rackpeek:ci";
  9. private IContainer _container = default!;
  10. private IPlaywright _playwright = default!;
  11. public IBrowser Browser { get; private set; } = default!;
  12. public string BaseUrl { get; private set; } = default!;
  13. public async Task InitializeAsync()
  14. {
  15. _container = new ContainerBuilder(DockerImage)
  16. .WithPortBinding(8080, true) // random host port
  17. .WithWaitStrategy(
  18. Wait.ForUnixContainer()
  19. .UntilHttpRequestIsSucceeded(r => r
  20. .ForPort(8080)
  21. .ForPath("/")))
  22. .Build();
  23. await _container.StartAsync();
  24. var mappedPort = _container.GetMappedPublicPort(8080);
  25. BaseUrl = $"http://127.0.0.1:{mappedPort}";
  26. Console.WriteLine($"RackPeek running at: {BaseUrl}");
  27. _playwright = await Playwright.CreateAsync();
  28. Browser = await _playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
  29. {
  30. Headless = true,
  31. //Headless = false,
  32. SlowMo = 500,
  33. Args = new[]
  34. {
  35. "--disable-dev-shm-usage",
  36. "--no-sandbox"
  37. }
  38. });
  39. Assertions.SetDefaultExpectTimeout(15000);
  40. }
  41. public async Task DisposeAsync()
  42. {
  43. if (Browser != null)
  44. await Browser.DisposeAsync();
  45. _playwright?.Dispose();
  46. if (_container != null)
  47. await _container.DisposeAsync();
  48. }
  49. }