0% found this document useful (0 votes)
2 views

How to Run Automation Scripts on Selenium Grid

To run automation scripts on Selenium Grid, first set the Hub URL and define browser and platform capabilities using DesiredCapabilities. Then, create a WebDriver instance with RemoteWebDriver to execute tests on the Grid, and configure the execution environment in config.properties. Finally, modify the code to support both local and remote execution, and follow the steps for a standalone setup if needed.

Uploaded by

suresh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

How to Run Automation Scripts on Selenium Grid

To run automation scripts on Selenium Grid, first set the Hub URL and define browser and platform capabilities using DesiredCapabilities. Then, create a WebDriver instance with RemoteWebDriver to execute tests on the Grid, and configure the execution environment in config.properties. Finally, modify the code to support both local and remote execution, and follow the steps for a standalone setup if needed.

Uploaded by

suresh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

How to Run Automation Scripts on

Selenium Grid
After setting up Selenium Grid, the next step is to update our
automation scripts to execute tests in the Grid environment.

Steps to Configure Selenium Grid in Automation Scripts

1. Set the Hub URL

To connect your test script with the Selenium Grid, you need to
specify the Hub URL. The URL format is:

http://<HUB_MACHINE_IP>:<PORT>/wd/hub

• Example 1 (Localhost): http://localhost:4444/wd/hub


• Example 2 (Hub running on a different machine):
http://192.168.13.1:4444/wd/hub

String hubUrl = "http://localhost:4444/wd/hub";

2. Set Browser and Platform Capabilities

To define which Operating System (OS) and Browser your test should
run on, use the DesiredCapabilities class.

Create an instance of DesiredCapabilities:

DesiredCapabilities dc = new DesiredCapabilities();

• Set the Platform (OS):

dc.setPlatform(Platform.WIN11); // Example: Windows 11


// dc.setPlatform(Platform.MAC); // Example: macOS
• Set the Browser:

dc.setBrowserName("chrome");
// dc.setBrowserName("MicrosoftEdge"); // Example:
Microsoft Edge

3. Create WebDriver Instance Using RemoteWebDriver

Instead of using browser-specific WebDriver classes like


ChromeDriver, EdgeDriver, etc., use RemoteWebDriver because we
don’t know which browser and OS the test will run on.

Why RemoteWebDriver?

RemoteWebDriver is the parent class for all WebDriver


implementations and allows execution on remote machines.

Initialize WebDriver with Grid Configuration:

WebDriver driver = new RemoteWebDriver(new URL(hubUrl), dc);

Here, new URL(hubUrl) converts the String hubUrl into a valid URL
object and dc contains the configured platform and browser details.

4. Run Test Cases

Once WebDriver is initialized, the rest of the script remains the same.

driver.get("http://google.com");
System.out.println(driver.getTitle());
driver.quit();
5. Configuring Execution Environment Using config.properties

Before running the tests on Selenium Grid, you need to configure the
execution environment.

• Add a new property in config.properties:

execution_env=remote

When switching from local to remote execution, use the following


method to decide whether to run locally or remotely.

6. Modify Code to Support Local or Remote Execution

Use the following code structure to handle browser setup based on


the environment specified in config.properties:

Code Snippet:
public void setup(String os, String br) {
if (ConfigUtil.getProperty("execution_env").equalsIgnoreCase("remote")) {
// If the execution environment is remote, configure DesiredCapabilities
DesiredCapabilities dc = new DesiredCapabilities();

// Set OS
if (os.equalsIgnoreCase("windows")) {
dc.setPlatform(Platform.WIN11);
} else if (os.equalsIgnoreCase("MAC")) {
dc.setPlatform(Platform.MAC);
} else {
System.out.println("No matching OS");
return;
}
// Set Browser
switch (br.toLowerCase()) {
case "chrome": dc.setBrowserName("chrome"); break;
case "edge": dc.setBrowserName("MicrosoftEdge"); break;
default:
System.out.println("No matching browser");
return;
}

// Create RemoteWebDriver instance


driver = new RemoteWebDriver
(new URL("http://localhost:4444/wd/hub"), dc);
}

if (ConfigUtil.getProperty("execution_env").equalsIgnoreCase("local")) {
// For local execution, no need for DesiredCapabilities,
//just create WebDriver instance based on the browser
switch (br.toLowerCase()) {
case "chrome": driver = new ChromeDriver(); break;
case "edge": driver = new EdgeDriver(); break;
default:
System.out.println("Invalid browser");
return;
}
}
}
7. Standalone Setup (Single Machine)

For a single-machine setup, follow these steps:

Step 1: Download Selenium Server JAR File

Download the Selenium Server JAR file from the official Selenium
website.

Step 2: Run the Selenium Server

Execute the following command to start the Selenium Grid in


standalone mode:

java -jar selenium-server.jar standalone

Step 3: Open the Grid URL

Open the Grid URL in a browser:


http://localhost:4444/

Step 4: Verify Grid Status

Before executing remote session:


No sessions were running.
After executing remote session:
You should be able to see the sessions in the Grid.

Summary

1. Set the Hub URL → http://localhost:4444/wd/hub


2. Define Browser & OS → Using DesiredCapabilities
3. Use RemoteWebDriver → To connect with the Grid
4. Run the test as usual

You might also like