Parallel Testing with TestNG

I am going to show you how we make config for parallel testing. Let’s start with creating a testng.xml file.

Right-click to project’s name and select a “Create TestNG XML”.

Now you should see the testng.xml file under your project. Now add a method for parallel and thread numbers.

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite" thread-count="3" parallel="methods">
<test verbose="2" preserve-order="true" name="ParallelTesting">
<classes>
<class name="ParallelTesting"></class>
</classes>
</test>
</suite>

I gave 3 to thread-count because I have 3 @ Test. How many parallel tests you run, you should give that number to the thread count. The class name is the class name that we want to run in our java class.I wrote this code and this code opens 3 different Chrome browser and opens Google, Facebook, Instagram pages at the same time:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.*;

public class ParallelTesting {

public final String DRIVER_PATH = "Drivers/chromedriver";
public final String DRIVER_TYPE = "webdriver.chrome.driver";
public final String BASE_URL_GOOGLE = "https://www.google.com/";
public final String BASE_URL_FACEBOOK = "https://www.facebook.com/";
public final String BASE_URL_INSTAGRAM = "https://www.instagram.com/";
public WebDriver driver;


@Test
public void OpenChrome1() throws Exception {
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications","--ignore-certificate-errors","--disable-extensions");
System.setProperty(DRIVER_TYPE, DRIVER_PATH);
driver = new ChromeDriver(options);
driver.get(BASE_URL_GOOGLE);
driver.quit();
}

@Test
public void OpenChrome2() throws Exception {
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications","--ignore-certificate-errors","--disable-extensions");
System.setProperty(DRIVER_TYPE, DRIVER_PATH);
driver = new ChromeDriver(options);
driver.get(BASE_URL_FACEBOOK);
driver.quit();
}

@Test
public void OpenChrome3() throws Exception {
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications","--ignore-certificate-errors","--disable-extensions");
System.setProperty(DRIVER_TYPE, DRIVER_PATH);
driver = new ChromeDriver(options);
driver.get(BASE_URL_INSTAGRAM);
driver.quit();
}

}

You should run the testng.xml file to run test cases. I ran these test cases in almost 29 seconds and I also ran with headless mode, that time these cases took 15 seconds. You can use headless mode in your parallel method, you can save time.

Quality Assurance Engineer-Automation