A Variant – Selenium Grid 4

     Selenium Grid has been an integral part of automation testing, as it lets us perform test case execution on different combinations of browsers, operating systems (or platforms), and machines. It also enables us to perform parallel execution to accelerate the cross-browser testing process.

     Selenium Grid 4, the successor to Selenium Grid 3. Selenium 4 features an improved Selenium Grid design; the significant change is the introduction of a fully distributed mode. Unlike the earlier versions of Selenium (i.e. version 3.x), the Selenium 4 Server jar contains everything (including dependencies) required to run a Grid.

     Until the Selenium Grid 3, it could only be used as Hub and Node(s). Though this is still supported in Selenium 4, it now supports two more types of Grid. In this article, I would like to share all the three grid types supported in Selenium 4:

  • Standalone (act as Server and Node).
  • Classical Grid (Hub and Node like earlier versions).
  • Fully Distributed (Sessions, Distributor, Router).
Standalone

     In this mode, the selenium grid setup runs both Hub and Node running together. So just run the jar file with the standalone command to get into the standalone mode. Following is the command to activate standalone mode:

java -jar selenium-server-4.1.1.jar standalone 

     Once we start the standalone mode, we will get the below logs in command-line; that will give us the information about the discovered drivers, local distributors, standalone executed server IP, and port:

     Once the standalone mode is ready, we can use the below code to execute the scripts on the available Node driver,

DesiredCapabilities caps = new DesiredCapabilities();
caps.setBrowserName("chrome");
driver = new RemoteWebDriver(new URL("http://localhost:4444"), caps);  
driver.manage().window().maximize();
driver.get("https://www.google.com/");
driver.findElement(By.xpath("//input[@title='Search']")).sendKeys("journeyofquality\n");
driver.findElement(By.xpath("//h3[contains(text(),'journey of quality – make it perfect!')]")).click();

     In the above code, I used the localhost instead of IP because standalone started and execution happening on the same system.

Classical Grid

     In this mode, we are using Hub and Node like earlier versions. In the Node configuration, we have to specify the detect drivers option which detects what are the drivers we have within the system, and also specify publish events parameter which specifies the hub and subscribe events for the hub as well, once we run the hub it will emit the publish events and the subscribe events options. This is very helpful while running Node(s) on different systems. Following is the command used to start the Hub:

java -jar selenium-server-4.1.1.jar hub

Once the Hub started, we will get the below logs in the command line,

     Next, we can start the Node on the same system or from a different system. We can use the below code to start the Node if Hub and Node are running on the same system,

java -jar selenium-server-4.1.1.jar node --detect-drivers true

     We can use the below code to start the Node if Hub and Node are on different systems,

java -jar selenium-server-4.1.1.jar node --detect-drivers true --publish-events tcp://192.168.1.7:4442 --subscribe-events tcp://192.168.1.7:4443

Once the Node started, we will get the below logs in the command line,

     Once the classical grid is ready, we can use the below code to execute the scripts on the available Node drivers,

DesiredCapabilities caps = new DesiredCapabilities(); caps.setBrowserName("chrome"); DesiredCapabilities caps = new DesiredCapabilities();
caps.setBrowserName("chrome");
driver = new RemoteWebDriver(new URL("http://192.168.1.7:4444"), caps);
driver.manage().window().maximize();
driver.get("https://www.google.com/");
driver.findElement(By.xpath("//input[@title='Search']")).sendKeys("journeyofquality\n");
driver.findElement(By.xpath("//h3[contains(text(),'journey of quality – make it perfect!')]")).click();

     In the above code, the IP address from the Hub system, if we execute the code on a hub system, we can see the browser execution happening on the Node system.

Fully Distributed

     In the fully distributed mode, we have to specify the sessions first, once the sessions are established then we need to distribute the sessions. The sessions are used for the router to subscribe events and sessions at the Node level. Following are short descriptions of the terms,

  • Sessions – Sessions having the Session ID and Node URI, which maps the session ID to the Node.
  • Distributor – Selects the appropriate Node where the test should be executed.
  • Router – Listens to the new session request.
  • Node – Test machine where the test execution takes place.

     As the first step is starting the Sessions which is primarily responsible to map Session IDs to the corresponding Node on which the session is running. Following is the command to start the sessions,

java -jar selenium-server-4.1.1.jar sessions

Once the sessions started, we will get the below logs in the command line,

     The second step is to start the distributor process. When there is a request from the Selenium client to create a Sessions request, the Distributor is responsible for assigning an appropriate Node. Following is the command to start the distributor process,

java -jar selenium-server-4.1.1.jar distributor --sessions http://localhost:5556

     Once the distributor process started, we will get the below logs in the command line,

     The third step is to start the Router. The router listens to the new Session requests on http://localhost:4444. Following is the command to start the router,

java -jar selenium-server-4.1.1.jar router --sessions http://localhost:5556 --distributor http://localhost:5553

     Client requests are sent to the Router and depending on the request type, the appropriate path is selected. The incoming request for creating a new session is redirected to the Router process. All other types of requests are sent to the Node based on the request after querying the Node URI from the Sessions map using the Session ID.

     Finally, we can start the Node on the same system or from a different system. We can use the below code to start the Node if Hub and Node are running on the same system,

java -jar selenium-server-4.1.1.jar node --detect-drivers true

     We can use the below code to start the Node if Hub and Node are on different systems,

java -jar selenium-server-4.1.1.jar node --detect-drivers true --publish-events tcp://192.168.1.7:4442 --subscribe-events tcp://192.168.1.7:4443

     We can check the Selenium Grid setup using the command http://localhost:4444/status on the web browser. Once the fully distributed mode is ready, we can use the below code to execute the scripts on the available Node drivers,

DesiredCapabilities caps = new DesiredCapabilities();
caps.setBrowserName("chrome");
driver = new RemoteWebDriver(new URL("http://192.168.1.7:4444"), caps);
driver.manage().window().maximize();
driver.get("https://www.google.com/");
driver.findElement(By.xpath("//input[@title='Search']")).sendKeys("journeyofquality\n");
driver.findElement(By.xpath("//h3[contains(text(),'journey of quality – make it perfect!')]")).click();

     Fully distributed mode is well advanced Selenium Grid mode can be widely used when we have a requirement for complete distributed mode execution. I hope everyone enjoyed reading the article and got some idea on the new variant of Selenium Grid; its configuration and implementation part. Try to utilize the opportunity to configure and implement the different modes in your automation world.

make it perfect!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: