How to start with Playwright tests for WebViewer

Step-by-step guide for testing with Playwright

Step-by-step with VisualStudio

Step-by-step with Powershell

1. Create a new test project in Tests folder

  • Right klick on the image-20240918-073823.png folder

  • image-20240918-073848.png

  • image-20240918-073911.png

  • image-20240918-074110.png

  • image-20240918-074207.png
  • image-20240918-074403.png

  • image-20240918-074554.png

  • image-20240918-074601.png
  • Go to Powershell and to your project folder. The folder where you have your solution file. For me it is
    C:\Git\Deploys\WebViewer where my image-20240918-090952.png exists.

  • Now you write

    Now you should get:

  • Change to your project

2. Install Playwright

  • Right click on your project in the solution explorer

    and choose

  • Go to image-20240918-085029.png

    , take care you choose image-20240918-085058.png

    , otherwise you do not find Playwright. Then write image-20240918-085125.png in the searchbar.

  • Choose:

    and image-20240918-085434.png . Then image-20240918-085443.png .


  • Now right click on your project and then image-20240918-085714.png . Wait until your image-20240918-085759.png

  • Go to Powershell, to your project folder and type in

  • Write

    and hopefully get something like this


  • Now you need to build your project. Write this image-20240918-080142.png and get

    image-20240918-080209.png

     

  • Now go to

and write:

Now you have installed playwright and the stuff for the browsers

These mapps

should be here:
C:\Users\YourName\AppData\Local\ms-playwright\

 

 

 

Your Packages in your Dependencies of your Project should look like this

 




3. Add runsettings file

  • Right click on your project in the solution explorer

    and choose image-20240918-092445.png . Give it a name like image-20240918-092514.png . “My” can be replaced with every name you want. Now image-20240918-092626.png

  • When you see your file after adding, it looks like this:

    Put this example code inside instead

    <?xml version="1.0" encoding="utf-8"?> <RunSettings> <!-- NUnit adapter --> <NUnit> <NumberOfTestWorkers>24</NumberOfTestWorkers> </NUnit> <!-- General run configuration --> <RunConfiguration> <EnvironmentVariables> <!-- For debugging selectors, it's recommend to set the following environment variable --> <DEBUG>pw:api</DEBUG> <SUBSCRIBE>Subscribe to me</SUBSCRIBE> </EnvironmentVariables> </RunConfiguration> <!-- Playwright --> <Playwright> <BrowserName>chromium</BrowserName> <!--chromium, firefox, webkkit--> <ExpectTimeout>5000</ExpectTimeout> <LaunchOptions> <!--Running tests in headless mode (without opening a browser window) can make them run faster and is useful for CI/CD pipelines.--> <Headless>false</Headless> <!--<Channel>msedge</Channel> --><!--Which version playwright tests against msedge, chrome and the beta versions and msedgedev --> </LaunchOptions> </Playwright> <TestRunParameters> <Parameter name="WebViewer.BrowserTest" value="Subscribe"></Parameter> </TestRunParameters> </RunSettings>

(You can get explanations and more examples here: https://playwright.dev/dotnet/docs/test-runners )

Now right click on your runsettings file and click on image-20240918-093532.png Now it will look like this with the correct formatting:

4. Choose your runsetting for your project

Go to image-20240918-094216.png and then image-20240918-094231.png and choose image-20240918-094243.png

In the next window you go to your project folder

and choose your runsettings file

and image-20240918-094347.png it.


5. Write and run your test

Here is a short exampel test which you can put into your UnitTest1.cs to try if you installed everything correct.

using Microsoft.Playwright.NUnit; using Microsoft.Playwright; namespace WebViewerPlaywrightTests; [Parallelizable(ParallelScope.Self)] [TestFixture] public class Tests : PageTest { [Test] public async Task MyTest() { await Page.GotoAsync("http://localhost:65513/en-GB/Dometic/Account/Login?returnUrl=%2Fen-GB%2FDometic%2FCatalogue%2F1"); await Page.GetByRole(AriaRole.Button, new() { Name = "I accept cookies" }).ClickAsync(); await Page.GetByPlaceholder("User name").ClickAsync(); await Page.GetByPlaceholder("User name").FillAsync("sign"); await Page.GetByPlaceholder("Password").ClickAsync(); await Page.GetByPlaceholder("Password").FillAsync("sign"); await Page.GetByRole(AriaRole.Button, new() { Name = "Sign in" }).ClickAsync(); await Expect(Page.GetByRole(AriaRole.Link, new() { Name = "Start page" })).ToBeVisibleAsync(); } }

If you wrote a localhost test like the example, start VisualStudio in a second instans and compile the WebViewer project.

When the browser is open and looks for example like this:

go back to the VisualStudio with your test project.

Now open the image-20240918-094721.png .
You find it here:

Choose your test

and click the green triangle or a bit more down the page on image-20240918-094935.png
If everything works fine it should look like this now:


6. Codegen

Go to image-20240918-115554.png and then image-20240918-115603.png to open the Developer Powershell

Go to your project

and now write

Enter.

 

Now these two windows opened:

Go to the Playwright Inspector and change from Library to NUnit

 

 

Copy the address from the browser, for example http://localhost:65513/en-GB/Dometic and paste
it into codegen. See in the Playwright Inspector how the code has been changed.

If you now click araound a bit on the page you will see how even these steps will be added to the code. Check always that you have the red Record on both in the browser image-20240918-122542.png but even in the Inspector: image-20240918-122556.png .

This code can you now copy and paste into your test file in VisualStudio to use it as it is or to edit and put other functionality inside. Then run the test as usual.

Good luck!

7. Link for more playwright information

8. Short demo video about Playwright

9. Discussion after the demo