/EndToEndBlazorWasmPlaywrightTesting

Attempt to get Blazor Wasm, WebAPI, Playwright all to play nicely together in memory for testing.

Primary LanguageHTML

EndToEndBlazorWasmPlaywrightTesting

An attempt to get Blazor WASM, WebAPI, and Playwright to all play nicely together in memory for testing.

This initial commit demonstrates testing in memory using Playwright, against a Blazor Server front end, a separate WebAPI and NUnit test project. The WebAPI is created using WebApplicationFactory, this allows you to inject mocked databases, storage and other dependencies. The HttpClient provided by the WebApplicationFramework is then passed to the Blazor Server front end to make use of the Mocked services.

The goal of this project is to reproduce the same in-memory testing for a Blazor WASM front end instead of the server. The WASM version will be tracked on branch.

#Project Structure

image

  • BlazorApp - Blazor Server Application (Based on Max Schmitt's example https://github.com/mxschmitt/razor-playwright-dotnet-example)
  • BlazorWasmApp - (Not currently used on this branch) Stand alone Blazor Wasm application. - Target to test in memory.
  • NUnitTests - Adapted version of Max Schmitt's example https://github.com/mxschmitt/razor-playwright-dotnet-example
    • The key here is that the UnitTest1.cs inherits the BlazorTests.cs and starts the server in OneTimeSetUp. The Utilimiate goal would be starting both hosts (WebAPI & FrontEnd) in an un-namespaced OneTimeSetUp to cover all tests and reuse the hosts between testing.
  • WebAPI
    • The intention will be to mock services like Databases, storage, vaults etc. so that the in-memory tests execute very fast.

Test Execution.

If you run both tests "CountTest" & "FetchDataTest" on this main branch they should pass.

image

There is a runsettings file contained in the root of the directory, if you select this file in Test Explorer you will run the tests in headless mode, allowing you to see the tests executing in the browser.

image

If you put a breakpoint on WebAPI > Controllers > WeatherForecastController at link #24 when the "FetchDataTest" executes, if you inspect the Http Request you will notice the host is localhost without any port, this shows that the HttpClient from the WebApplicationFramework is working as expected.

image

#Now, The goal is reproduce this for a WASM application.