'Hot to debug playwright-dotnet tests with Microsoft.AspNetCore.TestHost

I'm trying to run Playwright locally by using debugging from xUnit and AspNetCore.TestHost and even if when I call the API endpoints the calls are successfully the SPA page is not loaded.

Are there any changes that I should do more to WebApplicationFactory ?

So far I've tried this approach :

CustomWebApplicationFactory.cs

public class CustomWebApplicationFactory : WebApplicationFactory<Startup>
    {
        protected override IHostBuilder CreateHostBuilder()
        {
            return new HostBuilder()
                .ConfigureHostConfiguration(config =>
                {
                    // Make UseStaticWebAssets work
                    var applicationPath = typeof(Startup).Assembly.Location;
                    var applicationDirectory = Path.GetDirectoryName(applicationPath);
                    var name = Path.ChangeExtension(applicationPath, ".StaticWebAssets.xml");

                    var inMemoryConfiguration = new Dictionary<string, string>
                    {
                        [WebHostDefaults.StaticWebAssetsKey] = name,
                    };

                    config.AddInMemoryCollection(inMemoryConfiguration);
                })
                .ConfigureWebHost(webHostBuilder => webHostBuilder
                    .UseKestrel()
                    .UseSolutionRelativeContentRoot(typeof(Startup).Assembly.GetName().Name)
                    .UseStaticWebAssets()
                    .UseStartup<Startup>());
        }
    }

PlaywrightFixture.cs

public class PlaywrightFixture
    {
        public PlaywrightFixture()
        {
            this.Playwright = Microsoft.Playwright.Playwright.CreateAsync().GetAwaiter().GetResult();

            var options = new BrowserTypeLaunchOptions
            {
                Headless = false,
            };

            this.Browser = this.Playwright.Chromium.LaunchAsync(options).GetAwaiter().GetResult();
        }

        private IPlaywright Playwright { get; set; }

        public IBrowser Browser { get; private set; }
    }

UnitTest1.cs

public class UnitTest1 : IClassFixture<CustomWebApplicationFactory>, IClassFixture<PlaywrightFixture>
    {
        private readonly HttpClient _client;
        private readonly PlaywrightFixture _playwrightFixture;
        private readonly CustomWebApplicationFactory _factory;

        public UnitTest1(CustomWebApplicationFactory factory, PlaywrightFixture playwrightFixture)
        {
            _client = factory?.CreateClient() ?? throw new ArgumentNullException(nameof(factory));
            _factory = factory ?? throw new ArgumentNullException(nameof(factory));

            _playwrightFixture = playwrightFixture ?? throw new ArgumentNullException(nameof(playwrightFixture));
        }

        [Fact]
        public async Task Test1()
        {

            var apiCallSimulation = await _client.GetAsync(@"\WeatherForecast");
            var contentResult = await apiCallSimulation.Content.ReadAsStringAsync();

            var page = await _playwrightFixture.Browser.NewPageAsync();
            var result = await page.GotoAsync($"https://localhost:5001");
        }
    }

The apiCallSimulation receives a 200 and the call is made to the controller but If I try to access the link I'm unable to do it.

Is there a way to achieve to run test locally ?? I know there is the solution to start manually the project and give the url, but I would like to do it as it goes with integration tests.

I have in mind one more idea to use Pragma marks and if is in Development to run dotnet .dll but then I might have an issue with Azure DevOps with CI/CD



Solution 1:[1]

I'm not sure if the TestServer is really a 'server' listening on a port or just in memory access to server without the tcp/ip part. If so, you don't have how to make playrigth use the same specific http client made for the in-memory server, because the httpclient and TestServer are using fake objects instead of real network one to provide fast tests

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1