# How to Configure Selenium Grid Remote Execution with TargetFactory in anhtester/automationframeworkselenium

> Configure Selenium Grid remote execution with TargetFactory in anhtester/automationframeworkselenium. Learn how TargetFactory routes WebDriver to your Selenium Grid for efficient parallel testing.

- Repository: [Anh Tester/automationframeworkselenium](https://github.com/anhtester/automationframeworkselenium)
- Tags: how-to-guide
- Published: 2026-02-24

---

**The `TargetFactory` class routes WebDriver instantiation to a remote Selenium Grid by reading the `TARGET` property from `FrameworkConstants` and constructing a `RemoteWebDriver` connection using configured `REMOTE_URL` and `REMOTE_PORT` values.**

The anhtester/automationframeworkselenium framework abstracts browser instantiation through a factory pattern that supports both local and distributed execution. Configuring Selenium Grid remote execution with TargetFactory requires modifying property files or system variables rather than changing test code, enabling seamless environment switching for CI/CD pipelines. The architecture ensures identical browser capabilities whether running locally or against a Grid hub.

## Understanding the TargetFactory Architecture

The `TargetFactory` class located in [`src/main/java/com/anhtester/driver/TargetFactory.java`](https://github.com/anhtester/automationframeworkselenium/blob/main/src/main/java/com/anhtester/driver/TargetFactory.java) serves as the central dispatcher for WebDriver creation. It evaluates the execution target at runtime and delegates to either local browser factories or remote Grid initialization.

### Target Enum and Configuration Constants

The framework defines execution modes through the **Target** enum in [`src/main/java/com/anhtester/enums/Target.java`](https://github.com/anhtester/automationframeworkselenium/blob/main/src/main/java/com/anhtester/enums/Target.java), which supports `LOCAL` and `REMOTE` values. At startup, `TargetFactory` retrieves the target mode from `FrameworkConstants`:

```java
Target target = Target.valueOf(FrameworkConstants.TARGET.toUpperCase());

```

The `FrameworkConstants` class in [`src/main/java/com/anhtester/constants/FrameworkConstants.java`](https://github.com/anhtester/automationframeworkselenium/blob/main/src/main/java/com/anhtester/constants/FrameworkConstants.java) loads these values from property files under `src/test/resources/config/`:

```java
public static final String REMOTE_URL = PropertiesHelpers.getValue("REMOTE_URL");
public static final String REMOTE_PORT = PropertiesHelpers.getValue("REMOTE_PORT");
public static final String TARGET = PropertiesHelpers.getValue("TARGET");

```

### Local vs Remote Execution Logic

When `TARGET` is set to `REMOTE`, the factory invokes `createRemoteInstance()` instead of local browser initialization. This method constructs the Grid endpoint URL and instantiates a **RemoteWebDriver** with the appropriate capabilities:

```java
private RemoteWebDriver createRemoteInstance(MutableCapabilities capability) {
    String gridURL = String.format("http://%s:%s",
            FrameworkConstants.REMOTE_URL,
            FrameworkConstants.REMOTE_PORT);
    LogUtils.info("Remote URL: " + gridURL);
    return new RemoteWebDriver(new URL(gridURL), capability);
}

```

The **MutableCapabilities** are supplied by `BrowserFactory` ([`src/main/java/com/anhtester/driver/BrowserFactory.java`](https://github.com/anhtester/automationframeworkselenium/blob/main/src/main/java/com/anhtester/driver/BrowserFactory.java)), ensuring that browser-specific options like headless mode and security settings remain consistent between local and remote executions.

## Step-by-Step Grid Configuration

### Property File Configuration

To enable remote execution, modify `src/test/resources/config/config.properties` with your Grid hub details:

```properties
TARGET = remote
REMOTE_URL = 192.168.1.10
REMOTE_PORT = 4444

```

Setting `TARGET = remote` triggers the remote execution path in `TargetFactory.createInstance()`, while `REMOTE_URL` and `REMOTE_PORT` define the Selenium Grid hub address. The framework automatically propagates these values through `PropertiesHelpers` without requiring code changes.

### Runtime System Property Overrides

For dynamic configuration in CI/CD environments, set system properties before initializing the test suite:

```java
public class CISetup {
    static {
        System.setProperty("TARGET", "remote");
        System.setProperty("REMOTE_URL", "grid.mycompany.com");
        System.setProperty("REMOTE_PORT", "5555");
    }
}

```

The `PropertiesHelpers` utility checks system properties first, then falls back to file-based configuration, enabling environment-specific overrides without file modifications.

### Docker Grid Quick Start

To verify your configuration locally, start a standalone Selenium Grid using Docker:

```bash
docker run -d -p 4444:4444 --name selenium-hub selenium/standalone-chrome

```

For a full Grid with separate hub and nodes, use the provided Docker Compose configuration:

```bash
docker compose -f docker-compose-grid.yml up -d

```

Once the container reports readiness at `http://localhost:4444`, set `REMOTE_URL = localhost` and `REMOTE_PORT = 4444` in your properties file to connect the framework.

## Implementation in Test Classes

When tests extend `BaseTest`, the `setupDriver()` method automatically invokes `TargetFactory.createInstance()`. You can also instantiate the factory directly for custom implementations:

```java
import com.anhtester.driver.TargetFactory;
import org.openqa.selenium.WebDriver;

public class RemoteExecutionDemo {
    public static void main(String[] args) {
        WebDriver driver = new TargetFactory().createInstance("chrome");
        driver.get("https://example.com");
        driver.quit();
    }
}

```

This approach respects the `TARGET` configuration, creating either a local ChromeDriver or a RemoteWebDriver connected to your Grid based on the current property values.

## Summary

- **TargetFactory** in [`src/main/java/com/anhtester/driver/TargetFactory.java`](https://github.com/anhtester/automationframeworkselenium/blob/main/src/main/java/com/anhtester/driver/TargetFactory.java) serves as the central dispatcher for local and remote WebDriver creation.
- Set `TARGET = remote` in `config.properties` to route execution through Selenium Grid.
- Configure `REMOTE_URL` and `REMOTE_PORT` to specify your Grid hub endpoint.
- The **RemoteWebDriver** instance receives capabilities from `BrowserFactory`, ensuring configuration parity with local runs.
- System properties override file configuration for dynamic CI/CD environments.
- No test code changes are required when switching between local and remote execution.

## Frequently Asked Questions

### How do I change the Selenium Grid URL without modifying property files?

Set the `REMOTE_URL` and `REMOTE_PORT` system properties before the framework initializes. The `PropertiesHelpers` class loads system properties with higher priority than file values, allowing CI pipelines to inject Grid endpoints dynamically.

### What happens if TARGET is set to an invalid value?

The `Target.valueOf()` call in `TargetFactory` throws an `IllegalArgumentException` if the configured `TARGET` string does not match the enum constants `LOCAL` or `REMOTE`. Ensure the value is uppercase or use `toUpperCase()` conversion as implemented in the source.

### Are browser capabilities identical between local and remote execution?

Yes. Both execution paths use `BrowserFactory.getOptions()` to generate **MutableCapabilities**, ensuring that headless settings, window sizes, and security arguments remain consistent whether running locally or on the Grid.

### Can I use TargetFactory without extending BaseTest?

Yes. Instantiate `TargetFactory` directly and call `createInstance(String browser)` to obtain a WebDriver instance. The factory independently reads `FrameworkConstants` and configures the connection accordingly, making it suitable for custom test runners or standalone scripts.