STQA Data Provider and Annotations Assignment 6
STQA Data Provider and Annotations Assignment 6
frameworks
Aim:
- To understand and implement Data Provider, Data Driven Framework, and
Annotations in software testing.
Objectives:
- To learn how to design and set up a Data Driven Framework for software
testing.
- Implement Annotations using TestNG
Theory:
1. What is a Data Provider?
- A Data Provider is a mechanism that supplies test data to test cases during the
execution of software tests.
- It enables the parameterization of tests, allowing for the execution of the same
test scenario with multiple sets of data.
Tasks:
Annotations.java
package testNG;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;
@BeforeMethod
public void beforeMethod() {
System.out.println("@BeforeMethod");
}
@AfterMethod
public void afterMethod() {
System.out.println("@AfterMethod");
}
@BeforeClass
public void beforeClass() {
System.out.println("@BeforeClass");
}
@AfterClass
public void afterClass() {
System.out.println("@AfterClass");
}
@BeforeTest
public void beforeTest() {
System.out.println("@BeforeTest");
}
@AfterTest
public void afterTest() {
System.out.println("@AfterTest");
@BeforeSuite
public void beforeSuite() {
System.out.println("@BeforeSuite");
}
@AfterSuite
public void afterSuite() {
System.out.println("@AfterSuite");
}
}
Output:
DataProvider [Test NG]
LaunchBrowsers.java
package stqa_java_project;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
System.setProperty("webdriver.chrome.driver",chromeDriverPath);
WebDriver chrome = new ChromeDriver();
return chrome;
}
System.setProperty("webdriver.gecko.driver",firefoxDriverPath);
WebDriver firefox = new FirefoxDriver();
return firefox;
}
Tests.java
package testNG;
import org.testng.annotations.Test;
import stqa_package.LaunchBrowsers;
import org.openqa.selenium.WebDriver;
import java.util.concurrent.TimeUnit;
import org.testng.Assert;
import org.openqa.selenium.By;
import org.testng.annotations.DataProvider;
browser.manage().timeouts().implicitlyWait(10,
TimeUnit.SECONDS);
browser.findElement(By.name("username")).sendKeys(userName);
browser.findElement(By.name("password")).sendKeys(password);
browser.findElement(By.tagName("button")).click();
try {
String dashboardXpath =
"//*[@id=\"app\"]/div[1]/div[1]/header/div[1]/div[1]/span/h6";
String dashboardText =
browser.findElement(By.xpath(dashboardXpath)).getText();
boolean isDashboard =
(dashboardText.equalsIgnoreCase("dashboard")) ? true : false ;
if(isDashboard)
System.out.println("Pass");
else
throw new Exception();
} catch (Exception e) {
Assert.fail("Failed Login");
System.out.println("Fail");
}
}
@DataProvider
public Object[][] dp() {
return new Object[][] { new Object[] { "admin", "admin123" },
new Object[] { "add", "b" }, };
}
}
Output:
Data Driven Framework [Test NG]
DataDriven.java
package stqa_package;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
browser.findElement(By.name("userName")).sendKeys(usernameCell.toS
tring());
browser.findElement(By.name("password")).sendKeys(passwordCell.toS
tring());
browser.findElement(By.name("submit")).click();
try {
String statusButtonXpath =
"/html/body/div[2]/table/tbody/tr/td[2]/table/tbody/tr[2]/td/table/tbody
/tr/td[1]";
String assertion =
browser.findElement(By.xpath(statusButtonXpath)).getText();
boolean buttonIsNotSignOff =
assertion.equalsIgnoreCase("sign-off") ? false : true;
if (buttonIsNotSignOff)
throw new Exception("Fail");
resultCell.setCellValue("Pass");
browser.findElement(By.xpath(statusButtonXpath)).click();
System.out.println("Pass");
} catch (Exception exception) {
resultCell.setCellValue(exception.getMessage());
System.out.println(exception.getMessage());
}
fileInputStream.close();
FileOutputStream fileOutputStream = new
FileOutputStream(excelFile);
workbook.write(fileOutputStream);
}
browser.quit();
}
}
Output: