Run Playwright on Selenium Grid

After last week’s success with BrowserStack, I tried to run Playwright on Selenium Grid. The user experience for this use case is at the opposite end of the scale. The documentation is not much help and Selenium Grid has its own incompatibilities. Let’s look at how we can still make it work.

 

Preparation

I added this little test that shows me what browser runs the test:

If I run it directly through Visual Studio, I get this output:

YOUR WEB BROWSER IS:
Headless Chrome 108 on Windows 10

When we run this test in Selenium Grid, the operating system should change to Linux.

 

Create a dynamic Selenium Grid

You can follow along my post on how to create a dynamic Selenium Grid. However, we need different container versions and two additional environment variables. It is a small wonder that Playwright works, but it is super specific what versions it accepts (see issue #18892 on GitHub).

Make sure that your config.toml looks like this:

The docker-compose-v3-dynamic-grid.yml needs version 4.5.0-20221017 for the containers and you must add SE_NODE_PORT and SE_NODE_GRID_URL as environment variables:

You can start the dynamic Grid with this command:

 

Run codegen with Selenium Grid

We can run the test code generator in a PowerShell terminal with these two commands:

If everything works, only the Playwright Inspector window will start. The browser runs in the Selenium Grid where you can connect to the session in http://127.0.0.1:4444/ui#/sessions (password: secret).

You can click inside the Selenium Grid session and the code gets recorded in the Playwright Inspector window:

We need to click inside Selenium Grid to fill the Inspector window with code.

 

Run the library project inside Selenium Grid

As with codegen, we must set the environment variable pointing to our Selenium Grid. Otherwise, the Playwright code looks as if we want to run it locally:

The video recording is done by Playwright and not by Selenium Grid. Therefore, your video will be in the videos folder and not in the assets folder of the Selenium Grid.

 

Run NUnit tests inside Selenium Grid

Since the tests need no changes, we can set the environment variable and the run dotnet test to use our Selenium Grid:

The output of the Where() test should write something like this to the console:

YOUR WEB BROWSER IS:
Headless Chrome 106 on Linux

 

Next

Selenium Grid can be used with Playwright but prepare yourself for a lot of fiddling before you can use it. Selenium works a lot smoother and with better support should something go wrong. With these challenges out of the way, it is an elegant way to change to a remote execution of your tests – all it takes is an environment variable and you are good to go.
Next week we take a deeper look on the production-readiness of Playwright.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.