> Markdown version of [Running Tests on Multiple Browsers in a Grid](https://vaadin.com/docs/v14/tools/testbench/testbench-running-test-on-multiple-browsers). Section index: [llms.txt](https://vaadin.com/docs/v14/tools/llms.txt)

# <a id="testbench.grid"></a>Running Tests on Multiple Browsers in a Grid

A distributed test environment ("Test grid") consists of a hub and a number of test nodes. The hub acts as an orchestrator, tracking what browsers are available in the nodes and making sure that a node is only used by one test at a time. The nodes have one or several browsers installed and a node is where the actual test is executed.

When running a test on a hub, the TestBench test asks the hub for a certain browser (based on a list of *capabilities*) instead of launching a local browser. The hub waits until a suitable browser is available on some node, reserves that and redirects the test to that given node. The test is then executed and after it has finished, the node reservation is removed and the node used for another test.

> **Note:** When running on a hub, you do not need a local webdriver installed. The webdriver must be installed on the node instead.

## <a id="testbench.grid.test-remote"></a>Preparing your Tests for Running on a Test Grid

The tests created previously are setup only to run on a single browser, as a single `ChromeDriver` (or other `WebDriver`) instance is created in a `@Before` method. When running on multiple browsers in parallel, it’s easier not to handle the driver instances manually but instead let TestBench handle creation and destruction when needed. To do this, you need to:

1. Extend `ParallelTest` instead of `TestBenchTestCase`. The `ParallelTest` class takes care of creating and destroying driver instances as needed.

2. Define the grid hub URL using either

   1. `@RunOnHub("hub.testgrid.mydomain.com")` on the test class (or a super class) or the system property `com.vaadin.testbench.Parameters.hubHostname`

   2. Configure Sauce Labs credentials and use Sauce Connect proxy to use Sauce Labs test grid. See [Using Sauce Labs Test Grid](#testbench.grid.sauce-labs)

A test class extending `ParallelTest` will automatically:

- Execute test methods in parallel on the hub defined using `@RunOnHub` or the corresponding system property

- Create a suitable webdriver instance

- Terminate the driver after the test ends

- Grab a screenshot if the test fails

- Support running the test locally on only one browser for debugging, using `@RunLocally` or the corresponding system property

> **Note:** When changing the super class of the test, you need to remove any calls to `setDriver(new ChromeDriver())` or similar, and also any `@After` method which does `getDriver().quit()`.

> **Tip:** In almost all cases you want to configure something for all your grid tests so it makes sense to create a common superclass, e.g. `public abstract class AbstractIT extends ParallelTest`. Then you can add a `@RunOnHub` annotation on that class.

> **Note:** Up to 50 test methods in any `ParallelTest` class will be executed simultaneously by default. The limit can be set using the `com.vaadin.testbench.Parameters.testsInParallel` system property. If your tests do not work in parallel set the parameter to `1`.

> **Note:** When running tests in parallel, you need to ensure that the tests are independent and do not affect each other in any way.

> **Note:** You can handle creation and destruction of the webdriver manually also when running on a hub if you do not want to use `ParallelTest` for one reason or another. In this case you should create a `RemoteWebdriver` with the correct hub URL and set suitable `DesiredCapabilities` on the driver.

## <a id="testbench.grid.sauce-labs"></a>Using Sauce Labs Test Grid (since V14.9)

To run your tests on Sauce Labs you first need to get an account at <https://saucelabs.com/> and take note of your user name and access key.

Before starting a test, you need to set up the [Sauce Connect proxy](https://docs.saucelabs.com/secure-connections/sauce-connect/quickstart/). The proxy creates a secure connection between the Sauce Labs browser environment and your machine, so the Sauce Labs browsers can access a server you are running locally. You should run the proxy with a tunnel identifier (`-i some-tunnel-identifier`) to avoid conflicts with other tunnels that you or a CI server might create later.

When the proxy is up and running, you can run your tests by passing the Sauce Labs username and access key to it. This can be done using system properties, e.g.

```sh
mvn verify -Dsauce.user=<yourusername> -Dsauce.sauceAccessKey=<youraccesskey> -Dsauce.tunnelId=some-tunnel-identifier
```

or by first setting the credentials as environment variables and then running the tests, e.g.

```sh
export SAUCE_USERNAME=<yourusername>
export SAUCE_ACCESS_KEY=<youraccesskey>
export SAUCE_TUNNEL_ID=some-tunnel-identifier
mvn verify
```

When writing your tests, there is nothing special about Sauce Labs that needs to be taken into account. You write them as described in [Preparing your Tests for Running on a Test Grid](#testbench.grid.test-remote)

When running tests on a CI/build server, you typically have a build step before the tests, where the Sauce Connect Proxy is started, and a build step after the tests, where the tunnel is torn down.

## <a id="testbench.grid.test-browsers"></a>Defining the Browsers to Run Tests On

You can define the tested browsers and their versions in an environment variable `TESTBENCH_GRID_BROWSERS` or system property `com.vaadin.testbench.Parameters.gridBrowsers` with a comma separated list e.g. `-Dcom.vaadin.testbench.Parameters.gridBrowsers=chrome,firefox,safari-11,safari-9`.

If you don’t want to use the environment variable, you can define the configuration in your test class, in a method annotated with `@BrowserConfiguration`. It returns a list of `DesiredCapabilities`, typically describing what platform, os and browser name/version should be used. Typically this is defined in a superclass for the test so that all tests are run on the same browsers, e.g.

```java
@RunOnHub("hub.testgrid.mydomain.com")
public abstract class AbstractIT extends ParallelTest {

   @BrowserConfiguration
   public List<DesiredCapabilities> getBrowserConfiguration() {
       List<DesiredCapabilities> browsers =
           new ArrayList<DesiredCapabilities>();

       // Add all the browsers you want to test
       browsers.add(BrowserUtil.firefox());
       browsers.add(BrowserUtil.chrome());
       browsers.add(BrowserUtil.ie11());

       return browsers;
   }
}
```

> **Note:** The `BrowserUtil` helper methods create a `DesiredCapability` object which works in many cases. To customize the versions and other values, annotate your test class using `@BrowserFactory(MyBrowserFactory.class)` and implement `MyBrowserFactory` by extending `DefaultBrowserFactory`.

> **Tip:** To run a multi browser test locally, you can use the `com.vaadin.testbench.Parameters.runLocally` system property (or a `@RunLocally` annotation on the test class) to override what browser to run on. The value of the property or annotation should be the browser to run on, e.g. `chrome` or `@RunLocally(Browser.CHROME)`. When `RunLocally` is used, any hub configuration is also ignored and a local webdriver is used.

`97A5876A-5BB7-4EDB-9439-EFF447DE28DA`
