TestNG enables you to execute test methods, test classes, and test cases in parallel, enhancing the efficiency of your testing process. It allows us to run multiple tests at the same time across multiple environments instead of running tests one by one or in sequential order. Additionally, TestNG supports multiple test suites, which means you can group various tests into different suites and execute them simultaneously. This feature is particularly beneficial for large projects, as it helps reduce overall execution time and improves resource utilization.
With TestNG, you can easily manage and organize your testing efforts, ensuring that all aspects of your application are thoroughly tested on time. Hence, it is called parallel test execution in Selenium. Parallel testing helps us to run classes, test methods, and tests in parallel. We can reduce the execution time as tests will get executed simultaneously by using parallel test execution. Furthermore, TestNG allows you to configure the degree of parallelism through its XML configuration file, where you can specify whether to run tests at the method, class, or suite level.
This flexibility ensures optimal resource utilization and faster feedback on test results. By leveraging these capabilities, teams can significantly enhance their testing efficiency and ensure robust application performance across various scenarios. Additionally, implementing best practices such as ensuring thread safety and managing resource allocation will further improve the reliability and speed of your testing process.
Step 1: Creating a TestNG.xml file for executing test
Follow the below steps to handle above scenario
- Create a new project in eclipse
- Create two packages name com.sampletestpackage and com.sampletestpackage
- Create a class in each package (name them as Flipkart.java and SnapDeal.java) and copy the below code in respective classes
- Create a new file in your project and name it as testing.xml.
 Amazon.java
package com.sampletestpackage;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class Flipkart {
WebDriver driver = new ChromeDriver();
String username = ""; // Change to your username and passwrod
String password = "";
// This method is to navigate flipkart URL
@BeforeClass
public void init() {
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.navigate().to("https://www.flipkart.com");
}
// To log in flipkart
@Test
public void login() {
driver.findElement(By.partialLinkText("Login")).click();
driver.findElement(
By.cssSelector("input[placeholder='Enter email/mobile']"))
.sendKeys(username);
driver.findElement(
By.cssSelector("input[placeholder='Enter password']"))
.sendKeys(password);
driver.findElement(By.cssSelector("input[value='Login'][class='submit-btn login-btn btn']")).click();
}
// Search For product
@Test
public void searchAndSelectProduct() {
//driver.findElement(By.id("fk-top-search-box")).sendKeys("moto g3");
driver.findElement(By.name("q")).sendKeys("moto g3");
driver.findElement(
By.cssSelector("search-bar-submit.fk-font-13.fk-font-bold"))
.click();
// select the first item in the search results
String css = ".gd-row.browse-grid-row:nth-of-type(1) > div:nth-child(1)>div>div:nth-child(2)>div>a";
driver.findElement(By.cssSelector(css)).click();
}
@Test
public void addAndRemoveFromCart() {
driver.findElement(
By.cssSelector(".btn-express-checkout.btn-big.current"))
.click();
driver.findElement(By.cssSelector(".remove.fk-inline-block")).click();
Alert a = driver.switchTo().alert();
a.accept();
}
@Test
public void logout() {
Actions act = new Actions(driver);
WebElement user = driver.findElement(By.partialLinkText(username));
act.moveToElement(user).build().perform();
driver.findElement(By.linkText("Logout")).click();
}
@AfterClass
public void quit() {
driver.close();
}
}
SnapDeal.Java
package com.sampletestpackage;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class SnapDeal {
WebDriver driver = new ChromeDriver();
String username = ""; // Change to your username and passwrod
String password = "";
String pinCode = "";
// This method is to navigate snapdeal URL
@BeforeClass
public void init() {
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.navigate().to("https://www.snapdeal.com");
}
// To log in flipkart
@Test
public void login() {
driver.findElement(By.xpath("//button[text()='Login']")).click();
driver.switchTo().frame("loginIframe");
driver.findElement(By.cssSelector("div[onClick='getLoginForm()']"))
.click();
driver.findElement(By.id("userName")).sendKeys(username);
driver.findElement(By.id("j_password_login_uc")).sendKeys(password);
driver.findElement(By.id("submitLoginUC")).click();
driver.switchTo().defaultContent();
}
// Search For product
@Test
public void searchAndSelectProduct() {
driver.findElement(By.id("inputValEnter")).sendKeys("iphone 6s");
driver.findElement(By.cssSelector(".sd-icon.sd-icon-search")).click();
// select the first item in the search results
String css = ".product_grid_row:nth-of-type(1)>div:nth-child(1)";
driver.findElement(By.cssSelector(css)).click();
}
@Test
public void addAndRemoveFromCart() {
driver.findElement(By.xpath("//li[contains(text(),'Silver')]")).click();
driver.findElement(By.id("pincode-check")).sendKeys(pinCode);
driver.findElement(By.id("buy-button-id")).click();
driver.findElement(By.cssSelector("i[title='Delete Item']")).click();
Alert a = driver.switchTo().alert();
a.accept();
}
@Test
public void logout() {
driver.findElement(By.linkText("START SHOPPING NOW")).click();
Actions act = new Actions(driver);
WebElement user = driver.findElement(By.cssSelector(".sd-icon.sd-icon-user"));
act.moveToElement(user).build().perform();
driver.findElement(By.linkText("Logout")).click();
}
@AfterClass
public void quit() {
driver.close();
}
}
TestNg.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
Â
<suite thread-count="1" verbose="1" name="Gmail Suite" annotations="JDK" parallel="tests">
        Â
  <test name="Flipkart">
<classes>
 <class name="com.sampletestpackage.Flipkart"/>
</classes>
   </test>
  Â
  <test name="SnapDeal">
     <classes>
       <class name="com.sampletestpackage.SnapDeal"/>
     </classes>
   </test>
</suite>
2 Responses
If I want to run single test class 200 times. How do I configure ??