What is Selenium Grid?
Selenium Grid is a testing tool which allows us to run our tests on different machines against different browsers, operating machines in parallel. It is a network for connected Test machine which we call them as nodes which have any Operating System and Browser. This network is controlled by Hub. Hub takes instructions from the code and run the desired test cases in the nodes.
When to Use Selenium Grid?
- We use Selenium Grid when we want to reduce the time it takes for the test suite to complete a test pass.
- To run your tests against multiple browsers, multiple versions of browsers, and browsers running on different operating systems.
For Example: If you have 100 tests suites, but you set up Selenium-Grid to support 3 different machines i.e, separate physical machines) to run those tests, your test suite will complete in one-third the time.
Difference between Grid 1 and Grid 2
Following are the main differences between Selenium Grid 1 and Selenium Grid 2
Grid 1 | Grid 2 |
You need to install Apache Ant | Apache Ant installation is not required in Grid 2 |
It has its own remote control which is different from Selenium RC Server | It is bundled with Selenium Server jar file |
It supports only Selenium RC commands | It supports both Selenium RC and WebDriver scripts |
Per one remote control we can automate one browser | You can automate up to 5 browsers per remote control |
Selenium Grid Architecture
Selenium Grid has a Hub and Node Architecture.
What is Hub?
- The hub is the central point where you load your tests in the Grid setup.
- There should be only one hub in a grid.
- The hub is launched only on a single machine, for example, a computer whose operating system is Windows 10 and whose browser is Chrome.
- We load our test into the hub then hub executes them into different nodes which are also a computers. For example, if you have specified a capability to run a Test on Chrome, Hub will redirect the Test to Node which satisfy the criteria and Node will execute those test.
What is Node?
- The Test Machine which opts to connect with the hub test machine which opts to connect with the Hub.
- A Grid network can have multiple nodes
- A node is supposed to have different platforms, i.e. different operating system and browsers
- The node does not need be the same platform for running as that of hub.
How to Set Up Selenium Grid? Using Command Line
Here we will use 2 machines. The system which will run the first machine is the hub while the other machine will run a node. To understand easily, let us call the machine where the hub runs as “Machine X” while the machine where the node runs will be “Machine Y”. And also note down their IP addresses. Let us say Machine X has an IP address of 192.168.1.7 while Machine Y has an IP address of 192.186.1.8.
Step 1: Down the Selenium Server using the URL https://www.selenium.dev/downloads/
Step 2: Place the Selenium Server .jar file in your HardDrive. Here I am placing it in C drive of both Machine X and Machine Y. This will finish with our Selenium Grid installation. The below steps will launch the node and the hub.
Step 3:
- Now we are now going to launch a hub. Go to Machine X. By using the command prompt we will navigate to the root of Machine X’s – C drive, because that is the directory where we placed the Selenium Server.
- On the command prompt, type java -jar selenium-server-standalone-3.141.59.jar -role hub
- The hub should successfully be launched.
Step 4:
We also use another way to verify whether the hub is running i.e through a browser. Selenium Grid, by default, uses Machine X’s port 4444 for its web interface. We simply open up a browser and go to the URL http://localhost:4444/grid/console
Also, you can check if Machine Y can access the hub’s web interface by launching a browser there and going to where “iporhostnameofmachineX” should be the IP address or the hostname where the hub is running of the machine. Since Machine X’s IP address is 192.168.1.12, then on the browser on Machine Y you should type http://192.168.1.12:4444/grid/console
Step 5:
- Now that the hub is already set up, we are going to launch a node. Go to Machine Y and launch a command prompt.
- Navigate to the root of Drive C and type the below code. We used the IP address 192.168.1.12 because that is where the hub is running. We used port 5566 and you may choose any free port number you desire.
- NOTE: You now have to give path to the Gecko driver if using Firefox. Here is code that needs to be used
java -Dwebdriver.gecko.driver=”C:\geckodriver.exe” -jar selenium-server-standalone-3.141.59.jar -role webdriver -hub http://192.168.1.12:4444/grid/register -port 5566
Test Scripts Designing that can run on the Grid
We need to use DesiredCapabilites and the RemoteWebDriver objects to design test scripts that will run on the grid.
- DesiredCapabilites is used to set the type of browser and OS that we will automate
- RemoteWebDriver is used to set which node (or machine) that our test will run against.
First we need to import this package to use the DesiredCapabilites object
You need to import these packages to use the RemoteWebDriver object.
Using the DesiredCapabilites Object
Go to the Grid’s web interface and hover on an image of the browser that you want to automate we need to take note of the platform, and the browserName.
DesiredCapabilities capability = DesiredCapabilities.firefox();
capability.setBrowserName(“firefox”)
capability.setPlatform(Platform.windows);
Running a Sample Test Case on the Grid
Below is a simple WebDriver Testng code that you can create in Eclipse on Machine X. Once you run it, automation will be performed on Machine Y.
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
public class GridExample {
public static WebDriver driver;
public static void main(String[] args) throws MalformedURLException, InterruptedException{
String URL = "http://newtours.demoaut.com/";
String Node = "https://192.168.1.12:4444/wd/hub";
DesiredCapabilities cap = DesiredCapabilities.firefox();
driver = new RemoteWebDriver(new URL(Node), cap);
driver.navigate().to(URL);
Thread.sleep(5000);
driver.quit();
}
}
Conclusion:
- Selenium Grid is used to run multiple tests at the same time on different browsers and platforms.
- Grid uses the hub-node concept.
- The hub is the central point wherein you load your tests.
- Nodes are the Selenium instances that will execute the tests that you loaded on the hub.
- You need to download the Selenium Server jar file to install the Selenium Grid.
- To verify if the hub is running there are 2 ways: one was using through the command prompt, and the other through a browser.
- You should use the DesiredCapabilities and the RemoteWebDriver objects to run test scripts on the Grid,.
- DesiredCapabilites is used to set the type of browser and OS that we will automate RemoteWebDriver is used to set which node (or machine) our test will run against.