PlaywrightFixture.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. // Change this if needed
  7. private const string _dockerImage = "rackpeek:ci";
  8. private IContainer _container = default!;
  9. private IPlaywright _playwright = default!;
  10. public IBrowser Browser { get; private set; } = default!;
  11. public string BaseUrl { get; private set; } = default!;
  12. public async Task InitializeAsync() {
  13. _container = new ContainerBuilder(_dockerImage)
  14. .WithPortBinding(8080, true) // random host port
  15. .WithWaitStrategy(
  16. Wait.ForUnixContainer()
  17. .UntilHttpRequestIsSucceeded(r => r
  18. .ForPort(8080)
  19. .ForPath("/")))
  20. .Build();
  21. await _container.StartAsync();
  22. var mappedPort = _container.GetMappedPublicPort(8080);
  23. BaseUrl = $"http://127.0.0.1:{mappedPort}";
  24. Console.WriteLine($"RackPeek running at: {BaseUrl}");
  25. _playwright = await Playwright.CreateAsync();
  26. Browser = await _playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions {
  27. Headless = true,
  28. SlowMo = 400,
  29. //Headless = false,
  30. //SlowMo = 1500,
  31. Args = new[]
  32. {
  33. "--disable-dev-shm-usage",
  34. "--no-sandbox"
  35. }
  36. });
  37. Assertions.SetDefaultExpectTimeout(15000);
  38. }
  39. public async Task DisposeAsync() {
  40. if (Browser != null)
  41. await Browser.DisposeAsync();
  42. _playwright?.Dispose();
  43. if (_container != null)
  44. await _container.DisposeAsync();
  45. }
  46. }