What is Selenium Grid?
Selenium Grid is a part of the Selenium Suite that specializes in running multiple tests across different browsers, operating systems, and machines in parallel. It allows for simultaneous running of tests in different browsers and environments, saving considerable time. With Selenium Grid, one server acts as the hub. Tests contact the hub to access browser instances. The hub has a list of servers that provide access to browser instances (WebDriver nodes) and lets tests use these instances. Selenium Grid allows running tests in parallel on multiple machines, managing different browser versions and browser configurations centrally (hub), and allows for running tests on a large scale on various environments.
Selenium Grid Architecture
The architecture of Selenium Grid consists of a single hub and multiple nodes. The hub is the central point where the tests are loaded, and it is also responsible for distributing the tests across multiple nodes. The nodes, on the other hand, are the test environments where the web browsers reside, and these are the environments where the tests are executed.
For example, let's say you have a test suite that needs to be executed on Chrome, Firefox, and Safari. You set up a hub on a server machine and connect three nodes to this hub. Each node is a different machine that has a different browser installed. Node 1 has Chrome, Node 2 has Firefox, and Node 3 has Safari.
When you execute the test suite, the hub receives the tests and based on the test configuration, distributes them to the appropriate nodes. Tests that need to be run on Chrome are sent to Node 1, those for Firefox to Node 2, and so on. This way, all tests can be run in parallel, greatly reducing the overall test execution time.
The prerequisites for using Selenium Grid are:
Java: Selenium Grid is built on Java, so you need to have Java installed on your system. Ensure that you have the Java Development Kit (JDK) for running the programs.
Selenium Server JAR: You need to download the Selenium Server JAR file. It acts as a standalone server that implements WebDriver's wire protocol.
Web Browsers: You need to have the desired web browsers installed on your system on which you want to run the tests.
WebDriver: You need the WebDriver for the respective browsers. WebDriver is a tool for automating web application testing. Selenium Manager will configure the drivers automatically if you add --selenium-manager true.
Test Environment: You need a test environment set up. This could be your local system, virtual machines, or even cloud-based platforms.
Test Scripts: You need to have your test scripts ready to be executed on the Selenium Grid.
How to set up Selenium Grid?
Download the latest version of selenium server from Seleniumdev.com
(Please ensure you download the latest and most stable selenium server)
To start the hub:
java -jar selenium-server-4.18.1.jar hub
Open any browser and enter http://192.168.0.196:4444 this shows the hub is ready and have windows and have
these many browsers installed and ready to use. (The URL is different for each system as it takes system IP address)
To start the Node:
Now to register the Nodes in the same machine, Open the command prompt and navigate to a folder where the server is located. Run the server by using below command.
Node 1 same machine and default port
java -jar selenium-server-4.18.1.jar node --selenium-manager true
Node 2 same machine and different port
java -jar selenium-server-4.18.1.jar node --selenium-manager true --port 6666
To register the Nodes in a different machine, Download the selenium server jar and the drivers in that machine and Run the below command.
java -jar selenium-server-4.12.1.jar node --selenium-manager true --publish-events tcp://<ipaddressofhub> — subscribe-events tcp://<ipaddressofhub>
According to our setup we can run the below command.
java -jar selenium-server-4.12.1.jar node --selenium-manager true --publish-events tcp://192.168.0.196:4442 --subscribe-events tcp://192.168.0.196:4443
How to Setup Test Suit:
Create a simple TestNg Maven project and import the required dependency.
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.Test;
public class GoogleTest {
private WebDriver driver;
@Test(priority = 0)
public void getTitle() throws MalformedURLException {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setBrowserName("chrome");
WebDriver driver = new RemoteWebDriver(new URL("http://192.168.0.196:4444"), caps);
driver.get("https://google.com");
System.out.println(driver.getTitle());
driver.findElement(By.name("q")).sendKeys("Numpy Ninja");
driver.close();
}
@Test(priority = 1)
public void NumpyNinjaTitle() throws MalformedURLException {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setBrowserName("MicrosoftEdge");
driver = new RemoteWebDriver(new URL("http://192.168.0.196:4444"), caps);
driver.get("https://www.numpyninja.com/");
System.out.println(driver.getTitle());
driver.close();
}
}
The below image shows your test is run on two different browsers as we set in our test case.
Advantages of Using Selenium Grid
Parallel Execution: Selenium Grid allows for the simultaneous execution of tests on different browsers and environments. This greatly reduces the overall test execution time, making it ideal for large test suites.
Multi-Browser Testing: It enables testing on multiple browsers, ensuring that your application works smoothly across all supported browsers.
Multi-Environment Testing: With Selenium Grid, you can test your application on different operating systems and devices, ensuring full coverage and compatibility.
Centralized Management: The hub in the Selenium Grid acts as a central point for managing and controlling the test execution on different nodes.
Scalability: Selenium Grid provides the ability to scale your testing efforts by adding more nodes to the grid. This is particularly useful for larger projects with high testing demands.
Efficiency: By running tests in parallel and on different environments, Selenium Grid can significantly increase the efficiency of your testing processes.
Disadvantages of Using Selenium Grid
Complex Setup: Setting up a Selenium Grid environment can be quite complex, especially when dealing with a large number of nodes and different environments.
High Resource Consumption: Running tests on multiple browsers and operating systems simultaneously can consume a lot of system resources, potentially slowing down other operations.
Maintenance Overhead: Keeping the grid up to date with the latest browser and driver versions can be time-consuming. There's also the need to ensure compatibility between different components.
Requires High Technical Expertise: To effectively use and manage Selenium Grid, a high level of technical knowledge and experience is necessary.
Possible Synchronization Issues: As tests are distributed across different nodes for parallel execution, there can be synchronization issues that could affect test results.
Not Ideal for Unit Testing: Selenium Grid is designed for end-to-end tests. For unit testing, other tools may be more efficient.
Comments