Selenium-Testng examples to execute multiple classes

Selenium – TESTNG examples to execute multiple Classes,Most of the time we will face problem while executing the multiple test cases from multiple classes using TESTNG,In order to overcome those problem i am explaining with real time examples using TestNG with selenium webdriver.

Here we are creating three classes as Selewebinitiate , Login and Logout classes.Let’s create the classes and create Testng.xml file as below and also read Execution of multiple Classes with TestNG


Generate HTML Reports using Selenium
Selenium WebDriver Methods
Read Data From properties file using Selenium
Launch Firefox Browser using GeckoDriver

Selewebinitiate java Class:


package com.Bazaar.Purchase; 
import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.testng.annotations.BeforeTest;
 
public Class Selewebinitiate(){ 
//Declare the driver variables
Webdriver driver;
String baseUrl; 

//Now Create TestNG annotation to run before Test
@BeforeTest
public void setup(){
 
//I am using chrome driver to run the script in google chrome browser
extent = new ExtentReports("E:\\Automation_Testing\\FutureBazaar\\FutureBazaar_Results\\FB_TestResults.html",true);
test=extent.startTest("Selenium Web driver initialization");
//Chrome browser initialization
File ChromeDriver = new File("F:\\Java_Applications\\Zip Files\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver",ChromeDriver.getAbsolutePath());
driver = new ChromeDriver(options);
baseUrl="http://www.xxxxx.com/rpp/baby";
 }
}

2.Login java class:

Now we will create Login class with TestNG annotation as below and we will use extends option to pass Selewebinitiate objects in this class.Let’s create the class

package com.Bazaar.Purchase;
import org.testng.annotations.Test;

public class StoreLogin extends Selewebinitiate {

@Test
public void custLogin(){

driver.findElement(By.id("userEmailId")).clear();
driver.findElement(By.id("userEmailId")).sendKeys("rajeshkuchana");
String username=driver.findElement(By.id("userEmailId")).getAttribute("value");
if(username.contains("4795")){
test.log(LogStatus.PASS,"Username is entered");
}else{
test.log(LogStatus.FAIL, "Username is not entered");
}
driver.findElement(By.id("passwordId")).clear();
driver.findElement(By.id("passwordId")).sendKeys("xxxxxxx");
String password=driver.findElement(By.id("passwordId")).getAttribute("value");
if(password.contains("4739")){
System.out.println("user enter Password.Test Case is Pass");
}else{
test.log("user not enter password,Test Case is Fail");
}
driver.findElement(By.id("frm_0")).click();

}
}


3.Logout class

In this class we will logout the application,let’s create the class and passing Selewebinitiate class

package com.Bazaar.Purchase;
import org.testng.annotations.Test;

public class Logout extends Selewebinitiate {

@Test
public void appLogout(){

driver.findElement(By.id("Logoutid")).click();
driver.quit();
driver.close();
}
}


4.TestNG.XML:

Now create TestNG.xml file as below here we are using
1.name : Name in Testng xml file indicates Name of the Test as below i.e <test name=”Bazaar App”>
2.verbose=”2″ :This indicates write the execution log details to console of Eclipse where we are able to see Test Execution results.This verbose can indicates from 1 to 9 numbers.According to numbers it displays different log details in console.
3.parallel=”tests” : It is a mode in parallel and having different types of modes those are Tests,methods,classes and none.

Testng.xml file as below


 <suite name="Test Suite" parallel="tests" verbose="2">  
 <test name="Bazaar App">  
 <classes>  
 <class name="com.Bazaar.Purchase.StoreLogin">  
 <class name="com.Bazaar.Purchase.Logout">  
 </class></class></classes>  
 </test>  
 </suite>  

Now run the testng.xml file by right click on it TestNG–TestNG Suite.It will automatically execute the tests within single browser.Thank you for reading ,please provide your valuable comments on this posts.Please share my post to help other people.




Test Results:


Test NG Test Results



Please provide your valuable comments on this posts and please share with social media sites.

Post a comment

0 Comments