Parallel execution of tests using Selenium-Grid and TestNG - Perficient Blogs
Blog
  • Topics
  • Industries
  • Partners

Explore

Topics

Industries

Partners

Parallel execution of tests using Selenium-Grid and TestNG

Selenium-Grid allows you to run your tests on different machines against different browsers in parallel. That is, running multiple tests at the same time against different machines running different browsers and operating systems. Essentially, Selenium-Grid support distributed test execution. It allows for running your tests in a distributed test execution environment.

When to use

  1. To reduce the time it takes for the test suite to complete a test pass.
  2. To run your tests against multiple browsers, multiple versions of browser, and browsers running on different operating systems.

Selenium environment set up

  1. Download java and install
  2. Setting up the path for windows
  3. Download IE driver/Chrome driver
  4. Put IE driver/Chrome driver into system environment variables
  5. Download FF/Chrome/IE
  6. Download selenium-server-standalone-*.jar

Note: You need set up the environment for both hub and node.

How to start

Start hub:

java -jar selenium-server-standalone-2.45.0.jar -role hub

Start node:

java -jar selenium-server-standalone-2.45.0.jar -role node -port 5558 -hub http://10.2.20.84:4444/grid/register

Here 10.2.20.84 is the hub’s IP, we can use ipconfig command to get it. And 4444 is the default port (can be change by –port if you don’t want to use the default port). Obviously, you can start more than one node using the code above. One thing need to be careful is the port. When you start more than one node, you should using different port number to prevent the risk that port is already in used on your system.

1

When you got the information like “Registering the node to hub: xxxxxxxx”, that means node is ready to be used. You can use link http:// 10.2.20.84:4444/grid/console to get the node’s status.

Now selenium grid has been set up successfully, we can run a small case:

2

Using RemoteWebDriver() to connect the hub, and the connection information is http://10.2.20.84:4444/wd/hub. After connected to hub, there will create a session on hub, meanwhile, hub will mapping the appropriate node depending on the request.

TestNG Configuration

parallel: set the default mechanism used to determine how to use parallel threads when running tests.

thread-count: set the default maximum number of threads to use for running tests in parallel

3

Here we set the parallel value as “tests” and thread-count as “2”. Then when we run scripts as testNG suite, it will use 2 parallel threads for tests. Also you can change parallel value to methods or classes. And the thread-count can be 3, 4, 5… It depends on how many parallel threads you want.

Best Practice

  1. Port: When you start more than one node in one machine, you should using different port number to prevent the risk that port is already in used on your system.
  2. Do not use “static” variable to initialize driver with parallel execution of scripts. When you define your field as static, then every thread has access to it and every thread can modify that memory space. Initialize driver with “static” works fine with non-parallel execution of scripts but not parallel execution.

Reference

http://www.seleniumhq.org/

http://www.testng.org/

TAGS  

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to the Weekly Blog Digest:

Sign Up

Categories