Folder Structure
new –> project –> JAVA project
new –> source folder
src/main/java
package: applicationutility
package: baselibrary
file: BaseLibrary.java
package: excelutility
interface: ExcelUtility.java
package: propertyutility
interface: PropertyUtility.java
package: screenshotutility
package: testingbaba_pages
file: Login_Page.java
file: TextBox_Page.java
package: waitutility
src/main/test
package: Testingbaba_test
file: Login_Test.java
file: TextBox_Test.java
folder: driver
chromedriver.exe
folder: testdata
file: config.properties
BaseLibrary.java
package baselibrary;
import java.io.FileInputStream;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import excelutility.ExcelUtility;
public class BaseLibrary implements ExcelUtility, PropertyUtility {
String configpath = "path of config.properties file";
static public WebDriver driver;
public void getLaunchUrl(String url, String browsername) {
if(browsername.equals("chrome")){
System.setProperty("webdriver.chrome.driver", "C:\\Users\\injil\\OneDrive\\Desktop\\New folder (2)\\Testng_Frame\\drivers\\chromedriver.exe");
driver = new ChromeDriver();
driver.get(url);
driver.manage().window().maximize();
}
else if(browsername.equals("firefox")){
System.setProperty("webdriver.gecko.driver", "C:\\Users\\injil\\OneDrive\\Desktop\\New folder (2)\\Testng_Frame\\drivers\\geckodriver.exe");
driver = new ChromeDriver();
driver.get(url);
driver.manage().window().maximize();
}
}
@AfterTest
public void tearDown() {
driver.quit();
}
@Override
public String getReaddata(String path, int sheetno, int colno, int rowno) {
String value = "";
try {
FileInputStream fis = new FileInputStream(path);
XSSFWorkbook wb = new XSSFWorkbook(fis);
XSSFSheet sh = wb.getSheetAt(sheetno);
value = sh.getRow(rowno).getCell(colno).getStringCellValue();
}catch(Exception e) {
}
return value;
}
@override
public String getReaddata(String key) {
String value = "";
try {
FileInputStream fis = new FileInputStream(configpath);
Properties prop = new Properties();
prop.load(fis);
value = prop.getProperty(key);
}catch(Exception e){
System.out.println("issue in filereading" + e);
}
return value;}
}
ExcelUtility.java
package excelutility;
public interface ExcelUtility {
public String getReaddata(String path, int sheetno, int colno, int rowno);
}
Login_Page.java
package testingbaba_pages;
import baselibrary.BaseLibrary;
public class Login_Page extends BaseLibrary{
public void getTitle(){
String title = driver.getTitle();
System.out.println(title);
}
}
Login_Test.java
package testingbaba_test;
import baselibrary.BaseLibrary;
public class Login_Test extends BaseLibrary{
Login_Page ob;
@Parameters({"env", "browsername"})
@BeforeTest
public void getLaunchUrl_testingbaba(String env, String browsername){
getLaunchUrl(getReaddata(env), browsername); // Reading url from property file
ob = new Login_Page();
}
@Test
public void ToverifyTitle(){
ob.getTitle();
}
}
TextBox_Page.java
package testingbaba_pages;
import java.util.List;
import org.junit.Assert;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import baselibrary.BaseLibrary;
public class Text_Box_Page extends BaseLibrary {
String path = "C:\\Users\\injil\\OneDrive\\Desktop\\New folder (2)\\Testng_Frame\\testdata\\testdata.xlsx";
public Text_Box_Page() {
PageFactory.initElements(driver, this);
}
@FindBy(xpath="//*[@id=\"myModal2\"]/div/div/div[1]/button")
private WebElement closebtn;
@FindBy(xpath="//*[@id=\"navbarSupportedContent\"]/ul/li[5]/a")
private WebElement practice;
@FindBy(xpath="//*[@id=\"elements-accordion\"]/div[1]/div[1]/h2/button")
private WebElement elements;
@FindBy(xpath="//*[text()=\"text box\"]")
private WebElement textbox;
@FindBy(xpath="//*[@id=\"fullname1\"]")
private WebElement name;
@FindBy(xpath="//*[@id=\"fullemail1\"]")
private WebElement email;
@FindBy(xpath="//*[@id=\"fulladdresh1\"]")
private WebElement c_add;
@FindBy(xpath="//*[@id=\"paddresh1\"]")
private WebElement p_add;
@FindBy(xpath="//*[@id=\"tab_1\"]/div/div[1]/form/input[3]")
private WebElement submit;
@FindBy(xpath="//*[@class=\"col-md-6 mt-5\"]/label")
private List<WebElement> listofdata;
public void clickonClosebtn() {
closebtn.click();
}
public void clickonPractice() {
practice.click();
}
public void clickonElements() {
elements.click();
}
public void clickonTextbox() {
textbox.click();
}
public void filldetails() {
for(int i=1;i<=3;i++) {
name.clear();
name.sendKeys(getReaddata(path, 0, 0, i));
email.clear();
email.sendKeys(getReaddata(path, 0, 1, i));
c_add.clear();
c_add.sendKeys(getReaddata(path, 0, 2, i));
p_add.clear();
p_add.sendKeys(getReaddata(path, 0, 3, i));
submit.click();
}
public void getVerify() {
SoftAssert sf = new SoftAssert();
ArrayList<String>expected_ui = new ArrayList<String>();
ArrayList<String>actual_xls = new ArrayList<String>();
actual_xls.add(getReaddata(excelpath, 0, 0, 1))
actual_xls.add(getReaddata(excelpath, 0, 1, 1))
actual_xls.add(getReaddata(excelpath, 0, 2, 1))
actual_xls.add(getReaddata(excelpath, 0, 3, 1))
for(int i=1;i<listofdata.size()-1;i=i+2){
expected_ui.add(listofdata.get(i).getText());
}
for(int i=0;i<=expected_ui.size()-1;i++){
// Assert.assertEquals(actual_xls.get(i), expected_ui.get(i)); // hard assert
sf.assertEquals(actual_xls.get(i), expected_ui.get(i)); // soft assert
}
sf.assertAll();
}
}
TextBox_Test.java
package testingbaba_test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import baselibrary.BaseLibrary;
import testingbaba_pages.Text_Box_Page;
public class Text_Box_Test extends BaseLibrary {
Text_Box_Page ob;
@Parameters({"env", "browsername"})
@BeforeTest
public void getLaunchUrl_TestingBaba(String env, String browsername) {
getLaunchUrl(getReaddata(env), browsername); // Reading from properties file
ob = new Text_Box_Page();
}
@Test(priority = 0)
public void clickonClosebtn() {
ob.clickonClosebtn();
}
@Test(priority = 1)
public void clickonPractice() {
ob.clickonPractice();
}
@Test(priority = 2)
public void clickonElements() {
ob.clickonElements();
}
@Test(priority = 3)
public void textbox() {
ob.clickonTextbox();
}
@Test(priority = 4)
public void filldetails() {
ob.filldetails();
}
@Test(priority = 5)
public void getVerify() {
ob.getVerify();
}
}
You can create a suite file (testng.xml) to run all the test classes at once by right click on project folder –> TestNG –> Convert to TestNG
TestNG.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="AliSuite">
<parameter name="env" value="preprod"></parameter>
<parameter name="browsername" value="chrome"></parameter> //cross browser testing
<test name="LoginTest" enabled="False">
<classes>
<class name="testingbaba_test.Login_Test"/>
</classes>
</test>
<test name="TextBoxTest">
<classes>
<class name="testingbaba_test.TextBox_Test"/>
</classes>
</test>
<!-- Test -->
</suite> <!-- Suite -->
You can use enabled = “False” to stop running a particular Test.
Creating Properties File
To store things like URL, locators in the form of key value pairs.
config.properties file
staging=https://www.testingbaba.com/old
preprod=https://www.testingbaba.com/
mobile = you are selected mobile
PropertUtility.java file
package propertyutility;
public interface PropertyUtility{
public string getReaddata(String key);
}
Assertion
Assertion: assertion we have provide by Test-Ng. we are using assertion to verify the actual and expected results. If expected and actual will not match then test cases will be fail and also same reason will mention in test report.
We have two types of assertions-
Soft assert(verify)-
Hard assert(Assert)-
- Hard assert: if actual and expected will not match then test case will be immediate fail.
- Soft assert: if actual and expected will not match then test case will not be fail till assert all () statement is not executed.