How to run multiple test cases using TestNG Test Suite in Selenium

0 votes
May 21, 2019 in Selenium by Neerja
30,817 views

1 answer to this question.

+1 vote

Hi Neerja, to run multiple test cases using TestNG test suite in selenium, perform these steps one by one:

  1. Right click on Project folder, go to New and select ‘File‘.

  2. In New file wizard, add file name as ‘testng.xml‘ and click on Finish button.

  3. It will add testng.xml file under your project folder. 

  4. Now add below given code in your testng.xml file. After giving appropriate names and adding your test cases in <classes> tag, your testng.xml file will looks like this:

    • <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
      
      <suite name="Test Suite-1" >
      
      <test name="Test Case-1" >
      
      <classes>
      
      <class name="co.edureka.selenium.demo1.Demo1" />
      
      <class name="co.edureka.selenium.demo1.Demo2" />
      
      <class name="co.edureka.selenium.demo1.ImgExistTest" />
      
      <class name="co.edureka.selenium.demo1.ForgotPassword" />
      
      </classes> 
      
      </test>
      
      </suite>
  5. Now run the xml file by right click on the testng.xml file and select Run As > TestNG Suite.

For further understanding, you can refer to the Selenium Training.

answered May 22, 2019 by Abha
• 28,140 points
This works better when you have limit cases like 50 to 100 or so.

Say if u have 100s to thousand cases, it is hard to maintain.

Other way is using <package>, but i have challenges calling from Maven or enabling parallelism when using packages.

Do you have any other better option?

hey, @Uday,

You can look into this: 

Step 1: Creating a TestNG.xml file for executing test

Follow the below steps to handle the 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>

Related Questions In Selenium

0 votes
2 answers

Can anyone help me that how to run Selenium WebDriver test cases in Chrome?

You first need to download chrome driver ...READ MORE

answered Aug 26, 2019 in Selenium by Abha
• 28,140 points
2,012 views
0 votes
1 answer

How to run Selenium test cases in Headless Chrome browser?

@Afreen to run selenium test cases in ...READ MORE

answered May 17, 2019 in Selenium by Kushal
4,171 views
0 votes
1 answer

How to run test scripts in Selenium using PhantomJS?

Hey @Shushil, you can run your test ...READ MORE

answered May 17, 2019 in Selenium by Abha
• 28,140 points
1,762 views
0 votes
1 answer

How to run test cases in headless mode using Phantom JS?

Hi Bhanu, you can run test cases ...READ MORE

answered Aug 14, 2019 in Selenium by Abha
• 28,140 points
1,605 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
13,772 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,187 views
0 votes
2 answers

What is the role of TestNG & JUnit frameworks in Selenium?

TestNG and JUnit are test frameworks . it ...READ MORE

answered Sep 4, 2020 in Selenium by Sri
• 3,190 points
2,938 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
7,263 views
0 votes
1 answer

How to run a group of test cases using TestNG in Selenium Webdriver?

Hey Kajal, TestNG allows you to perform ordered ...READ MORE

answered Jun 12, 2019 in Selenium by Abha
• 28,140 points
2,941 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP