How to Run Automation Scripts on Selenium Grid
How to Run Automation Scripts on Selenium Grid
Selenium Grid
After setting up Selenium Grid, the next step is to update our
automation scripts to execute tests in the Grid environment.
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
To define which Operating System (OS) and Browser your test should
run on, use the DesiredCapabilities class.
dc.setBrowserName("chrome");
// dc.setBrowserName("MicrosoftEdge"); // Example:
Microsoft Edge
Why RemoteWebDriver?
Here, new URL(hubUrl) converts the String hubUrl into a valid URL
object and dc contains the configured platform and browser details.
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.
execution_env=remote
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;
}
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)
Download the Selenium Server JAR file from the official Selenium
website.
Summary