Banking Guru 99

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 13
At a glance
Powered by AI
The document discusses Java code for reading configuration properties and generating test reports. It also includes XML configuration for TestNG.

The ReadConfig class is used to read configuration properties from a file and return values for things like the base URL, username, and password when running tests.

The Reporting class is used to generate extent reports by attaching a reporter and logging test results with screenshots when tests pass or fail.

package com.inetbankin.

utilities;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

public class ReadConfig {

Properties pro;
public ReadConfig()
{
File src=new File("./Configuration/config.properties") ;
try {
FileInputStream fis= new FileInputStream(src);
pro=new Properties();
pro.load(fis);
}
catch(Exception e)
{
System.out.println("Exception is "+ e.getMessage());
}}
public String getAppl()
{
String url=pro.getProperty("baseURL");
return url;
}
public String getUser()
{
String user=pro.getProperty("username");
return user;
}
public String getpass()
{
String pass=pro.getProperty("password");
return pass;
}
public String getChromeUrl()
{
String Chrome=pro.getProperty("chromepath");
return Chrome;
}
public String getFirefoxUrl()
{
String Chrome=pro.getProperty("firefoxpath");
return Chrome;
}
}
___________________________________________________________________________________
___________________________________________________________________________________
package com.inetbankin.utilities;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.markuputils.ExtentColor;
import com.aventstack.extentreports.markuputils.MarkupHelper;
import com.aventstack.extentreports.reporter.ExtentSparkReporter;
import com.aventstack.extentreports.reporter.configuration.Theme;

