Selenium Grid Tutorial
πTutorial-1: Maven and Jenkins Integration
πTutorial-2: Selenium Framework
πTutorial-3: Database Testing with Selenium
πTutorial-4: How to Handle iFrames in Selenium WebDriver Using switchTo()
πTutorial-5: Cross Browser Testing using Selenium
πTutorial-6: Β How to Take Screenshot in Selenium
πTutorial-7: Log4j in Selenium
πTutorial-8: Headless Browser Testing in Selenium
πTutorial-9: Robot Class in Selenium WebDriver
πTutorial-10: AutoIT in Selenium WebDriver
πTutorial-11: Handling SSL Certificate Errors in Selenium
πTutorial-12: How to Handle AJAX Calls in Selenium
πTutorial-13: JavaScriptExecutor in Selenium WebDriver
πTutorial-14: Selenium Python Tutorial
πTutorial-15: How to Set Up IntelliJ IDEA with Selenium
πTutorial-16: Flash Testing Using Selenium WebDriver
πTutorial-17: Apache Ant with Selenium WebDriver
πTutorial-18: How to Generate XSLT Reports in Selenium
πTutorial-19: GitHub Integration with Selenium WebDriver and Jenkins
πTutorial-20: Handling Cookies in Selenium WebDriver
πTutorial-21: Using SoapUI with Selenium WebDriver
πTutorial-22: Cucumber with Selenium WebDriver
πTutorial-23: Selenium Firefox Profile
πTutorial-24: Drag and Drop in Selenium WebDriver with Practical
πTutorial-25: Selenium C# Tutorial
πTutorial-26: Object Repository in Selenium WebDriver
πTutorial-27: How to Scroll a Web Page in Selenium
πTutorial-28: Sikuli Integration with Selenium WebDriver
πTutorial-29: Master XPath in Selenium
πTutorial-30: Selenium Waits
πTutorial-31: Right Click and Double Click in Selenium WebDriver
πTutorial-32: How to Handle Proxy Authentication in Selenium
πTutorial-33: Comprehensive Guide to Exception Handling in Selenium
πSelenium Grid Tutorial: What is Selenium Grid?
Selenium Grid Tutorial: Selenium Grid is a part of the Selenium Suite that allows you to run multiple tests across different browsers, operating systems, and machines in parallel. This is done using a Hub-Node architecture, where:
- The Hub is the central server that manages test execution.
- The Nodes are remote machines that run the tests.
Selenium Grid significantly reduces execution time by running multiple test cases simultaneously.
πSelenium Grid Architecture
1. Hub
β The Hub is the central server where test scripts are loaded.
β There can only be one Hub in a Grid setup.
β The Hub distributes test execution to connected Nodes.
2. Nodes
β Nodes are remote Selenium instances where the tests are executed.
β A Grid can have multiple Nodes running on different machines and browsers.
β Nodes can be on different operating systems than the Hub.
How Selenium Grid Works
1οΈβ£ A user runs a test on the Hub.
2οΈβ£ The Hub routes the test to a Node that matches the required browser and OS configuration.
3οΈβ£ The Node executes the test and sends results back to the Hub.
πHow to Set Up Selenium Grid (Step-by-Step Guide)
Selenium Grid Tutorial: To configure Selenium Grid, we will use two machines:
- Machine A (Hub) β Central server that distributes tests.
- Machine B (Node) β Executes test scripts.
Letβs assume:
- Machine A (Hub) has IP: 192.168.1.3
- Machine B (Node) has IP: 192.168.1.4
Step 1: Download Selenium Server JAR
1οΈβ£ Download the Selenium Standalone Server from the official Selenium website.
2οΈβ£ Save the .jar file in C:\ on both Machine A and Machine B.
Step 2: Start the Hub (Machine A)
1οΈβ£ Open Command Prompt on Machine A.
2οΈβ£ Navigate to C:\ using:
bashcd C:\
3οΈβ£ Start the Selenium Hub using:
pgsqljava -jar selenium-server-standalone-<version>.jar -role hub
4οΈβ£ Selenium Grid Tutorial: The Hub is now running. You can verify by opening a browser and navigating to:
arduinohttp://localhost:4444
Step 3: Start a Node (Machine B)
1οΈβ£ Selenium Grid Tutorial: Open Command Prompt on Machine B.
2οΈβ£ Navigate to C:\ using:
bashCopyEditcd C:\
3οΈβ£ Start the Node and connect it to the Hub using:
pgsqljava -jar selenium-server-standalone-<version>.jar -role node -hub http://192.168.1.3:4444/grid/register
4οΈβ£ Verify the Node is registered by opening the Selenium Grid Console:
arduinohttp://192.168.1.3:4444/grid/console
At this point, your Selenium Grid is successfully set up, and you can start executing test cases remotely.
πWhen to Use Selenium Grid?
Selenium Grid Tutorial: Cross-Browser Testing: Run tests on different browsers (Chrome, Firefox, Edge, etc.).
Cross-Platform Testing: Test applications on various operating systems (Windows, macOS, Linux).
Parallel Test Execution: Reduce test execution time by running multiple tests simultaneously.
πSelenium Grid 1 vs. Selenium Grid 2
| Feature | Selenium Grid 1 | Selenium Grid 2 |
|---|---|---|
| Separate Remote Control | Uses a different RC server | Bundled with Selenium Server |
| Apache Ant Installation | Required | Not required |
| Supports WebDriver? | No | Yes |
| Max Browsers per Remote Control | 1 | Up to 5 |
πDesigning Test Scripts for Selenium Grid
Selenium Grid Tutorial: To execute tests on Selenium Grid, we use:
DesiredCapabilitiesβ Defines the browser and OS for the test.RemoteWebDriverβ Specifies the remote machine (Node) to run the test.
Example: Running Tests on Selenium Grid
javaimport org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.MalformedURLException;
import java.net.URL;
public class SeleniumGridTest {
public static void main(String[] args) throws MalformedURLException {
DesiredCapabilities cap = new DesiredCapabilities();
cap.setBrowserName("chrome");
cap.setPlatform(Platform.WINDOWS);
WebDriver driver = new RemoteWebDriver(new URL("http://192.168.1.3:4444/wd/hub"), cap);
driver.get("https://www.google.com");
System.out.println("Title of the page: " + driver.getTitle());
driver.quit();
}
}
Explanation:
β setBrowserName("chrome") β Specifies the browser.
β setPlatform(Platform.WINDOWS) β Specifies the operating system.
β new RemoteWebDriver(new URL("http://192.168.1.3:4444/wd/hub"), cap) β Connects to the Selenium Hub.
πConfiguring Sele:nium Grid with JSON
Selenium Grid Tutorial: Selenium Grid can also be configured using a JSON file instead of the command line.
Steps to Configure JSON for Selenium Grid
Step 1: Create a JSON Configuration File for Hub
1οΈβ£ Create a new text file hubConfig.json and add:
json{
"port": 4444,
"role": "hub"
}
Step 2: Start the Hub Using JSON
Run this command:
pgsqljava -jar selenium-server-standalone-<version>.jar -role hub -hubConfig hubConfig.json
Step 3: Create a JSON Configuration File for Node
Selenium Grid Tutorial: Create a new text file nodeConfig.json and add:
json{
"capabilities": [{
"browserName": "chrome",
"maxInstances": 5
}],
"hub": "http://192.168.1.3:4444",
"role": "node"
}
Step 4: Start the Node Using JSON
Run this command on Machine B:
pgsql java -jar selenium-server-standalone-<version>.jar -role node -nodeConfig nodeConfig.json
Now your Selenium Grid is configured using JSON.
πSummary
βSelenium Grid Tutorial: Selenium Grid allows parallel test execution across different browsers, OS, and machines.
β Hub-Node architecture distributes test execution efficiently.
β Setup involves running a Hub and multiple Nodes.
β Tests are executed using DesiredCapabilities and RemoteWebDriver.
β JSON configuration provides an alternative setup method.
By setting up Selenium Grid, you can save time, ensure cross-browser compatibility, and improve test efficiency.
