Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question]: How do you launch the webserver #2369

Open
carlblanchard opened this issue Nov 6, 2022 · 11 comments
Open

[Question]: How do you launch the webserver #2369

carlblanchard opened this issue Nov 6, 2022 · 11 comments

Comments

@carlblanchard
Copy link

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

image

@mxschmitt
Copy link
Member

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.

@carlblanchard
Copy link
Author

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.

@mxschmitt
Copy link
Member

Sorry for the late reply. Is this something which goes into the direction you want? https://github.com/mxschmitt/razor-playwright-dotnet-example

@carlblanchard
Copy link
Author

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.

@carlblanchard
Copy link
Author

Hi Max, I have just noticed your app is Blazor Server Side. This goal is for a WASM app.

@carlblanchard
Copy link
Author

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.

@carlblanchard
Copy link
Author

Hi @mxschmitt any ideas regarding the Blazor Wasm example I supplied?

@JesperRisager
Copy link

I have the exact same setup, want to achieve the same goal and ran into the same issues as @carlblanchard

@carlblanchard
Copy link
Author

Still nothing :( It's very disappointing

@philippedurocher
Copy link

philippedurocher commented Mar 1, 2023

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.

@CameronMackenzie99
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants