Updated readme for playwright tests

This commit is contained in:
Joe DeCock 2025-08-29 11:32:47 -05:00
parent 3ffd64e594
commit ead4f09669
2 changed files with 44 additions and 30 deletions

View file

@ -87,8 +87,8 @@ public class AppHostFixture : IAsyncLifetime
}
}
// Not running in ncrunch AND no service found running.
// So, create an AppHost that will be used for the duration of this test run.
// Not running in ncrunch AND no service found running.
// So, create an AppHost that will be used for the duration of this test run.
var appHost = await DistributedApplicationTestingBuilder
.CreateAsync<Hosts_AppHost>();
appHost.Configuration["DcpPublisher:RandomizePorts"] = "false";
@ -108,12 +108,12 @@ public class AppHostFixture : IAsyncLifetime
_app = await appHost.BuildAsync();
var resourceNotificationService = (await appHost.BuildAsync()).Services
var resourceNotificationService = _app.Services
.GetRequiredService<ResourceNotificationService>();
await (await appHost.BuildAsync()).StartAsync();
await _app.StartAsync();
// Wait for all the services so that their logs are mostly written.
// Wait for all the services so that their logs are mostly written.
foreach (var resource in AppHostServices.All)
{
@ -172,7 +172,7 @@ public class AppHostFixture : IAsyncLifetime
private void WriteLogs(string logMessage) => _activeWriter?.Invoke(logMessage);
/// <summary>
/// This method builds a http client.
/// This method builds an http client.
/// </summary>
/// <param name="clientName"></param>
/// <returns></returns>
@ -189,7 +189,7 @@ public class AppHostFixture : IAsyncLifetime
inner = new SocketsHttpHandler
{
// We need to disable cookies and follow redirects
// because we do this manually (see below).
// because we do this manually (see below).
UseCookies = false,
AllowAutoRedirect = false
};
@ -198,11 +198,11 @@ public class AppHostFixture : IAsyncLifetime
{
#if DEBUG_NCRUNCH
// This should not be reached for NCrunch because either the service is already running
// or the test base has thrown a SkipException.
// or the test base has thrown a SkipException.
throw new InvalidOperationException("This should not be reached in NCrunch");
#else
// If we're here, that means that we need to create a http client that's pointing to
// aspire.
// If we're here, that means that we need to create an http client that's pointing to
// aspire.
if (_app == null)
{
throw new NotSupportedException("App should not be null");
@ -212,15 +212,15 @@ public class AppHostFixture : IAsyncLifetime
baseAddress = client.BaseAddress;
// We can't directly use the HTTP Client, because we need cookie support, but if we
// enable that the cookies get shared across multiple requests
// enable that the cookies get shared across multiple requests
// https://github.com/dotnet/AspNetCore.Docs/issues/15848
// By wrapping the http client, then handling all the cookies
// ourselves, we bypass this problem.
// ourselves, we bypass this problem.
inner = new CloningHttpMessageHandler(client);
#endif
}
// Log every call that's made (including if it was part of a redirect).
// Log every call that's made (including if it was part of a redirect).
var loggingHandler =
new RequestLoggingHandler(
CreateLogger<RequestLoggingHandler>()
@ -232,15 +232,15 @@ public class AppHostFixture : IAsyncLifetime
// Manually take care of cookies (see reason why above)
var cookieHandler = new CookieHandler(loggingHandler, new CookieContainer());
// Follow redirects when needed.
// Follow redirects when needed.
var redirectHandler = new AutoFollowRedirectHandler(CreateLogger<AutoFollowRedirectHandler>())
{
InnerHandler = cookieHandler
};
// Return a http client that follows redirects, uses cookies and logs all requests.
// Return an http client that follows redirects, uses cookies and logs all requests.
// For aspire, this is needed otherwise cookies are shared, but it also
// gives a much clearer debug output (each request gets logged).
// gives a much clearer debug output (each request gets logged).
return new HttpClient(redirectHandler)
{
BaseAddress = baseAddress
@ -252,7 +252,7 @@ public class AppHostFixture : IAsyncLifetime
if (UsingAlreadyRunningInstance)
{
// An aspire host is already found (likely was started manually)
// so build a http client that directly points to this host.
// so build an http client that directly points to this host.
var url = clientName switch
{
AppHostServices.Bff => "https://localhost:5002",

View file

@ -1,20 +1,34 @@
# Hosts.Tests
This project contains the integration tests for the various hosts. This host is built using Aspnet Aspire.
This project contains integration tests for the various hosts. These tests run the aspire host in
bff/hosts/Hosts.AppHost which hosts all the BFF host projects and then exercises them using playwright.
The actual tests have been written in such a way that they only need a HTTP client to work. If you don't do anything,
then the system will start an aspire test host, run the tests, then kill the aspire host again.
Howeve,if you want faster feedback, you can also start the aspire host yourself. To avoid collisions between the running
application and the testing framework trying to rebuild all libraries, I recommend running the system in release mode.
## Playwright Setup
To run the tests, you need to have playwright installed. You can do this by running the following command:
```
cd src/samples/Hosts.Tests
cd test/Hosts.Tests
dotnet build
pwsh bin/Debug/net9.0/playwright.ps1 install --with-deps
```
The actual tests have been written in such a way that they only need an HTTP client to work. If you don't do anything,
then the system will start an aspire test host, run the tests, then kill the aspire host again.
However, if you want faster feedback, you can also start the aspire host yourself. To avoid collisions between the
running application and the testing framework trying to rebuild all libraries, I recommend running the system in release
mode.
```
cd hosts/Hosts.AppHost
dotnet run --configuration Release
```
During startup of the first test, the system checks if the aspire host is already running. If so, it will skip starting
and simply configure a http client to work against the host.
During startup of the first test, the system checks if the aspire host is already running. If so, it will skip starting
and simply configure an http client to work against the host.
## Running under NCrunch
@ -22,10 +36,10 @@ and simply configure a http client to work against the host.
It turns out that aspnet aspire doesn't work well with NCrunch. See this link for more info.
https://forum.ncrunch.net/yaf_postst3541_Aspire.aspx
But as NCrunch is a really fast way to get feedback on the tests, I've tried to make this work differently.
But as NCrunch is a really fast way to get feedback on the tests, I've tried to make this work differently.
I've added a new configuration: Debug_Ncrunch and have included conditional compilation. If you have this enabled
(as it is in NCrunch) then it not use any of the aspire initialization and simply proceed to running the tests.
This is by far the fastest way to develop the tests. You can even have the aspire host running in debug AND debug the tests
at the same time.
(as it is in NCrunch) then it not use any of the aspire initialization and simply proceed to running the tests.
This is by far the fastest way to develop the tests. You can even have the aspire host running in debug AND debug the tests
at the same time.