Selenium Grid is a powerful tool that allows you to run your tests on different machines and browsers simultaneously. This capability is essential for cross-browser testing and helps in reducing the time taken for test execution. In this section, we will guide you through the process of setting up Selenium Grid.

Key Concepts

  1. Hub and Node Architecture:

    • Hub: The central point where you load your tests. It distributes the tests to the registered nodes.
    • Node: The machines where the tests are executed. Nodes can be configured to run different browsers and operating systems.
  2. Parallel Execution: Running multiple tests at the same time across different environments.

  3. Cross-Browser Testing: Ensuring that your application works as expected across different web browsers.

Prerequisites

Before setting up Selenium Grid, ensure you have the following:

  • Java Development Kit (JDK) installed on your machine.
  • Selenium Server Standalone JAR file. You can download it from the Selenium official website.

Step-by-Step Setup

Step 1: Start the Hub

  1. Open a command prompt or terminal window.

  2. Navigate to the directory where the Selenium Server Standalone JAR file is located.

  3. Run the following command to start the hub:

    java -jar selenium-server-standalone-<version>.jar -role hub
    

    Replace <version> with the actual version number of the JAR file you downloaded.

  4. Once the hub is started, you should see a message indicating that the hub is up and running. You can also verify by visiting http://localhost:4444/grid/console in your web browser.

Step 2: Configure and Start a Node

  1. Open another command prompt or terminal window.

  2. Navigate to the directory where the Selenium Server Standalone JAR file is located.

  3. Run the following command to start a node:

    java -jar selenium-server-standalone-<version>.jar -role node -hub http://localhost:4444/grid/register
    
  4. The node will register itself with the hub, and you should see a confirmation message in the terminal.

Step 3: Verify the Setup

  • Open a web browser and go to http://localhost:4444/grid/console.
  • You should see the hub console with the registered node(s) listed.

Practical Example

Here is a simple example of how you can run a test on Selenium Grid:

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 {
        // Define the desired capabilities
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        
        // Create a new instance of the remote web driver
        WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
        
        // Open a website
        driver.get("https://www.example.com");
        
        // Print the title of the page
        System.out.println("Page title is: " + driver.getTitle());
        
        // Close the browser
        driver.quit();
    }
}

Explanation

  • DesiredCapabilities: Used to set the browser type and other configurations for the node.
  • RemoteWebDriver: Connects to the Selenium Grid hub and executes the test on the available node.

Exercise

Task: Set up a Selenium Grid with one hub and two nodes. Configure one node to run Chrome and the other to run Firefox. Write a test script that runs a simple test on both nodes.

Solution

  1. Start the hub as described in Step 1.

  2. Start the first node with Chrome:

    java -jar selenium-server-standalone-<version>.jar -role node -hub http://localhost:4444/grid/register -browser browserName=chrome
    
  3. Start the second node with Firefox:

    java -jar selenium-server-standalone-<version>.jar -role node -hub http://localhost:4444/grid/register -browser browserName=firefox
    
  4. Modify the test script to run on both browsers by changing the DesiredCapabilities accordingly.

Conclusion

Setting up Selenium Grid allows you to efficiently run tests across multiple environments, significantly improving test coverage and execution speed. In the next section, we will explore how to run tests in parallel using Selenium Grid, further enhancing your test automation capabilities.

Test Automation with Selenium

Module 1: Introduction to Test Automation

Module 2: Getting Started with Selenium

Module 3: Locating Web Elements

Module 4: Interacting with Web Elements

Module 5: Synchronization in Selenium

Module 6: Test Frameworks and Selenium

Module 7: Advanced Selenium Concepts

Module 8: Selenium Grid and Parallel Testing

Module 9: Continuous Integration and Selenium

Module 10: Best Practices and Troubleshooting

© Copyright 2024. All rights reserved