Codementor Events

Setting up Selenium Grid to run your tests in parallel on multiple browsers

Published Jun 23, 2018Last updated Dec 20, 2018
Setting up Selenium Grid to run your tests in parallel on multiple browsers

I spent quite a lot of time understanding how to set up selenium grid. So I decided to put what I've learned into writing. In this article, I'll share some steps in setting up selenium grid. You don't need to be selenium expert to understand this article, the only thing you need to know is how to write some basic JAVA code.

Before we start, let's discuss some basic concepts

What is Selenium?

selenium - Simply put, selenium automates browsers and you can use that ability to automatically interact with the browser for the purpose of testing. Read more here

What is Selenium Grid?

Selenium-Grid allows you to run your tests on different machines against different browsers in parallel(at the same time). That is, running multiple tests at the same time against different machines running different browsers and operating systems. Selenium-Grid support distributed test execution. Read more here. Selenium Grid uses a hub-node concept where you only run the test on a single machine called a hub, but the execution will be done by different machines called nodes.

What is Selenium Grid Hub and Node?

A hub is a central part of an activity. The same definition also applies to Selenium Grid Hub - Selenium grid hub is the central point through which tests are loaded to be executed on the node(s). A grid can only have one hub and the hub can have multiple nodes registered on it - the nodes can be running on multiple machines. A hub tells the node which tests to execute. Nodes are instances of hub upon which your tests will execute

What is Selenium Webdriver?

Selenium Webdriver - WebDriver is a tool for automating web application testing, and in particular to verify that they work as expected. It aims to provide a friendly API that's easy to explore and understand. Webdriver provides the APIs that are needed to automatically interact with browsers the same way that a normal user would use the browser. Selenium webdriver supports many browsers such as Chrome, Firefox, IE, EDGE, and Safari. Read more here.

What is Testng?

Testng
TestNG is a testing framework inspired from JUnit and NUnit but introducing some new functionalities that make it more powerful and easier to use. It is designed to covers all categories of tests. Read more here

What is Apache Maven?

Apache Maven - Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project's build, reporting, and documentation from a central piece of information. Read more here

Now that we have the basic understanding of some important concepts, let's get started with what we are here for which is setting up Selenium Grid.

Dependencies

The following must be installed on your system before you can complete this article

  • Java Runtime - This is needed to run selenium grid hub
  • Set up a window virtual machine on your system - for the purpose of this article I'll use VMware Fusion, you can use any other virtual machines app depending on your preference. Also, make sure to install window 7 or any other os on it - this will be needed in running our Node. You don't need to use a local virtual machine if you have any remote machine with an os. Install chrome and firefox browsers on the virtual os. For the purpose of this tutorial, I'll be using VMware Fusion with window 7 installed on it.
  • Maven - Install maven on your system.
  • IDE - you can use any Java IDE, for the purpose of this article, I'll use IntelliJ IDEA - it's community version is free

Run the following on your terminal to check if you've Java runtime, and Maven installed on your system

$ java -version
$ mvn -version

The result should be similar to this
Screen Shot 2018-06-17 at 9.59.07 PM.png

Having installed all the dependencies, let's start by setting up and running selenium grid hub and node.

Set up Selenium Grid Hub

We need to set up Selenium Grid Hub on the testing Machine(Machine A) - on the machine where the tests code leaves.

STEP 1
Download selenium standalone server and place it in any directory on the testing machine, let call our testing machine Machine A. Click here to download the latest version of selenium standalone server. Selenium standalone server is the only thing we need to kickstart our hub.

Before you continue, note the IP address of your testing machine(Machine A).
If you're using a mac system, you can use the following command to check your IP

$ ifconfig |grep inet

and if you're using a window system, run the following command on your command prompt to get your IP address

> ipconfig

you should see your IP address from the result of running the above commands.
Save the IP address somewhere, we'll need it in the next step

STEP 2
Now, let's launch our hub. Open your terminal or command prompt if you're using window os and change directory to where you saved the selenium standalone server you downloaded in STEP 1.

Then run the following command

java -jar <selenium_standalone_server_file_name_here> -role hub -host <your_ip_here>

