playwright-dotnet
playwright-dotnet copied to clipboard
[Question]: How do you launch the webserver
Your question
The playwright site does not have advanced documentation for .net.
How would you get Playwright to launch the web server from visual studio 2022? I would be looking to launch a Blazor Wasm front end along with APIs using WebApplicationFactory, ideally all from within Visual Studio
You could start it out of process via dotnet run
inside e.g. a fixture, that would be our recommended way as of today.
We internally start it in-process inside a fixture, depending on your application setup, this would work as well.
Out of process dotnet run
is an ugly workaround, I have been using this method with my spec flow/selenium solution for the past few years from which I am trying to move away. I dislike that the process is always left running and having to add code to loop the processes and kill stuff isn't cricket.
I would love to create an in-process test server. I am struggling to do this at present. If you have any working examples that would run a Blazor WASM application, please do share, as I have been pulling my hair out these past couple of days trying to get a Minimal .net 6 Blazor Wasm started in a Test Server and it's not fun, I know WebApplicationFactory does not support BlazorWasm, No deps file = No WebApplicationFactory... So any other suggestions for an in-process solution would be truly welcome.
Sorry for the late reply. Is this something which goes into the direction you want? https://github.com/mxschmitt/razor-playwright-dotnet-example
Hi Max, that looks like an ideal solution. It's along the same lines as Xavier's article which I found just the other day. https://medium.com/younited-tech-blog/end-to-end-test-a-blazor-app-with-playwright-part-1-224e8894c0f3
My solution is made up of a single WASM front-end project and a WebAPI project, and NUnit test project (I guess this will be pretty standard for most people)
I've taken Xavier's concept of using the server project to spin up the client. However, I've taken the approach of calling it a test harness or proxy if you prefer and ditching the pages, controllers etc. Simple razor pages project. It spins up the front end in memory, which is nice and quick. I think this is a good way in order to ditch the test harness when deploying outside of dev/test, also it means I don't pollute the WebAPI with front-end middleware. However, I do like the way you inherit the server set-up in BlazorTest.cs that's very slick.
What my dream is... what, I am trying to achieve is:- I'd like to spin up the WebAPI using webApplicationFramework in order to Mock loads of backend services, database, storage etc. Once I have that done that, I'm thinking of passing the HttpClient from webApplicationFramework into the services collection for the Blazor WASM so they can communicate with each other.
It's been proving a challenge but hopefully, it will be worth it, in order to perform fast end-to-end testing in Visual Studio before raising PR's to dev, also hope that the tests can run in the build pipelines rather than having to release in order to perform end-to-end testing... That's the dream.
Thanks for the code Max, I will further play with it tomorrow.
Hi Max, I have just noticed your app is Blazor Server Side. This goal is for a WASM app.
Hi @mxschmitt, I've prepared a more realistic solution. The main branch is based on your examples and works with Blazor Server. Both tests show the overall desired outcome for the project. https://github.com/carlblanchard/EndToEndBlazorWasmPlaywrightTesting
I have also added a second branch, which uses the pure Blazor WASM front-end project, which fails and shows what I am trying to achieve. https://github.com/carlblanchard/EndToEndBlazorWasmPlaywrightTesting/tree/InMemoryWasmHostedTests
If you could lend your expertise to overcome this conundrum it would be very much appreciated, not only by me but I guess all automated developers & testers.
Hi @mxschmitt any ideas regarding the Blazor Wasm example I supplied?
I have the exact same setup, want to achieve the same goal and ran into the same issues as @carlblanchard
Still nothing :( It's very disappointing
https://danieldonbavand.com/2022/06/13/using-playwright-with-the-webapplicationfactory-to-test-a-blazor-application/
Have you tried this? I'm able to start the host, start the WASM ~~but looks like login and jwt are not working. I'm using Identity (Individual Account) setup with Duende for the JWT.~~ and my Playwright tests are working fine.
To make them work, I used the article and adjust the CreateHost method of CustomWebApplicationFactory by adding the following code to configure the webhost so it's using https, like this :
builder.ConfigureWebHost(webHostBuilder => webHostBuilder
.UseUrls(new string[] { "https://localhost:1234/" })
.UseSetting("https_port", "1234")
.UseKestrel());
Now I can test my Blazor WASM application using Playwright.
The dotnet team did a community stand-up: Playwright testing for Blazor WASM and server: https://github.com/MackinnonBuck/blazor-playwright-example/tree/main
https://www.youtube.com/watch?v=lJa3YlUliEs
It's using NUnit but thought I would post here as it may be of use.
I think that including features like the web server launch in playwright-dotnet API would really be valuable as it is an incredibly common use case.