To run Selenium WebDriver tests in parallel, we need to set up the Selenium Grid Server as a Hub. The Selenium Server hub will provide the available configurations or capabilities to the Selenium WebDriver tests. The slave machines, also called node, connect to the hub for parallel execution. Selenium WebDriver tests use the JSON wire protocol to talk to the Hub to execute Selenium commands.
The Hub acts like the central point that will receive the entire test request and distribute it to the right nodes.
In this recipe, we will set up a Selenium Grid Server and then add nodes with different OS and browser combinations. We will then use this setup to run tests in parallel using TestNG.
Download the latest Selenium Server standalone JAR file from http://docs.seleniumhq.org/download/. For this recipe, the selenium-server-standalone-2.47.1 version is used.
Setting up a Hub is a fairly simple job. Launch the Selenium Server using the following command:
java -jar selenium-server-standalone-2.46.0.jar -port 4444 -role hub -nodeTimeout 600
This command will start the Selenium Server in Hub role with the following output on the command prompt:
When we launch the Selenium Standalone Server in Hub role, it starts listening to nodes and Selenium tests on port 4444. If you browse to http://localhost:4444/grid/console
on the Hub machine, it will display the following information in the browser:
To run tests with Selenium Grid, Selenium WebDriver tests need to use the instance of the RemoteWebDriver
and DesiredCapabilities
classes to define which browser, version, and platform tests will be executed. Based on preferences set in the DesiredCapabilities
instance, the Hub will point the test to a node that matches these preferences. In this example, Hub will point the test to a node running on the Windows operating system and Firefox browser with the specified version:
DesiredCapabilities cap = new DesiredCapabilities(); cap.setBrowserName("firefox"); cap.setPlatform(org.openqa.selenium.Platform.WINDOWS);
These preferences are set using the setBrowserName()
, setVersion()
, and setPlatform()
methods of the DesiredCapabillities
class.
An instance of the DesiredCapabilities
class is passed to RemoteWebDriver
:
driver = new RemoteWebDriver(new URL("http://192.168.1.100:4444/wd/hub"),cap);
In this example, the test is connecting to the Hub running on http://192.168.1.100:4444/wd/hub
with the instance of DesiredCapabilities
named cap
.
3.145.70.38