TestNG - Execute multiple classes in testng examples

TestNG - Execute multiple classes in TestNG examples,in testng.xml file you can execute or run multiple classes under one test or Test Suite with the help of classes tags ,TestNG is a test framework to run all your multiple classes in a sequential manner,Let's see how to execute or run multiple classes in TestNG with examples as below.


TestNG - Execute multiple classes in TestNG examples


In this examples i am taking three java classes which will contains Selenium WebDriver driver initialization ,Normal Mail site opening and Sample registration class.Let's write the code step by step and execute the same.

Must Read:

Session ID is null using webdriver after calling quit
How to send extent reports in email with screenshots
Write Test Cases PASS or FAIL using Selenium
TESTNG Cannot find classpath error
Read data from properties file in selenium
Run Selenium scripts using Command Line
Run Selenium Scripts using Runnable JAR
TOP Selenium Interview Questions asked in MNC

Selenium XPATh tutorial step by step



Selenium WebDriver class



package com.mail.Mails;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;


public class SeleniumWebdrivers {

static WebDriver driver;
static String baseUrl="http://www.ebay.in";

@BeforeSuite
public void open_browser(){

//Open Firefox Browser with the help of Firefox Driver
driver = new FirefoxDriver();
driver.manage().window().maximize();
//Open baseurl with the help of get method
driver.get(baseUrl);

try {
Thread.sleep(2500);

} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}

}

@AfterSuite
public void close_browser(){
driver.quit();
}

}

Search Product class

In this class we are using extends keywords to class driver objects into other classes,please read more details about How to pass driver instance to other classes.
package com.mail.Mails;

import org.openqa.selenium.By;

import org.testng.annotations.Test;

public class Searchproduct extends SeleniumWebdrivers {

@Test

public void Search_product(){
//Enter Keywords in Search box
driver.findElement(By.id("gh-ac")).sendKeys("Mobiles");
//Click on Go button
driver.findElement(By.id("gh-btn")).click();
try {
Thread.sleep(2500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();}

}

}


Products class



package com.mail.Mails;

import org.openqa.selenium.By;
import org.testng.annotations.Test;

public class ProductDescp extends SeleniumWebdrivers {

@Test

public void product_Descriptoin(){

//Count number of products displayed on displayed page as per

//Search Criteria

String listingcount=driver.findElement(By.xpath("/html/body/div[5]/div[2]/div
[1]/div[1]/div/div[1]/div/div[2]/div/div/span[1]")).getText();
//Print number of listing counts

String count = listingcount.replaceAll("\\D+","");

System.out.println("Listed number of mobiles are : "+count);

//Using Parseint methods to change string into Integer

int counts = Integer.parseInt(count);

//Validating listing products count with if condition

if(counts > 0){

System.out.println("Number of listing are greater than zero :"+ "counts "+" Test case is PASS");

}else{

System.out.println("Number of listing products are lessthan zero : Test Case is FAIL");

}

}

}



TestNG.XML file



<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="Suite1" verbose="1" >

<test name="Eproducts">

<classes>

<class name="com.mail.Mails.Searchproduct"/>

<class name="com.mail.Mails.ProductDescp"/>

</classes>

</test>

</suite>



testng xml


Test Results


[TestNG] Running:
E:\QTP Tutorials\Selenium Automation\MailsAutomation\testng.xml

Listed number of mobiles are : 1851
Number of listing are greater than zero :counts Test case is PASS

===============================================
Suite1
Total tests run: 2, Failures: 0, Skips: 0
===============================================


Execute Multiple Classes in TestNG Examples



What have you learned?

1.What is TestNG XML file
2.How to create TestNG XML file with number of classes.
3.How to create driver objects and use in other classes.

Please share my posts and provide your valuable comments ,i hope you like my post.Thank you for reading.

Post a comment

0 Comments