TestNG | Parallel execution in Cucumber

TestNG provides an ability to run test classes in parallel. By using a parallel execution of classes, each class will be started and executed simultaneously in different threads. Because many devices are available in the Perfecto Lab, we can achieve more test coverage in less time.

Important: This document includes references to a third-party product, TestNG. The user interface and usage of third-party products are subject to change without notice. For the latest published information about TestNG, see https://testng.org/doc/documentation-main.html.

Parallel execution

After adding the TestNG framework and passing parameters from the testng.xml file, we can now leverage our framework to run on multiple tests on multiple devices in parallel. Only one test can run on one device at a time, which means we need to create a different driver for every method.

With parallel execution, we can run the same test on different device models to achieve more coverage or run different tests on the same device models to cut the execution time and get faster results for a specific model. For this to happen, we need to pass parameters to our testng class and also in the XML file.

TestNG allows the user to pass values to test methods as arguments by using parameter annotations through the testng.xml file. Sometimes, we may have to pass values to test methods during runtime. For example, we can pass the user name and password through testng.xml instead of hard coding them in testmethods, or we can pass the browser name as a parameter to execute on a specific device.

Now we need to add the parameters to the java file:

We send the MCM parameters to the @BeforeClass because that is where we need to create our driver. Then we pass the test data to the @TestMethod.

We use the @Parameters notation before the method notation and then add the relevant parameter parenthesis and curly brackets. Each parameter is inside double quotes, parameters are separated by a comma.

Copy
@Parameters({“param1, “param2”, Param3”})

Integrating with Cucumber

In my previous blogs, I have shown as how to create a Cucumber project and write down the test scenarios in a feature file. You can use JUnit to convert those scenarios into Java code methods.

Using the TestNG class , add the above methods to your TestNG framework, as shown below.

In the XML file that we created, we make the following changes:

  • Add 2 more tests, each with a different device as a parameter.

  • Because the mobilecloud URl, username, and password are the same for all devices, move them to the class level.

  • At the suite level, change the parallel param from “false: to “tests”.

Then, we run the XML file as TestNG suite. When the test run is complete, we can see the result in the TestNG prespective and download the report from the reportium app by logging into the Perfecto cloud and accessing the report.