Selenium Testing
No edit summary
Line 28: Line 28:
** Make sure that your machine has vCPU turned on in BIOS.
** Make sure that your machine has vCPU turned on in BIOS.
* Install Docker Desktop
* Install Docker Desktop
**
*Use the docker-selenium images/containers provided to run a whole grid setup without effort
*Use the docker-selenium images/containers provided to run a whole grid setup without any effort
**https://github.com/SeleniumHQ/docker-selenium<nowiki/>This page is '''''very detailed and well written''''', please read it carefully, it gives most answers on which setup to run.
**https://github.com/SeleniumHQ/docker-selenium<nowiki/>This page is surprisingly detailed and well written, please read it carefully, it gives most answers to which setup to run.


===== Running your test script =====
===== Running your test script =====
Line 40: Line 39:
      se:screenResolution: "1920x1080"
      se:screenResolution: "1920x1080"
  server: "<nowiki>http://localhost:4444/wd/hub</nowiki>"
  server: "<nowiki>http://localhost:4444/wd/hub</nowiki>"
Note that for example the browser name needs to be with without capital letters, it's case sensetive, and nothing will work if you have "Chrome" there instead of "chrome".
Note that for example the browser name needs to be with '''''without''''' capital letters, it's case sensetive, and nothing will work if you have "Chrome" there instead of "chrome".
 
If you want video recording of all your test runs, add  se:recordVideo: "true" to the .side.yml file like above.


===== Which Grid setup to use? =====
===== Which Grid setup to use? =====
Line 47: Line 48:
To set up the entire dynamic grid, you create a "config.toml" file with configuration next to your test script files ".side" and the ".side.yml" file, that configures browser selection and where to find the grid.
To set up the entire dynamic grid, you create a "config.toml" file with configuration next to your test script files ".side" and the ".side.yml" file, that configures browser selection and where to find the grid.


Shortend example;
Shortened example;
  [docker]
  [docker]
  # Configs have a mapping between the Docker image to use and the capabilities that need to be matched to
  # Configs have a mapping between the Docker image to use and the capabilities that need to be matched to
Line 61: Line 62:
  video-image = "selenium/video:ffmpeg-6.1-20231219"
  video-image = "selenium/video:ffmpeg-6.1-20231219"


