PlaywrightFixture.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. //Headless = false,
  29. SlowMo = 500,
  30. Args = new[]
  31. {
  32. "--disable-dev-shm-usage",
  33. "--no-sandbox"
  34. }
  35. });
  36. Assertions.SetDefaultExpectTimeout(15000);
  37. }
  38. public async Task DisposeAsync() {
  39. if (Browser != null)
  40. await Browser.DisposeAsync();
  41. _playwright?.Dispose();
  42. if (_container != null)
  43. await _container.DisposeAsync();
  44. }
  45. }