openAPISwaggerUI–part 4–adding tests

This is how I made

https://nuget.org/packages/OpenAPISwaggerUI in order to see the UI for an ASP.NET 9 project.

And hey, if you have any feedback, don’t be shy! Drop by https://github.com/ignatandrei/openAPISwaggerUI/ and let me know.

For testing, I have created a new ASP.NET Core 9 project that can be used to run

app.MapOpenApi();
app.UseOpenAPISwaggerUI();

public partial class Program { }

And I want to see that /swagger endpoint exists . And more, to have pictures with the different UIs.

The WebApplicationFactory is used to create a test server that can be used to test the application. But I need more – to have a real server that can be used to test the application.

I have found this link that show how to start Kestrel server in a test project : https://danieldonbavand.com/2022/06/13/using-playwright-with-the-webapplicationfactory-to-test-a-blazor-application/

And now the code for having the pre-requisites for testing is as easy as pie.

static CustomWebApplicationFactory factory;
[AssemblyInitialize]
public static void AssemblyInit(TestContext context)
{
    
    var exitCode = Microsoft.Playwright.Program.Main(new[] { "install" });
    if (exitCode != 0)
    {
        throw new Exception($"Playwright exited with code {exitCode}");
    }
    factory = new ();
    var url = factory.ServerAddress;
    Console.WriteLine($"Server address: {url}");
}

And we can test the weatherforecast endpoint – because who doesn’t love knowing the weather, right?

[TestMethod]
public async Task TestWeHaveRealWebServer()
{
    
    using var client = factory.CreateDefaultClient();
    var response = await client.GetAsync("/weatherforecast");
    response.EnsureSuccessStatusCode();
    var baseAddress = factory.ServerAddress;
    if(!baseAddress.EndsWith("/"))
    {
        baseAddress += "/";    
    }
    using var playwright = await Playwright.CreateAsync();
    var request = await playwright.APIRequest.NewContextAsync();
        
    var response1 = await request.GetAsync(baseAddress+"weatherforecast");
    Assert.IsTrue(response1.Ok);
}

And to test the endpoints for my library , OpenAPISwaggerUI

[TestMethod]
public async Task TestIntegrationWorks()
{

    var baseAddress = factory.ServerAddress;
    if (!baseAddress.EndsWith("/"))
    {
        baseAddress += "/";
    }
    using var playwright = await Playwright.CreateAsync();
    var request = await playwright.APIRequest.NewContextAsync();

    await using var browser = await playwright.Chromium.LaunchAsync();
    var context = await browser.NewContextAsync(new()
    {
        //RecordVideoDir = curDirVideos
    });
    var page = await browser.NewPageAsync();
    var pageSwagger = await page.GotoAsync(baseAddress + "swagger");
    await page.ScreenshotAsync(new PageScreenshotOptions { Path = "swagger.png" });
    var content= await page.ContentAsync();
    var hrefs = await page.Locator("a").AllAsync();
    
    Assert.IsTrue(hrefs.Count > 0);
    foreach (var li in hrefs)
    {
        var text= await li.TextContentAsync();
        var href = await li.GetAttributeAsync("href");
        ArgumentNullException.ThrowIfNull(href);
        if(href.StartsWith("/"))
        {
            href =  href[1..];
        }
        var pageNew = await browser.NewPageAsync();
        await pageNew.GotoAsync(baseAddress+ href);
        await pageNew.WaitForLoadStateAsync(LoadState.NetworkIdle);
        await pageNew.ScreenshotAsync(new PageScreenshotOptions { Path = $"{text}.png" });


    }
}