Selenium Grid -Chrome Browser

Chaya Thilakumara
Chaya Thilakumara
Published in
5 min readJan 4, 2019

Let’s see how to write your first program using Selenium Grid.Here I am not using any VM machines, I am creating both hub and node in the same machine. If you don’t have any VM/laptop(s) you can create both hub and node on the same machine.

-Summery-1. Node machine :chrome browser, Selenium server.jar file and chromedriver.exe2. Hub machine :Selenium server.jar file 3. Start the server on hub4. Register a node with hub (connection between hub and the node)5. In your code : Provide the url of your hub, the desired capabilities and chrome options to launch the desired capabilities6. Send the request to hub, then hub decide in which node it should run and node will launch the application using the chrome browser
  • Go to seleniumhq.org site and download Selenium Standalone Server .jar file.
SeleniumHQ > Downloads > Selenium Standalone Server
  • Download the latest binary execution of chromedriver.exe and save it.
ChromeDriver-WebDriver for Chrome
  • Open command prompt to create hub and node. Type below code in order to set up your hub.
java -jar <location of your Selenium server.jar file> -role hubeg: 
java -jar "C:\Program Files (x86)\Selenium\selenium-server-standalone-3.141.59.jar" -role hub
java -jar “C:\Program Files (x86)\Selenium\selenium-server-standalone-3.141.59.jar” -role hub

If the hub is up, you will see Selenium hub is up and running message in the command prompt.

Selenium Grid hub is up and running

Type http://localhost:4444/ url in your browser and if your getting the below screen, that means your hub or server has started.

Selenium Grid Hub :localhost:4444

Right now console is blank since there is no node registered under the hub.

localhost:4444/grid/console
  • Open a new command prompt window and go to the folder where exactly your selenium standalone jar file available.
Command: cd <file path of your selenium standalone jar file>
  • Type below code in order to set up your node.
java -Dwebdriver.chrome.driver=<file path of your chromedriver.exe> -jar <file path of your selenium standalone jar file> -role node -hub <url of your hub>eg:
java -Dwebdriver.chrome.driver="C:/Program Files (x86)/Selenium/chromedriver.exe" -jar "C:\Program Files (x86)\Selenium\selenium-server-standalone-3.141.59.jar" -role node -hub http://localhost:4444/grid/register/
Selenium Grid Node
The node is registered to the hub and ready to use

Now node is up and running.

When you refresh http://localhost:4444/grid/console page, you can see one node machine.If you have 5 VMs then you will see 5 sections.

http://localhost:4444/grid/console

Now both the hub and the node are registered, let’s write the code to execute your test case using selenium grid.

Whenever you want to run your code on different machines using selenium grid you want to create the object of desired capabilities.

DesiredCapabilities cap=new DesiredCapabilities();

If you want to define any capabilities you can define them using the object of desired capabilities.

cap.setBrowserName(“chrome”);
cap.setPlatform(Platform.WINDOWS);

Next you need to create chrome option class.

ChromeOptions op = new ChromeOptions();

If you like run your test in headless mode you can insert that to the chromeoptions.

options.setHeadless(true);

You need to merger desired capabilities with the chrome options.

options.merge(cap);

Then you need to define one string variable to locate where exactly your hub is running.

String huburl =”http://172.18.132.145:4444/wd/hub";

Instead of writing WebDriver driver=new ChromeDriver, you need to write:

WebDriver driver=new RemoteWebDriver(new URL(huburl), options);

To the RemoteWebDriver you need to pass where exactly your hub is running <new URL(huburl)> and the chrome options.

After defining the basic elements in selenium grid, you can write the test case you want to execute with the selenium grid. But before running your test case make sure both your hub and the node are up and running.

driver.get(“https://www.findmyfare.com/");System.out.println(“Title is “+driver.getTitle());driver.close();

One chrome icon is disabled that means one chrome is getting used.

Only four chrome browsers are free
Selenium Grid Test Results
Selenium Grid-Hub

Though the code we are sending the request to the hub.

Got a request to create a new session: Capabilities {browserName: chrome, goog:chromeOptions: {args: [ — headless, — disable-gpu], extensions: []}, platform: WINDOWS}Trying to create a new session on test slot {server:CONFIG_UUID=bd2e79bc-df3b-4403-b446-8d5ceadb2c43, seleniumProtocol=WebDriver, browserName=chrome, maxInstances=5, platformName=WIN10, platform=WIN10}

Then hub send the request to the node.

07:58:57.955 INFO [ActiveSessionFactory.apply] — Capabilities are: {
“browserName”: “chrome”,
“goog:chromeOptions”: {
“args”: [
“ — headless”,
“ — disable-gpu”
],
“extensions”: [
]
},
“platform”: “WINDOWS”
}
07:58:57.958 INFO [ActiveSessionFactory.lambda$apply$11] — Matched factory org.openqa.selenium.grid.session.remote.ServicedSession$Factory (provider: org.openqa.selenium.chrome.ChromeDriverService)
Starting ChromeDriver 2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387) on port 2414
Only local connections are allowed.
07:59:01.334 INFO [ProtocolHandshake.createSession] — Detected dialect: OSS
07:59:01.939 INFO [RemoteSession$Factory.lambda$performHandshake$0] — Started new session bd13583c5262ee75448d5c83197632b6 (org.openqa.selenium.chrome.ChromeDriverService)
08:29:13.352 INFO [ActiveSessions$1.onStop] — Removing session bd13583c5262ee75448d5c83197632b6 (org.openqa.selenium.chrome.ChromeDriverService)
Selenium Grid-Node

If you want to create one more node, open one more command prompt window and go to the folder where exactly your selenium standalone jar file available. Enter the same code which you used to create the node (only change the port number <-port 5557>).

java -Dwebdriver.chrome.driver=”C:/Program Files (x86)/Selenium/chromedriver.exe” -jar “C:\Program Files (x86)\Selenium\selenium-server-standalone-3.141.59.jar” -role node -hub http://localhost:4444/grid/register/ -port 5557
Selenium Server is up and running on port 5557
Registering the node to the hub: http://localhost:4444/grid/register
http://localhost:4444/grid/console

You ca go through my previous article on Selenium Grid using below link.

I hope that this post was able to assist you to implement Selenium Grid successfully. Let’s meet with another exciting article…Till then, Happy Testing !!!!

--

--

Chaya Thilakumara
Chaya Thilakumara

Pursue your passion, and everything else will fall into place.