Cross-Browser Testing with Selenium Grid and Java

Hashara Nethmin
2 min readJun 18, 2024

What is Cross-Browser Testing?

Cross-browser testing is making sure your website works properly on different web browsers like Chrome, Firefox, Safari, and Edge. Since people use different browsers to access websites, it’s important to check that your site looks good and functions well on all of them.

What is Selenium Grid?

Selenium Grid is a tool that helps you run tests on many computers with different browsers and operating systems at the same time. This means you can test how your website works on different setups without needing to do it one by one.

Prerequisites

Setting Up

1.1 Install Selenium Server

Download the Selenium Server jar file from the Selenium HQ website.

1.2 Start the Hub Environment.

Start the Selenium Hub that find as the central point.

java -jar selenium-server-standalone-x.xx.x.jar -role hub

1.3 Start the Nodes

You can start a node on the same machine or a different one. Here’s an example of starting a nodes using chrome & Firefox web.

// For Chrome
java -Dwebdriver.chrome.driver=path/to/chromedriver -jar selenium-server-standalone-x.xx.x.jar -role node -hub http://localhost:4444/grid/register -browser "browserName=chrome,maxInstances=5"

// For Firefox
java -Dwebdriver.gecko.driver=path/to/geckodriver -jar selenium-server-standalone-x.xx.x.jar -role node -hub http://localhost:4444/grid/register -browser "browserName=firefox,maxInstances=5"

1.4 Writing the Test Script

Here’s a test script to run a simple test on multiple browsers using Selenium Grid.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import java.net.MalformedURLException;
import java.net.URL;

public class CrossBrowserTest {
WebDriver driver;
String url = "http://example.com";

@BeforeClass
@Parameters({"browser"})
public void setUp(String browser) throws MalformedURLException {
String hub_URL = "http://localhost:4444/wd/hub";
DesiredCapabilities capabilities = new DesiredCapabilities();

if(browser.equalsIgnoreCase("chrome")) {
capabilities.setBrowserName("chrome");
} else if(browser.equalsIgnoreCase("firefox")) {
capabilities.setBrowserName("firefox");
}

driver = new RemoteWebDriver(new URL(hubURL), capabilities);
}

@Test
public void testCrossBrowser() {
driver.get(baseUrl);
Assert.assertEquals(driver.getTitle(), "Example Domain");
}

@AfterClass
public void tearDown() {
if (driver != null) {
driver.quit();
}
}
}

1.5 Configuring TestNG XML for Parallel Execution

Create a testng.xml file to configure and run the tests in parallel.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="CrossBrowserSuite" parallel="tests" thread-count="2">
<test name="Chrome">
<parameter name="browser" value="chrome"/>
<classes>
<class name="Cross-Browser Testing"/>
</classes>
</test>

<test name="Firefox">
<parameter name="browser" value="firefox"/>
<classes>
<class name="Cross-Browser Testing"/>
</classes>
</test>
</suite>

Scalability

To handle more tests, you can add more computers with different setups. Selenium Grid helps by running tests on many computers at the same time, and you can manage everything through the Selenium Grid console.

--

--

Hashara Nethmin

With a passion for tech writing. My interests lie at the intersection of technology and creativity,Through my writing.