In this tutorial, we will talk about how can we setup selenium Grid to execute tests parallel.
Prior to this, to understand the basic concept of Selenium Grid, refer below article –
Introduction to Selenium Grid – What is Selenium Grid? <Link>
Selenium Grid will actual help you to run the tests on multiple nodes which will reduce the total execution time. Let us see how selenium Grid work –
How Selenium Grid works?
Observe the above image, it shows how Selenium Grid works. It has Hub and Multiple nodes. Entire script is done at Hub node whereas entire execution will be done at Nodes. So, on various nodes we can have various OS along with various browsers.
(Note: Note that, ideally node machines are different from Hub machine)
Let us talk more about Hub and Node –
As the image shows, Hub is the machine where we write all our scripts. So, it a central point where we load out tests. In entire Grid system, we will have one Hub and multiple nodes. As it is Hub, it is launched on single machine. All the execution will be happening on Node machines, so there can be one or multiple nodes having different platforms with same or different browser.
So, the concept is like, Hub has got one PC (Parent PC) whereas Nodes has got multiple machines (Child PC). Hub will be giving instructions to Nodes, accordingly nodes will execute the test cases. Hub is only to give instructions.
Before we setup Hub and Nodes, we need to get selenium Standalone server from http://docs.seleniumhq.org/download/
To download Selenium Grid, click here
Let us see now how-to setup the Hub –
Now, Selenium Standalone Server is a jar file, so we need to do the setup from command prompt.
Open Command Prompt.
Use below command to start Hub.
java -jar C:selenium-server-standalone-3.4.0.jar -role hub
Once above command has run, hub setup will complete. You will see the message saying Hub is running. Also, it gives the Hub address which we need while registering nodes.
Note that, Hub will start at port 4444 by default.
Now, let us see how to setup Nodes –
Note: Ideally, nodes should be setup on different machines than Hub machine, but for demo purpose, I am using the same machine. The process for using different machines for Nodes is exactly same. Just we need to do the node setup on each of the node machine individually by giving Hub address (shown in above image).
To setup Node, we need same jar again. We just need to setup the role as node and give hub address while registering the Node. Let us see how this can be done.
Open another command Prompt. Do not close the command prompt where Hub is running.
Use below command to setup Node –
java -jar C:selenium-server-standalone-3.4.0.jar -role node -hub http://192.168.1.106:4444/grid/register/
where, Selenium server standalone jar is placed in C drive. Hub machine address Is used to register node.
Observe above screenshot, it shows Node is running.
Now, we are done with hub and node setup. In the similar way, we can do the node setup on multiple nodes (machines) as required.
That’s it, Node setup is done on the machine for 3 browsers by default ( Firefox, IE and Chrome). To verify this, go to the link – http://localhost:4444/grid/console
Now, suppose, you want to setup only 1 browser, then use below command line –
java -jar C:selenium-server-standalone-3.4.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 5556 -browser browserName=iexplore
Above command will set Internet Explorer.
If you want to setup only Chrome browser or Firefox browser, then use browserName=firefox and browserName=chrome
If you want to setup only 1 instance of all 3 browsers (Chrome, IE and firefox), then use –
java -jar C:selenium-server-standalone-3.4.0.jar -role webdriver -hub http://localhost:4444/grid/register -port 5556 -browser browserName=iexplore -browser browserName=firefox -browser browserName=chrome
Once Hub and Node setup is done (Grid setup is done), Create one sample test case to run on the Grid.
Refer below example –
package basicsOfSelenium;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.Test;
public class ArrayDemo {
@Test
public void mailTest() throws MalformedURLException{
DesiredCapabilities dr=null;
dr=DesiredCapabilities.firefox();
dr.setBrowserName("firefox");
dr.setPlatform(Platform.WINDOWS);
RemoteWebDriver driver=new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), dr);
driver.navigate().to("https://www.google.co.in");
driver.findElement(By.xpath("//*[@id='gs_htif0']")) .sendKeys("Search me");
driver.findElement(By.xpath("//*[@id='tsf']/div[2]/div[3]/center/input[1]")) .click();
driver.close();
}
}
When you run above piece of code, your test should be executed on node machine, on firefox browser.
If you want to run the test on multiple platforms, parallelly, then what you can do is, set browserType parameter in testng.xml as below –
<parametername =”browserType” value=”IE”/>
In similar way add above parameter for Chrome and firefox browser in testng.xml. Update the code to handle browser type in your program.
That’s it.
This is how we can setup the Selenium Grid and run test cases in parallel on Selenium Grid.
Hope this helps.