public class Reporting extends TestListenerAdapter {


public static ExtentReports extent;
public static ExtentSparkReporter spark;
public static ExtentTest log1;

// @BeforeTest
public void onStart(ITestContext testContext) {

// extent = new ExtentReports();


// final File CONF = new File("extent-config.xml");

String timeStamp = new


SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
String repName = "Test-Report-" + timeStamp + ".html";
spark = new ExtentSparkReporter(
"C:\\Users\\DemuduDonka\\eclipse-workspace\\intBankingV1\\
test-output\\" + repName);

try {
spark.loadXMLConfig("C:\\Users\\DemuduDonka\\eclipse-workspace\\
intBankingV1\\extent-config.xml");
} catch (IOException e) {
e.printStackTrace();
}
/*
* try { spark.loadXMLConfig(CONF); } catch (IOException e) {
*
* e.printStackTrace(); }
*/
extent = new ExtentReports();
extent.attachReporter(spark);
extent.setSystemInfo("Host name", "localhost");
extent.setSystemInfo("Evironment", "QA");
extent.setSystemInfo("user", "demudu");

spark.config().setDocumentTitle("Banking Test Project");


spark.config().setReportName("Functional Testing Report");

spark.config().setTheme(Theme.DARK);
}

//@AfterMethod
public void onTestSuccess(ITestResult result) {

log1 = extent.createTest(result.getName());
log1.log(Status.PASS, MarkupHelper.createLabel(result.getName() + "Test
Case Passed", ExtentColor.GREEN));

log1.pass(result.getThrowable());
// // System.out.println(result.getName());
String screenshotPath = "C:\\Users\\DemuduDonka\\eclipse-workspace\\
intBankingV1\\Screenshots\\"
+ result.getName() + ".png";
File f = new File(screenshotPath);
if (f.exists()) {
log1.pass("Screenshot is below:" +
log1.addScreenCaptureFromPath(screenshotPath));
}

//@AfterMethod
public void onTestFailure(ITestResult result) {

log1 = extent.createTest(result.getName());
log1.log(Status.FAIL, MarkupHelper.createLabel(result.getName() + "Test
Case Failed", ExtentColor.RED));
String screenshotPath = "C:\\Users\\DemuduDonka\\eclipse-workspace\\
intBankingV1\\Screenshots\\"
+ result.getName() + ".png";
File f = new File(screenshotPath);
if (f.exists()) {
log1.fail("Screenshot is below:" +
log1.addScreenCaptureFromPath(screenshotPath));
}
}

//@AfterMethod
public void onTestSkipped(ITestResult result) {

log1 = extent.createTest(result.getName());
log1.log(Status.PASS, MarkupHelper.createLabel(result.getName() + "Test
Case Skipped", ExtentColor.BROWN));
}

//@AfterTest
public void onFinish(ITestContext testContext) {
extent.flush();
}

}
___________________________________________________________________________________
___________________________________________________________________________________
________________________________________________
package com.inetbankin.utilities;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.ss.usermodel.DataFormatter;
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;

public class XLUtils {

public static FileInputStream fi;


public static FileOutputStream fo;
public static XSSFWorkbook wb;
public static XSSFSheet ws;
public static XSSFRow row;
public static XSSFCell cell;

public static int getRowCount(String xlfile, String xlsheet) throws


IOException {

fi = new FileInputStream(xlfile);
wb = new XSSFWorkbook(fi);
ws = wb.getSheet(xlsheet);
int rowcount = ws.getLastRowNum();
wb.close();
fi.close();
return rowcount;
}

public static int getCellCount(String xlfile, String xlsheet, int rownum)


throws IOException {

fi = new FileInputStream(xlfile);
wb = new XSSFWorkbook(fi);
ws = wb.getSheet(xlsheet);
row = ws.getRow(rownum);
int cellcount = row.getLastCellNum();
wb.close();
fi.close();
return cellcount;
}

public static String getCellData(String xlfile, String xlsheet, int rownum,


int colnum) throws IOException {
fi = new FileInputStream(xlfile);
wb = new XSSFWorkbook(fi);
ws = wb.getSheet(xlsheet);
row = ws.getRow(rownum);
cell = row.getCell(colnum);
String data;
try {
DataFormatter formtter = new DataFormatter();
String cellData = formtter.formatCellValue(cell);
return cellData;
} catch (Exception e) {
data = "";
}
wb.close();
fi.close();
return data;
}

public static void setCellData(String xlfile, String xlsheet, int rownum, int
colnum, String data)
throws IOException {
fi = new FileInputStream(xlfile);
wb = new XSSFWorkbook(fi);
ws = wb.getSheet(xlsheet);
row = ws.getRow(rownum);
cell = row.getCell(colnum);
cell.setCellValue(data);
fo = new FileOutputStream(xlfile);
wb.write(fo);
wb.close();
fi.close();
fo.close();
}
}
___________________________________________________________________________________
___________________________________________________________________________________
________________________________________________________________
package com.inetbanking.pageObjects;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class LoginPage {


public static WebDriver driver;

public LoginPage(WebDriver driver) {


this.driver = driver;
PageFactory.initElements(driver, this);
}

@FindBy(name = "uid")
WebElement txtUsername;

@FindBy(name = "password")
WebElement txtPassword;

@FindBy(name = "btnLogin")
WebElement login;

@FindBy(xpath = "//a[text()='Log out']")


WebElement logout;

public void setusername(String uname) {


txtUsername.sendKeys(uname);
}

public void setpassword(String pwd) {


txtPassword.sendKeys(pwd);
}

public void clickSubmit() {


login.click();
}

public void logoutMethod() {


logout.click();

}
}
___________________________________________________________________________________
___________________________________________________________________________________
__________________________________________________________________
package com.inetbanking.Testcases;

import java.io.File;
import java.io.IOException;
import java.time.Duration;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
//import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
//import org.apache.log4j.Logger;
//import org.apache.log4j.PropertyConfigurator;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;

import com.inetbankin.utilities.ReadConfig;

import io.github.bonigarcia.wdm.WebDriverManager;

public class BaseClass {


ReadConfig read = new ReadConfig();
public String baseURL = read.getAppl();
public String username = read.getUser();
public String password = read.getpass();
public String chromepath = read.getChromeUrl();
public String firefoxpath = read.getFirefoxUrl();
public static WebDriver driver;

public static Logger logger;

@Parameters("browser")
@BeforeClass
public void setup(String br) throws InterruptedException {
logger = Logger.getLogger("initBankingV1");
PropertyConfigurator.configure("Log4j.properties");
if (br.equals("chrome")) {
System.setProperty("webdriver.chrome.driver", chromepath);
// WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().window().maximize();
} else if (br.equals("firefox")) {
System.setProperty("webdriver.gecko.driver", firefoxpath);
// WebDriverManager.chromedriver().setup();
driver = new FirefoxDriver();
driver.manage().window().maximize();
}
// driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(15));
driver.get(baseURL);
}

@AfterClass
public void teardown() {
driver.quit();

public void captureScreen(WebDriver driver, String tname) throws IOException


{
TakesScreenshot ts = (TakesScreenshot) driver;
File source = ts.getScreenshotAs(OutputType.FILE);
File target = new File(
"C:\\Users\\DemuduDonka\\eclipse-workspace\\intBankingV1\\
Screenshots\\" + tname + ".png");
FileUtils.copyFile(source, target);
System.out.println("Screenshot taken");
}

}
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________
package com.inetbanking.Testcases;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

import com.inetbanking.pageObjects.LoginPage;

public class Tc_Login_001 extends BaseClass {

@Test
public void LoginTest() throws InterruptedException, IOException {
logger.info("base url opened");
LoginPage lp = new LoginPage(driver);

lp.setusername(username);
logger.info("entered username");
lp.setpassword(password);
logger.info("entered password");
lp.clickSubmit();
Thread.sleep(5000);
driver.getTitle();
System.out.println(driver.getTitle());
if (driver.getTitle().equals("Guru99 Bank Manager HomePage")) {
captureScreen(driver, "LoginTest1");
Assert.assertTrue(true);
logger.info("login test passed");
} else {
// captureScreen(driver ,"LoginTest") ;
Assert.assertTrue(false);
logger.info("login test failed");
}
}
}
___________________________________________________________________________________
___________________________________________________________________________________
________________________________________________________________

package com.inetbanking.Testcases;

import java.io.IOException;

import org.openqa.selenium.NoAlertPresentException;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.inetbankin.utilities.XLUtils;
import com.inetbanking.pageObjects.LoginPage;

public class Tc_Login_002 extends BaseClass {


@Test(dataProvider = "LoginData")
public void loginDDT(String user, String pwd) {
LoginPage lo = new LoginPage(driver);
lo.setusername(user);
logger.info("username provided");
lo.setpassword(pwd);
logger.info("password provided");
lo.clickSubmit();

if (isAlertPresent() == true) {
driver.switchTo().alert().accept();
driver.switchTo().defaultContent();
Assert.assertTrue(false);
logger.warn("login failed");
} else {
Assert.assertTrue(true);
logger.info("login passed");
lo.logoutMethod();
driver.switchTo().alert().accept();
driver.switchTo().defaultContent();
}
}

public boolean isAlertPresent() {


try {
driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
return false;
}
}

@DataProvider(name = "LoginData")
String[][] getData() throws IOException

{
String path = "C:\\Users\\DemuduDonka\\eclipse-workspace\\
intBankingV1\\src\\main\\java\\com\\inetbanking\\Testdata\\loginData1.xlsx";
int rownum = XLUtils.getRowCount(path, "Sheet1");
int colcount = XLUtils.getCellCount(path, "Sheet1", 1);
String logindata[][] = new String[rownum][colcount];

for (int i = 1; i <= rownum; i++) {


for (int j = 0; j <colcount; j++) {
logindata[i - 1][j] = XLUtils.getCellData(path, "Sheet1",
i, j);// 1 0
}
}
return logindata;

}
___________________________________________________________________________________
___________________________________________________________________________________
______________________________________________________________
config.properties:

baseURL=https://demo.guru99.com/V4/
username=mngr425620
password=zEtamAs
chromepath=./Drivers/chromedriver.exe
firefoxpath=./Drivers/geckodriver.exe
___________________________________________________________________________________
___________________________________________________________________________________
__________________________________________________

extent-config.xml:

<?xml version="1.0" encoding="UTF-8"?>


<extentreports>
<configuration>

<!-- report theme -->


<!-- STANDARD, DARK -->
<theme>STANDARD</theme>

<!-- document encoding -->


<!-- defaults to UTF-8 -->
<encoding>UTF-8</encoding>

<!-- protocol for script and stylesheets -->


<!-- defaults to https -->
<!-- HTTP, HTTPS -->
<protocol>HTTPS</protocol>

<!-- offline report -->


<timelineEnabled>true</timelineEnabled>

<!-- offline report -->


<enableOfflineMode>false</enableOfflineMode>

<!-- title of the document -->


<documentTitle>Extent Framework</documentTitle>

<!-- report name - displayed at top-nav -->


<reportName>Build 1</reportName>
<!-- timestamp format -->
<timeStampFormat>MMM dd, yyyy HH:mm:ss</timeStampFormat>

<!-- custom javascript -->


<scripts>
<![CDATA[
$(document).ready(function() {

});
]]>
</scripts>

<!-- custom styles -->


<styles>
<![CDATA[

]]>
</styles>

</configuration>
</extentreports>
___________________________________________________________________________________
___________________________________________________________________________________
____________________________________________________________
Log4j.properties:

log4j.rootLogger=INFO,CONSOLE,R,HTML,TTCC

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.TTCC=org.apache.log4j.RollingFileAppender
log4j.appender.HTML=org.apache.log4j.FileAppender

log4j.appender.R.File=./log/testlog.log
log4j.appender.TTCC.File=./log/testlog1.log
log4j.appender.HTML.File=./log/application.html

log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern= %5p [%t] (%F:%L)- %m%n
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d - %c -%p - %m%n
log4j.appender.TTCC.layout=org.apache.log4j.TTCCLayout
log4j.appender.TTCC.layout.DateFormat=ISO8601
log4j.appender.HTML.layout=org.apache.log4j.HTMLLayout
log4j.appender.HTML.layout.Title=Application log
log4j.appender.HTML.layout.LocationInfo=true
___________________________________________________________________________________
___________________________________________________________________________________
____________________________________________________________
POM.XML:

<?xml version="1.0" encoding="UTF-8"?>


<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>intBankingV1</groupId>
<artifactId>intBankingV1</artifactId>
<version>0.0.1-SNAPSHOT</version>

<name>intBankingV1</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.6.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.17.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.17.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>5.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.aventstack/extentreports -->
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports</artifactId>
<version>5.0.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>

</dependencies>

<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults
(may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see
https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see
https://maven.apache.org/ref/current/maven-core/default-
bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see
https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>

</project>
___________________________________________________________________________________
___________________________________________________________________________________
_______________________________________________________________

testng.xml:

<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="InetBankingV9">

<listeners>
<listener class-name="com.inetbankin.utilities.Reporting"/>
</listeners>

<test name="Banking app test">


<parameter name="browser" value="chrome"/>
<classes>
<!-- <class name="com.inetbanking.Testcases.Tc_Login_001"/> -->
<class name="com.inetbanking.Testcases.Tc_Login_002"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________

You might also like