Notes:

  • Replace <name_of_selenium_standalone_server_here> with your selenium standalone server file name - it's usually in this format selenium-server-standalone-3.5.0.jar(this is for version 3.5.0). If you're not running the command from the directory where you saved selenium standalone server, then replace <name_of_selenium_standalone_server_here> with the selenium file path
  • Replace <your_ip_here>with your IP address. Remembered, I asked you save the IP address somewhere in STEP 1, you that same IP here.
  • We tell selenium standalone server that this is our hub by passing hub to the -role command

After running the command you should see something similar to the below

22:21:23.778 INFO - Selenium build info: version: '3.5.0', revision: '8def36e068'
22:21:23.779 INFO - Launching Selenium Grid hub
2018-06-17 22:21:25.027:INFO::main: Logging initialized @1714ms to org.seleniumhq.jetty9.util.log.StdErrLog
22:21:25.051 INFO - Will listen on 4444
2018-06-17 22:21:25.188:INFO:osjs.Server:main: jetty-9.4.5.v20170502
2018-06-17 22:21:25.294:INFO:osjs.session:main: DefaultSessionIdManager workerName=node0
2018-06-17 22:21:25.294:INFO:osjs.session:main: No SessionScavenger set, using defaults
2018-06-17 22:21:25.300:INFO:osjs.session:main: Scavenging every 660000ms
2018-06-17 22:21:25.310:INFO:osjsh.ContextHandler:main: Started o.s.j.s.ServletContextHandler@24b1d79b{/,null,AVAILABLE}
2018-06-17 22:21:25.404:INFO:osjs.AbstractConnector:main: Started ServerConnector@44f75083{HTTP/1.1,[http/1.1]}{0.0.0.0:4444}
2018-06-17 22:21:25.405:INFO:osjs.Server:main: Started @2092ms
22:21:25.409 INFO - Nodes should register to http://172.16.2.94:4444/grid/register/
22:21:25.410 INFO - Selenium Grid hub is up and running

Note: copy this url http://<your_ip>:4444/grid/register/ and keep it somewhere, we'll need it to register the node on this hub we just started.

To test, if the hub is running - Open your browser and navigate to localhost:4444/grid/console
If all went well, you should see this

Screen Shot 2018-06-17 at 10.37.35 PM.png

STEP 3
Remembered from node explanation, we said nodes are instances of hub upon which our tests will execute and a node need to be registered on the hub. Since our test is going to be executed on the node, we need to download all the browser-drivers that we want to execute our test on. Browser-drivers are specific to each browser and they're webdriver implementation that controls their respective browsers on the local machine. They act as a proxy between the local and remote ends.
Download the following and put them in the same folder on Machine B - if you like you can also put them on different locations but just note their paths.

  • selenium standalone server - selenium jar server
  • chrome driver - A Chrome WebDriver implementation that controls a Chrome browser running on the local machine.
  • Gecko Driver - Gecko driver is WebDriver implementation that controls a gecko browser such as firefox running on the local machine.
  • IE driver - The InternetExplorerDriver is a standalone server which implements WebDriver's wire protocol that controls IE browser on the local machine

Note: you can decide to use any of the above drivers depending on which browser you want to execute your test on.

When you're done downloading the drivers, if your second machine is running on a window os, you can create a folder selenium in your C drive and put all the drivers in it.

Open your command prompt or terminal as the case may be on Machine B and run the following commands to kickoff the node

java -Dwebdriver.chrome.driver="C:\selenium\chromedriver.exe" -Dwebdriver.internetexplorer.driver="C:\selenium\IEDriverServer.exe" -Dwebdriver.gecko.driver="C:\selenium\geckodriver.exe" -jar "C:\selenium\selenium-server-standalone-3.5.0.jar" -role node -hub "http://<your_ip_here>:4444/grid/register/"

What we did?

  • This is assuming you're using a window os machine and the drivers are placed in selenium folder inside the C driver. If you're using another os or you have the drivers in another folder different from selenium, you can change the respective driver's path from the command above.
  • Replace http://<your_ip_here>:4444/grid/register/ with the url I asked you to copy in STEP 2. change <your_ip_here> to Machine A IP Address.
  • We tell selenium standalone server that this is one of our node by passing node to the -role command

