Selenium Grid

Back

Loading concept...

๐Ÿš€ Selenium Grid: Your Test Army Command Center

Imagine youโ€™re a general with one soldier. You can test one enemy position at a time. But what if you had an ARMY? Thatโ€™s Selenium Grid!


๐ŸŽฏ The Big Picture

Think of Selenium Grid like a Pizza Delivery HQ:

  • You have ONE phone center (the Hub) that takes all orders
  • You have MANY delivery drivers (the Nodes) ready to deliver
  • Customers can order ANY pizza type (different browsers)
  • Multiple deliveries happen at the SAME time (parallel execution)

Result? Tests that took 2 hours now take 10 minutes! ๐ŸŽ‰


๐Ÿ—๏ธ Grid Architecture

What Is It?

Selenium Grid is a smart server that lets you run tests on MANY computers at once.

graph TD A["Your Test Code"] --> B["Hub - The Boss"] B --> C["Node 1 - Chrome"] B --> D["Node 2 - Firefox"] B --> E["Node 3 - Safari"]

The Two Heroes

Component Job Real-World Example
Hub Traffic controller Airport control tower
Node Test runner Individual planes

Simple Example

// Your test talks to the Hub
// Hub finds the right Node
// Node runs your test
// Results come back to you!

Why This Rocks:

  • โœ… One place to manage everything
  • โœ… Tests run on many machines
  • โœ… No need to install browsers everywhere

๐Ÿ”ง Hub and Node Setup

Step 1: Start the Hub

The Hub is your command center. Start it first!

java -jar selenium-server.jar hub

Thatโ€™s it! Hub runs on http://localhost:4444

Step 2: Start Nodes

Nodes are your workers. They do the actual testing.

java -jar selenium-server.jar node

The Node automatically finds and joins the Hub!

Visual: How They Connect

graph TD H["Hub :4444"] --> N1["Node 1"] H --> N2["Node 2"] H --> N3["Node 3"] N1 --> B1["Chrome"] N2 --> B2["Firefox"] N3 --> B3["Edge"]

Pro Setup Example

# Start Hub on specific port
java -jar selenium-server.jar hub \
  --port 4444

# Start Node connecting to Hub
java -jar selenium-server.jar node \
  --hub http://hub-ip:4444

Remember:

  • ๐ŸŸข Start Hub FIRST
  • ๐ŸŸข Then start Nodes
  • ๐ŸŸข Nodes find Hub automatically

๐ŸŒ Remote WebDriver Connection

The Magic Bridge

Instead of talking to a local browser, your code talks to the Grid!

Before (Local):

WebDriver driver = new ChromeDriver();
// Runs on YOUR machine only

After (Remote):

URL hubUrl = new URL(
  "http://localhost:4444/wd/hub"
);

ChromeOptions options =
  new ChromeOptions();

WebDriver driver = new RemoteWebDriver(
  hubUrl,
  options
);
// Runs on ANY available Node!

What Changed?

Local Remote
new ChromeDriver() new RemoteWebDriver(url, options)
Runs on your PC Runs on Grid Node
One browser Many browsers possible

Key Parts

  1. Hub URL - Where to send requests
  2. Capabilities - What browser you want
  3. RemoteWebDriver - The connection magic
// Full example
DesiredCapabilities caps =
  new DesiredCapabilities();
caps.setBrowserName("firefox");

RemoteWebDriver driver =
  new RemoteWebDriver(
    new URL("http://grid:4444/wd/hub"),
    caps
  );

๐ŸŽฏ Distributed Test Execution

What Does โ€œDistributedโ€ Mean?

Your tests run on different machines at the same time!

graph LR T["Test Suite"] --> H["Hub"] H --> M1["Machine 1"] H --> M2["Machine 2"] H --> M3["Machine 3"]

Why Itโ€™s Amazing

Without Grid:

  • 100 tests ร— 1 minute each = 100 minutes โฐ

With Grid (10 Nodes):

  • 100 tests รท 10 nodes = 10 minutes! ๐Ÿš€

Real Example

// Test 1 goes to Node A
// Test 2 goes to Node B
// Test 3 goes to Node C
// All run at the SAME time!

Setting Up Distribution

Your test framework handles this. Example with TestNG:

<suite name="Distributed"
       parallel="tests"
       thread-count="5">
  <test name="Test1">
    <!-- runs on available node -->
  </test>
  <test name="Test2">
    <!-- runs on another node -->
  </test>
</suite>

Key Benefit: Hub automatically sends tests to available Nodes!


