0
0
Selenium Javatesting~15 mins

Selenium Grid setup in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Verify Selenium Grid setup with remote WebDriver
Preconditions (2)
Step 1: Create a RemoteWebDriver instance connecting to the Selenium Grid Hub URL
Step 2: Set desired capabilities for Chrome browser
Step 3: Navigate to https://www.example.com
Step 4: Verify the page title is 'Example Domain'
Step 5: Close the browser session
✅ Expected Result: The test should open Chrome browser on the remote node via Selenium Grid, load the example.com page, verify the title matches 'Example Domain', and close the browser without errors.
Automation Requirements - Selenium WebDriver with TestNG
Assertions Needed:
Assert that the page title equals 'Example Domain'
Best Practices:
Use explicit waits if needed
Use DesiredCapabilities or ChromeOptions properly
Handle exceptions and ensure driver quits in finally block
Use proper locator strategies if interacting with elements (not needed here)
Use TestNG annotations for setup and teardown
Automated Solution
Selenium Java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.net.URL;
import static org.testng.Assert.assertEquals;

public class SeleniumGridTest {
    private WebDriver driver;

    @BeforeClass
    public void setUp() throws Exception {
        ChromeOptions options = new ChromeOptions();
        // Connect to Selenium Grid Hub
        driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), options);
    }

    @Test
    public void testExampleDotComTitle() {
        driver.get("https://www.example.com");
        String title = driver.getTitle();
        assertEquals(title, "Example Domain", "Page title should be 'Example Domain'");
    }

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

The @BeforeClass method sets up the RemoteWebDriver to connect to the Selenium Grid Hub URL with ChromeOptions, which tells Selenium to use Chrome browser on a remote node.

The @Test method navigates to https://www.example.com and asserts the page title is exactly 'Example Domain'. This verifies the remote browser is working correctly.

The @AfterClass method ensures the browser session is closed properly to free resources.

This structure uses TestNG annotations for clear setup, test, and teardown phases, following best practices for Selenium Grid tests.

Common Mistakes - 4 Pitfalls
Hardcoding local WebDriver instead of RemoteWebDriver
Not specifying browser options or capabilities
Not handling exceptions or failing to quit driver
Using incorrect Hub URL or missing /wd/hub suffix
Bonus Challenge

Now add data-driven testing to run the same test on Chrome and Firefox browsers via Selenium Grid.

Show Hint