Note: STEP 3 has to be done on the virtual machine or on the second machine if you're not using a virtual machine.

If all went well, after running the above command, you should see something similar to the image below

Screen Shot 2018-06-18 at 9.11.23 AM.png

You'll notice from the image above, the last line says "The node is registered on the hub and ready to use". If you don't get that same message, then something is wrong with how you set up the grid - you can go back to the previous step.

If you go back to the terminal where you're running the hub on the testing machine(Machine A) you should also see a line that says "Registered a node http://<machine_a_ip>:5555"

Screen Shot 2018-06-18 at 9.15.22 AM.png

You can check machine A configuration by navigating to http://localhost:4444/grid/console on Machine A browser, same with what we did when we confirmed on the browser if the hub is running

Screen Shot 2018-06-18 at 9.19.08 AM.png

You should see something similar to the above if all went well.
Noticed that the console shows three browsers - chrome, ie11 and firefox, that is because we tell the node to use those browsers in the command we ran to kickstart the node.

Now that we have our hub and node running, let's write a simple test script that will run in parallel on multiple browsers.

What we're going to test?

To make this very simple, our test automated script will test google.com landing page

Test Dependencies

  • IDE - I'll use Intellij IDEA for the purpose of this tutorial
  • Maven
  • JAVA
    The test will be written using JAVA programming language, don't worry you don't have to be an expert in JAVA - you only need a basic understanding of JAVA syntax.

Create Test Project On IntelliJ

  • Launch IntelliJ IDE
  • Click on file, select New and click on project
  • On New Project dialog box, Select Maven and click on Next button

Screen Shot 2018-06-18 at 10.23.09 AM.png

  • Fill in the Group Id, ArtifactId and click next

Screen Shot 2018-06-18 at 10.26.08 AM.png

  • specify the project name and click finish
    When it's done you should see something similar to below image

Screen Shot 2018-06-18 at 10.43.07 AM.png

Now, let's add the following plugins to the project

  • Maven Surefire Plugin - The Surefire Plugin is used during the test phase of the build lifecycle to execute the unit tests of an application. We also need it to generate a report for our tests result
  • Apache Maven Compiler Plugin - The Compiler Plugin is used to compile the sources of your project.
  • selenium
  • Testng

Add plugins to project
Add the following code to project pom.xml(the file is located at the root of the project directory) file;

alt

