0% found this document useful (0 votes)
28 views

STQA Data Provider and Annotations Assignment 6

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

STQA Data Provider and Annotations Assignment 6

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

TestNG and data automation

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.

- It increases test coverage, identifies data-related issues, and supports the


reusability of test scripts.

2. What is a Data Driven Framework?


- A Data Driven Framework is an architecture that separates test script logic from
test data, allowing for the dynamic execution of test scenarios with different
input values.

- It contains the logic of the test without hard coding data.

- It stores test data separately.

- It orchestrates the execution by combining test scripts with data.


3. What are annotations in software testing?
- Annotations are metadata that provide additional information to the compiler or
interpreter without affecting the program's logic.

- Annotations are used to convey information about test methods, setup,


teardown, and configuration.

Tasks:

Annotations [Test NG]

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;

public class Annotations {


@Test
public void f() {
System.out.println("@Test");
}

@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;

public class LaunchBrowsers {

public WebDriver initializeChrome(String chromeDriverPath) {

System.setProperty("webdriver.chrome.driver",chromeDriverPath);
WebDriver chrome = new ChromeDriver();
return chrome;
}

public WebDriver initializeFirefox(String firefoxDriverPath) {

System.setProperty("webdriver.gecko.driver",firefoxDriverPath);
WebDriver firefox = new FirefoxDriver();
return firefox;
}

public WebDriver initializeEdge(String edgeDriverPath) {


System.setProperty("webdriver.ie.driver",edgeDriverPath);
WebDriver edge = new InternetExplorerDriver();
return edge;
}
}

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;

public class Tests {


@SuppressWarnings("deprecation")
@Test(dataProvider = "dp")
public void f(String userName, String password) {
String url1 = "https://opensource-demo.orangehrmlive.com/";
WebDriver browser =
LaunchBrowsers.initializeBrowser("chrome");
browser.get(url1);

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;

public class DataDriven {


static String excelFile = "credentials.xlsx";
static String sheetName = "credentials";

public static void main(String[] args) throws IOException {


WebDriver browser =
LaunchBrowsers.initializeBrowser("firefox");

String url = "https://demo.guru99.com/test/newtours/";


browser.get(url);

FileInputStream fileInputStream = new


FileInputStream(excelFile);
@SuppressWarnings("resource")
XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);
XSSFSheet sheet = workbook.getSheet(sheetName);
int lastRow = sheet.getLastRowNum();
for (int i = 1; i <= lastRow; i++) {
XSSFRow row = sheet.getRow(i);
XSSFCell usernameCell = row.getCell(0);
XSSFCell passwordCell = row.getCell(1);
XSSFCell resultCell = row.createCell(2);

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:

You might also like