What is a Selenium GRID?
Selenium GRID is one of the 3 components of Selenium : Selenium WebDriver, GRID and IDE. The architecture consists of a Hub and several nodes. Through our test script, we will connect to the Selenium Hub url and it will run tests on the nodes.
Parallel execution : A grid helps in execution of test script on a virtual machine(s) instead of local machine. Suppose there are few hundred testcases, which will take couple of hours to complete execution and it would totally seize your local machine for that time duration.
Scaling up browser containers : A grid enables execution of test script on scaled up browser instances. If I want to run my tests only on Chrome browser and I have hundreds of testcases, I can scale up Chrome instances and run them in parallel which will significantly reduce execution time. After that I can scale it down as per my requirement.
Cross browser testing : On my system, I don't have a specific browser and I don't want to set it up. I can always configure the grid to have those browsers and run my tests on them!
Standalone Selenium GRID :
This is something Selenium itself provides. We need to download and install the Selenium Server (preferably latest version and Java version >= 11) on our machine.
Navigate to Selenium Server Download page and we can see all the language compatible zip files : Or directly click on the selenium-server-4.21.0.jar file.
We will see the complete installation and execution of test scripts using Selenium server in another article.
Docker Selenium GRID :
Selenium provides Docker images to run on our machine and all the grid installation and different browser setup etc. will be done automatically, we don't need to do anything! (that means we don't need to configure the grid, browser instances etc. step by step- like in case of Selenium Server.)
What is a Docker Compose file?
According to official Docker documentations :
Docker Compose is a tool for defining and running multi-container applications. It is the key to unlocking a streamlined and efficient development and deployment experience.
Compose simplifies the control of your entire application stack, making it easy to manage services, networks, and volumes in a single, comprehensible YAML configuration file. Then, with a single command, you create and start all the services from your configuration file.
It is a .yml/.yaml, where all the selenium grid configurations are defined. Selenium provides these files for all types of releases : SeleniumHQ/docker-selenium
How to Run a Docker Compose file?
Start Docker: Once we have installed Docker on our system, it is also know as the Docker Desktop, just open it. There. It has started.
Get a docker compose file and store it in a folder : Here is a sample docker compose file :
# To execute this docker-compose yml file use `docker-compose -f docker-compose-v3-beta-channel.yml up`
# Add the `-d` flag at the end for detached execution
# To stop the execution, hit Ctrl+C, and then `docker-compose -f docker-compose-v3-beta-channel.yml down`
version: "3"
services:
chrome:
image: selenium/node-chrome:4.20.0-20240505
shm_size: 2gb
depends_on:
- selenium-hub
environment:
- SE_EVENT_BUS_HOST=selenium-hub
- SE_EVENT_BUS_PUBLISH_PORT=4442
- SE_EVENT_BUS_SUBSCRIBE_PORT=4443
edge:
image: selenium/node-edge:4.20.0-20240505
shm_size: 2gb
depends_on:
- selenium-hub
environment:
- SE_EVENT_BUS_HOST=selenium-hub
- SE_EVENT_BUS_PUBLISH_PORT=4442
- SE_EVENT_BUS_SUBSCRIBE_PORT=4443
firefox:
image: selenium/node-firefox:4.20.0-20240505
shm_size: 2gb
depends_on:
- selenium-hub
environment:
- SE_EVENT_BUS_HOST=selenium-hub
- SE_EVENT_BUS_PUBLISH_PORT=4442
- SE_EVENT_BUS_SUBSCRIBE_PORT=4443
selenium-hub:
image: selenium/hub:4.20.0-20240505
container_name: selenium-hub
ports:
- "4442:4442"
- "4443:4443"
- "4444:4444"
Run the docker compose file: Go to docker compose folder and open command prompt.
On the command prompt, run the command docker compose up -d . (-d means in detached mode. Your command prompt will be available to you for running further commands)
Pulling means the images are being downloaded and pulled from Docker image hub.
Open Selenium grid on browser : After all the installations are complete, open any browser and enter the url : localhost:4444/ui . The grid runs on the port 4444 by default, as defined in the docker compose file.
"localhost", because it is not hosted on any cloud or other remote machines. It is running on local machine.
That's it! The Selenium GRID is up and running. We just need to configure our test script, so it runs the tests on remote machine.
Happy learning!
Top comments (0)