Next, create a new package in <project_name>/src/main/java/ and name it base(inside that package we'll create a class that would setup our test driver).
When you're done, create a java class inside the base package and name it SetupTestDriver.
Add the following code to SetupTestDriver class

alt

What we did?

  • We craeted SetupTestDriver with a constructor that takes as arguments os, browser, baseUrl, and node. os is the name of the platform we want to run the test on, selenium supported quite a number of platforms which includes WINDOWS, LINUX, MAC and lot more. browser is the name of the browser we want to execute our test on - this can be chrome, firefox, ie11 or any other browsers we configured in our node. baseUrl baseUrl specified the url starting point of your test. node node is the url of your node, our test will use this to know which node to kickstart the test on.
  • Next, we set up the test platform using selenium Platform class. Platform.fromString(os.toUpperCase()) we created a new instance of Platform and we used it's fromString method to passed in our os.
  • Next, we added a condition to check for the name of the browser passed to the constructor of SetupTestDriver class, this is to help know what browser Options class we should use. If the browser name is chrome, we created a new instance of ChromeOption class and we passed in platform as one of it's capability, the same goes for fireforx using FirefoxOptions and IE11 using InternetExplorerOptions. Note - setting browser capabilities gives up the opportunity to configure the browser with some capabilities needed by our test, these capabilities may includes adding some browser's extension e.g adobe flash plugin and disabling some browser security setting that can affect our test.
  • Next, we created a new instance of selenium RemoteWebDriver class, passing in node url with the browser capabilities option. new RemoteWebDriver(new URL(node + "/wd/hub"), ieOption);
  • we added getOs, getBrowser, getBaseUrl, getNode and getDriver methods to ge those default values from the class instance.

Now that we have SetupTestDriver clas all set up, let's create a simple java test to test Google search box.

create a new test class in src/test/java, we can name it GoogleSearchTest

Add the following code to the class

alt

What we did?

  • We attached @BeforeClass decorator to setUp method - @BeforeClass is a testng decorator that tells testng to run a specific method first before running the other tests in the class. In our case setUp method will run first before any other method. we also attached @AfterClass decorator to closeBrowser method - this decorator tells testng to run this method at the end of running other tests in the class, it will run once the test in the class are done.
  • We also attached @Parameters decorator to setUp method, we need that decorator to be able to get the parameters passed to the test class. In our case, we'll be passing os, browser, url and node to our test.
  • In setUp method, we created a new instance of SetupTestDriver class passing the required parameters to its contructor.
  • googleTitleTest, googleUrlTest, googleSearchButtonTest, googleFeelingLuckyButtonTest and googleSearchBox are our test methods and we attached @Test decorator to them to tell testng that those methods are tests.
  • Finally, we added some to assertions to our test methods to validate the google search button and I'm feeling lucky button are displayed and enabled on the page and two other assertions to validate the value of the page title and the page current url.

Next, let set up our launch file - we'll use this file in running out test

Create a new resource package in src/test/, let's call it resources. Create a a new package insideresources, let's call itlaunchers- this will house our tests launchers files. Create another filegoogle.xmlinside thelaunchers` package.

Add the following to google.xml

alt

What we did?

  • We added a new test suite with <suite> tag and we specified that that all <test> should be run in paralle with parallel="tests" - this will run all the methods in the <test> tage in the same thread, but each <test> tage will be in a separate thread.- Read more here.
  • We also added three separate <test>, each with different browser parameters - ie11, chrome and firefox. This will allow our test to run in parallel with different browsers.
  • The remaining three parameters was passed through the test suites since those three are the same, unlike browser that differs.

Run

To run the test, open google.xml and right click on it, then select Run

Screen Shot 2018-06-23 at 9.46.31 AM.png

This will kick off the test and if everything works fine - the test should be running MACHINE B using chrome, ie11 and firefox browsers.

Conclusion

We've been able to cover the following in this article

  • Basic selenium concepts
  • Setting up selenium grid hub and node
  • Setting up simple google search test using JAVA and testng framework

Hope this help.

Discover and read more posts from Olawale Aladeusi
get started
post commentsBe the first to share your opinion
Zeeshan Ali
3 years ago

I am getting below run after running testng.xml file
please reply

[TestNG] Running:
C:\Users\ZEESHANALI\workspace\Selenium\TUI\testngparallel.xml

null
null
null
null
null

===============================================
GDN_Acco_Only
Total tests run: 62, Failures: 0, Skips: 62
Configuration Failures: 5, Skips: 5

Neelima Sunkara
4 years ago

Hi, I have followed the steps and i am getting configuration failure.

I am using Eclipse IDE and created a new project, exactly the way how it is described above. I am using TestNG, not sure what am i doing wrong.

Any help is much appreciated.

Here is the console output:

[RemoteTestNG] detected TestNG version 6.8.0
[TestNG] Running:
C:\Users\neelimas\eclipse-workspace-new\test\src\test\java\resources\launchers\google.xml

Google Search Tests
Total tests run: 6, Failures: 0, Skips: 6
Configuration Failures: 6, Skips: 0

Thanks,
Neelima

Neelima Sunkara
4 years ago

Nice post Olawalejarvis!

i couldn’t able to establish connection between hub and node when i use the following command with my system host ipaddress.
java -jar <selenium_standalone_server_file_name_here> -role hub -host <your_ip_here>

hub was up and running, but node command from step3 was resulting into error.

So i removed ipaddress from step2 command and started the hub again
ie., java -jar <selenium_standalone_server_file_name_here> -role hub

And this time the connection has established perfectly as expected.

Any thoughts?

Show more replies