To start this configuration from PowerShell, use this command. For ease of use, it's best to create a .ps1 file, for example called StartDynamicGrid.ps1, with this content. (it's multi-line and is tricky to keep at the command line)
docker network create grid
docker run -d -p 4442-4444:4442-4444 --net grid --name selenium-hub selenium/hub:4.16.1-20231219
docker run -d --net grid -e SE_EVENT_BUS_HOST=selenium-hub `
    -e SE_EVENT_BUS_PUBLISH_PORT=4442 `
    -e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 `
    -e SE_NODE_SESSION_TIMEOUT=60 `
    -v ${PWD}/config.toml:/opt/bin/config.toml `
    -v ${PWD}/assets:/opt/selenium/assets `
    -v /var/run/docker.sock:/var/run/docker.sock `
    selenium/node-docker:4.16.1-20231219
Note that these commands references '''config.toml''' from above, and if points to an directory called '''assets''', that will be filled with reports from the tests. There you will find new directories created, one for each test run, containing '''video.mp4''' with video recording (if set to true in .side.yml) and '''sessionCapabilities.json''' with a report on under which settings the test was executed.


 
When the grid has started (with the commands above), you can show the Selenium grid UI:http://localhost:4444/ui#
ds
 
Adding video recording to your .side.yml file: if you want video recording of all your executions, add  se:recordVideo: "true" to the .side.yml file in your local folder.
 
Show the local Selenium grid UI:http://localhost:4444/ui#





Revision as of 16:22, 1 January 2024

Setting up testing with Selenium

Background

This is not a MDriven specific instruction, it's a page with short instructions and links to other pages to make it easy to know one way to follow to set up testing towards your system.

Selenium does testing of web sites, so it's targeting a TurnKey application. These instruction and testing has all been done on Windows 11.

Selenium is a very widely used framework for running scripted web tests. You can use it both for application testing and for load testing. Load testing though will be for a small number of clients, because it takes a lot of CPU and memory on the test server to run parallel web browsers.

The easiest way to get going is using the Selenium IDE plug-in for your browser, for example Chrome. With the IDE you can create, edit and run a script without any setup on your local development machine. https://chromewebstore.google.com

Search in the Chrome web store for "Selenium IDE". Add it, and then click on the browser extension to start it.

You save your test scripts to a local folder of your choice.

Running your tests separate from your Desktop browser

You run tests on the Grid from the Selenium command line runner, readhere

Install the Selenium command line runner Command Line Runner

  • The command line runner uses node.js.
  • Once node.js is installed, run npm install -g selenium-side-runner

When you have a script that does what you want it to do, as simple and complex as that need to be, you install a Selenium Grid execution environment to run many copies of your test either after each other, or probably more important, in parallel to ensure good overall performance.

Installing a Selenium Grid is surprisingly simple, and we recommend this configuration (there are any number of ways, this is just a simple way on Windows)

  • Install WSL2 on Windows (add feature to Windows, Windows Subsystem for Linux)
    • Make sure that your machine has vCPU turned on in BIOS.
  • Install Docker Desktop
  • Use the docker-selenium images/containers provided to run a whole grid setup without effort
Running your test script

In the folder you have saved you tests (they are files with the extension .side) create a file called .side.yml, which will be used for all .side files, regardless of name. This file is used by the command line runner.

# this is how your .side.yml should or could look like
capabilities:
    browserName: "chrome"
    se:recordVideo: "true"
    se:screenResolution: "1920x1080"
server: "http://localhost:4444/wd/hub"

Note that for example the browser name needs to be with without capital letters, it's case sensetive, and nothing will work if you have "Chrome" there instead of "chrome".

If you want video recording of all your test runs, add se:recordVideo: "true" to the .side.yml file like above.

Which Grid setup to use?

There are grid setups that has only one node, or one node for each of the big browsers, Chrome, Firefox and Edge, but there are also a dynamic grid setup which we find very easy to use. It automatically will scale up to the number of CPU cores that your machine has.

To set up the entire dynamic grid, you create a "config.toml" file with configuration next to your test script files ".side" and the ".side.yml" file, that configures browser selection and where to find the grid.

Shortened example;

[docker]
# Configs have a mapping between the Docker image to use and the capabilities that need to be matched to
# start a container with the given image.
configs = [
    "selenium/standalone-firefox:4.16.1-20231219", '{"browserName": "firefox"}',
    "selenium/standalone-chrome:4.16.1-20231219", '{"browserName": "chrome"}',
    "selenium/standalone-edge:4.16.1-20231219", '{"browserName": "MicrosoftEdge"}'
]
# Windows: make sure Docker Desktop exposes the daemon via tcp, and use http://host.docker.internal:2375.
url = "http://127.0.0.1:2375"
# Docker image used for video recording
video-image = "selenium/video:ffmpeg-6.1-20231219"

To start this configuration from PowerShell, use this command. For ease of use, it's best to create a .ps1 file, for example called StartDynamicGrid.ps1, with this content. (it's multi-line and is tricky to keep at the command line)

docker network create grid
docker run -d -p 4442-4444:4442-4444 --net grid --name selenium-hub selenium/hub:4.16.1-20231219
docker run -d --net grid -e SE_EVENT_BUS_HOST=selenium-hub `
    -e SE_EVENT_BUS_PUBLISH_PORT=4442 `
    -e SE_EVENT_BUS_SUBSCRIBE_PORT=4443 `
    -e SE_NODE_SESSION_TIMEOUT=60 `
    -v ${PWD}/config.toml:/opt/bin/config.toml `
    -v ${PWD}/assets:/opt/selenium/assets `
    -v /var/run/docker.sock:/var/run/docker.sock `
    selenium/node-docker:4.16.1-20231219

Note that these commands references config.toml from above, and if points to an directory called assets, that will be filled with reports from the tests. There you will find new directories created, one for each test run, containing video.mp4 with video recording (if set to true in .side.yml) and sessionCapabilities.json with a report on under which settings the test was executed.

When the grid has started (with the commands above), you can show the Selenium grid UI:http://localhost:4444/ui#



Useful links

Main Selenium site: https://www.selenium.dev/

This page was edited 21 days ago on 04/08/2024. What links here