Selenium Grid and Jenkins Integration

Introduction

Raghavendra Kumara
Clairvoyant Blog

--

In the Agile/Scrum process, applications with a lot of automation test cases often have concerns around execution time to complete the execution of a test suite that has 500+ test cases. A possible solution to address this is by implementing a framework that supports parallel execution using the selenium grid.

With applications supporting multiple browsers like Chrome, Firefox, IE, on different OS versions and also compatibility on mobile devices, it’s important that the automation test cases certify for all these business needs. You can obtain more information regarding this here.

Selenium grid is one of the many solutions to this and more feasible than other tools as this is easy to maintain and more cost-effective.

Hub Configuration

Hub can be started with default parameters by running a command from a command-line.

java -jar selenium-server-standalone-x.xx.x.jar -role hub
Role specifies if the machine acts like a node or a hub. Jar file is the selenium standalone server

To specify a particular port for the hub, the command to be used for launching hub is

java -jar selenium-server-standalone-x.xx.x.jar -role hub -port 4445

Technically, a machine can be configured to be more than one hub making sure the ports are different for each hub.

Node Configuration

Once a hub is started, any number of machines can be connected to that hub as nodes. Node is started using a similar command with role as node and specify the hub details in that command.

java -DWebdriver.ie.driver=path of iedriver on node -jar selenium-server-standalone-x.xx.x.jar -port xxxx -role node -hub http://hostname:port/grid/register -browser “browserName=internet explorer, version=11,platform=WINDOWS, maxInstances=1”

maxInstances can be any number and that drives the number of instances in a node, which will run that many number of test cases in that one node.

Note: A machine can be both hub and node provided both are started with different ports.

A node can be started even when the hub is not started however, it’s recommended to start the hub first and then the node(s).
Each node will be started with a default port if not specified in the command. For successful parallel execution, each node has to be started with a unique port.
Selenium grid console will display the number of nodes connected and number of instances of each driver initiated in the grid (as shown below)

Selenium Grid Console

In the above screenshot, node is initiated with 5 instances of Firefox, 1 instance of IE and 5 instances of chrome driver.

Parallel execution

As the grid with hub and nodes is up and running, testNG xml needs to be modified to run test cases on selenium grid.

TestNG xml changes:

In the xml code above, the parallel type defines the way test cases will be run in parallel.
TestNG supports parallel by classes, tests, methods, instances.

Thread-count is the number of threads or number of testcases that need to be run in parallel at a time.

Typically, when all the nodes are started with only one instance each, the thread-count matches the number of nodes connected to the hub. During designing the grid, its advised to make sure that the thread-count is less than or equal to number of nodes to make sure there are no test cases waiting in the queue always for the nodes to be freed.

As the browser type and version of the browser are also parameterized, the same xml can be modified to run on a different browser and version based on the business needs to certify the application under test.

Based on the parameters, drivers are initialized and desired capabilities are set to launch the respective driver and version on the nodes as shown below.

Types of Parallel execution:

A sample testNG xml is attached below

In the above xml, the block with <test> </test> is referred as the test block and when “parallel=tests” is used in the xml, the tests are run in a way that all the test cases in the classes of one test block are run in one node and all the test cases in the classes of other test block are run in another node and these two nodes are running tests in parallel.

Similarly, when the “parallel=methods” is used, all the test cases on 1st class in the 1st test block are run in parallel in number of nodes connected to the grid.

When “parallel=classes” is used, all the test cases of one class run in one node and all the test cases of other class run in the other node and both these are running test cases in parallel.

Code to launch RemoteWebDriver on Grid:

Parameters from TestNG xml for seleniumHost and seleniumPort are used in the code to launch remote web driver.

To also accommodate to run test cases locally, specify gridURL as localhost and use an “if condition” in the code to check for localhost and if the host is localhost, don’t launch test cases on gird.

Integration with cloud-based tools for parallel execution

Cloud based tools like Saucelabs provide support to run test cases on multiple versions of the browsers and OS versions and certify the application under test for the business needs.

It supports debugging options by setting up capabilities in the code and the results can be recoded and viewed later for any error logs during the tests which are also accessible using the rest API.

To integrate framework with a cloud-based tool like saucelabs or browserStack or PerfectoMobile, all that is needed is to provide the grid_url parameter that point to these tools.

URL that connects to the saucelabs is mentioned below,

Provide this url under grid_url parameter in the testNG xml and the test cases will be now run on saucelabs instead of a local grid.

Desired Capabilities can be parameterized to be used from the testNG xml just like that of the local host.

Integration with Jenkins

For the integration with Jenkins, plugin with respect to the source code management needs to be installed on the Jenkins server. Once all the plugins are installed, create a new job by clicking on New item in Jenkins.

Source Code Management section will display the sources based on the plugins installed.

Choose any source and provide the url as well as the credentials related to the repository that Jenkins needs to build. Credentials are added in the Credentials provider (shown below)

Once all the options are provided, to have the job triggered, maven command needs to be provided under goals.

Typically, a maven command used is mvn clean install -DSuiteFile=SanityTest.xml. Name of the TestNG xml is parameterized in the pom.xml under the build tag as shown below

--

--