๐ŸŒˆ Cross-Browser Testing

The Problem

Your website must work on:

  • Chrome ๐ŸŸข
  • Firefox ๐ŸŸ 
  • Safari ๐Ÿ”ต
  • Edge ๐Ÿ”ท

Testing one by one? Painful!

The Solution

Grid runs ALL browsers at once!

graph TD T["Your Test"] --> H["Hub"] H --> C["Chrome Node"] H --> F["Firefox Node"] H --> S["Safari Node"] H --> E["Edge Node"]

Code Example

// For Chrome
ChromeOptions chrome =
  new ChromeOptions();
WebDriver chromeDriver =
  new RemoteWebDriver(hubUrl, chrome);

// For Firefox
FirefoxOptions firefox =
  new FirefoxOptions();
WebDriver firefoxDriver =
  new RemoteWebDriver(hubUrl, firefox);

The Magic: Capabilities

Tell Grid what you need:

DesiredCapabilities caps =
  new DesiredCapabilities();

// I want Firefox!
caps.setBrowserName("firefox");

// On Windows!
caps.setPlatform(Platform.WINDOWS);

// Hub finds matching Node

Browser Coverage Table

Browser Node OS Use Case
Chrome Windows Most users
Safari macOS Apple users
Firefox Linux Dev testing
Edge Windows Corporate

โšก Parallel Test Execution

The Speed Secret

Parallel = Running tests side by side, not one after another.

Visual Comparison

Sequential (Slow):

Test1 โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ
              Test2 โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ
                            Test3 โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ
Total: 24 seconds

Parallel (Fast):

Test1 โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ
Test2 โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ
Test3 โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ
Total: 8 seconds!

How to Enable Parallel

TestNG Example:

<suite parallel="methods"
       thread-count="10">
  <test name="ParallelTest">
    <classes>
      <class name="MyTests"/>
    </classes>
  </test>
</suite>

JUnit 5 Example:

# junit-platform.properties
junit.jupiter.execution
  .parallel.enabled=true
junit.jupiter.execution
  .parallel.mode.default=concurrent

Best Practices

  1. Independent Tests - Tests shouldnโ€™t share data
  2. Thread-Safe Code - No shared variables
  3. Unique Test Data - Each test uses own data
// GOOD - Each test has own driver
@Test
void testLogin() {
  WebDriver driver = createDriver();
  // test code
  driver.quit();
}

// BAD - Shared driver causes problems
static WebDriver sharedDriver; // Don't!

Scaling Formula

Nodes Tests Time Saved
1 100 0%
5 100 80%
10 100 90%
20 100 95%

๐ŸŽฎ Putting It All Together

Complete Setup Flow

graph TD A["1. Download Selenium Server"] --> B["2. Start Hub"] B --> C["3. Start Nodes"] C --> D["4. Write Remote Tests"] D --> E["5. Run in Parallel"] E --> F["6. Get Fast Results!"]

Quick Start Commands

# Terminal 1: Start Hub
java -jar selenium-server.jar hub

# Terminal 2: Start Chrome Node
java -jar selenium-server.jar node \
  --detect-drivers true

# Terminal 3: Run your tests!
mvn test

Complete Test Example

public class GridTest {
  WebDriver driver;

  @BeforeEach
  void setup() throws Exception {
    URL hub = new URL(
      "http://localhost:4444/wd/hub"
    );

    ChromeOptions options =
      new ChromeOptions();

    driver = new RemoteWebDriver(
      hub, options
    );
  }

  @Test
  void testGoogle() {
    driver.get("https://google.com");
    // Your test here
  }

  @AfterEach
  void teardown() {
    if (driver != null) {
      driver.quit();
    }
  }
}

๐Ÿ† Key Takeaways

Concept One-Line Summary
Grid Run tests on many machines
Hub The boss that routes tests
Node The worker that runs tests
Remote WebDriver Connect to Grid instead of local
Distributed Tests spread across machines
Cross-Browser Test all browsers at once
Parallel Tests run at same time

๐Ÿš€ Your Next Steps

  1. โœ… Download Selenium Server JAR
  2. โœ… Start a Hub locally
  3. โœ… Connect one Node
  4. โœ… Convert one test to RemoteWebDriver
  5. โœ… Watch the magic happen!

โ€œOne test at a time is like having one checkout lane at a supermarket. Selenium Grid opens ALL the lanes!โ€ ๐Ÿ›’

Youโ€™ve got this! ๐Ÿ’ช

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.