Introduction to Selenium Grid 4

Selenium Grid is a part of the Selenium Suite that specializes in running multiple tests across different browsers, operating systems, and machines in parallel. It achieves this by routing commands of your test scripts to browser instances that are running on remote machines. Each of these machines can run multiple instances of a browser, and the Grid manages the allocation of these browser instances to your tests. The key components that make up Selenium Grid are the Hub and Nodes.

Hub

The Hub acts as the central point where the tests are loaded. When you execute your Selenium tests, the Hub receives the test commands and understands on which browser, version, and platform (operating system) the test should run. There is typically only one Hub in a Selenium Grid setup. It listens to test requests and assigns them to Nodes that match the test’s requirements.

Nodes

Nodes are the machines that actually run the tests. A Grid can have one or many Nodes. Each Node registers with the Hub, specifying the browser versions and platforms (operating systems) it supports. When the Hub receives a request to run a test on a specific browser/version/platform, it searches for a Node that matches the criteria. Once a match is found, the Hub sends the test commands to that Node. A Node can run multiple browser instances, but the exact number depends on the resources (CPU, memory) of the machine and how the Node is configured.

Key Features and Benefits

  • Parallel Execution: Selenium Grid allows for the parallel execution of tests, which can significantly reduce the time required for running large test suites.
  • Cross-Browser Testing: It enables cross-browser testing by supporting tests across different browser types simultaneously.
  • Cross-Platform Testing: Tests can be run on different operating systems (Windows, Linux, macOS) from the same test suite, making it easier to ensure compatibility across environments.
  • Scalability: Grid’s architecture allows for scaling up by adding more Nodes to handle a larger volume of tests, or scaling out to include different browsers and operating systems.
  • Resource Optimization: By distributing tests across various machines, Selenium Grid optimizes the use of resources, allowing for more efficient use of testing infrastructure.

Setup

Setting up a Selenium Grid involves configuring one machine to serve as the Hub and one or more other machines to serve as Nodes. These machines can be physical machines in a lab, virtual machines, or containers. Docker has become a popular choice for running Selenium Grid because it simplifies the setup of isolated environments for different browsers and versions, and it can be easily scaled.

Usage

To use Selenium Grid, your test code needs to include the desired capabilities (browser, browser version, and platform) and the address of the Grid Hub. The test framework (e.g., WebDriverIO, Selenium WebDriver) then communicates with the Hub, which routes the tests to the appropriate Nodes.

Version 4.x Updates

Selenium Grid 4.x introduced several significant updates, including a more modern architecture that separates the Grid into four main components: Router, Distributor, Session Map, and Node, which together improve the Grid’s routing logic and scalability. It also comes with an improved UI for monitoring the Grid’s status and simplified configuration and deployment processes.

Selenium Grid is a powerful tool for teams looking to improve their testing efficiency and coverage across multiple browsers and platforms.

Integration with WebdriverIO

WebdriverIO is a custom implementation for Selenium’s W3C WebDriver API and is designed to be platform- and framework-agnostic. It allows you to control a browser or a mobile application with just a few lines of code. WebdriverIO supports Selenium Grid out of the box, making it easy to scale up your testing.

Setting Up Selenium Grid with Docker Compose

Docker Compose allows you to define and run multi-container Docker applications. With Compose, you can create a lightweight, reproducible, and portable testing environment for your WebdriverIO tests.

Step 1: Install Docker and Docker Compose

Ensure Docker and Docker Compose are installed on your machine. Installation guides can be found on the official Docker website.

Step 2: use Docker Compose File

Create a file named docker-compose.yml in your project directory and define Selenium Grid Hub and Node services.

version: '3'
services:
  selenium-hub:
    image: selenium/hub:latest
    ports:
      - "4444:4444"

  chrome:
    image: selenium/node-chrome:latest
    depends_on:
      - selenium-hub
    environment:
      - SE_NODE_HUB_HOST=selenium-hub
      - SE_NODE_HUB_PORT=4444

  firefox:
    image: selenium/node-firefox:latest
    depends_on:
      - selenium-hub
    environment:
      - SE_NODE_HUB_HOST=selenium-hub
      - SE_NODE_HUB_PORT=4444

Step 2: Start Selenium Grid

  1. Open a Terminal or Command Prompt: Navigate to the directory containing your docker-compose.yml file. Run Docker Compose Up: Execute the following command to start the Selenium Grid:

docker-compose up -d
The -d flag runs the containers in the background.

Step 3: Verify Selenium Grid is Running

  1. Open a Web Browser: Navigate to http://localhost:4444 to access the Selenium Grid console. You should see the Selenium Hub’s console, showing the connected Chrome and Firefox nodes.

Step 4: Run Selenium Tests

  • Configure your Selenium tests to use the remote WebDriver pointing to the Selenium Hub at http://localhost:4444/wd/hub.
  • Please check the testcase selenium grid to run the following script
exports.config = {
   	hostname: 'localhost',
	port: 4444,
	path: '/wd/hub',
    // Specify capabilities
    capabilities: [{
browserName: 'chrome'
 }], 
};

Step 5: Stop Selenium Grid

  • When you’re done, you can stop and remove the containers by running:
docker-compose down

This command stops the running containers and removes them, along with their networks. It does not remove the downloaded images, so starting the grid again later will be faster. By following these steps, you should be able to test a Docker Compose Selenium Grid setup locally on both Windows and Linux.