How to Run Multiple Test Suites in Selenium-TestNG

How to Run Multiple Test Suites in Selenium-TestNG

Table of Contents

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

  1. Create a new project in eclipse
  2. Create two packages name com.sampletestpackage and com.sampletestpackage
  3. Create a class in each package (name them as Flipkart.java and SnapDeal.java) and copy the below code in respective classes
  4. 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>

selenium certificaton

2 Responses

Leave a Reply

Your email address will not be published. Required fields are marked *

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

Share this article
Subscribe
By pressing the Subscribe button, you confirm that you have read our Privacy Policy.
Need a Free Demo Class?
Join H2K Infosys IT Online Training
Enroll Free demo class