Week8 - Day 1
~30 mins
Week 8 - Day 1 - POM with Extent Reports
package base;
import java.io.File;
import java.io.IOException;
import java.time.Duration;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.DataProvider;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.MediaEntityBuilder;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import io.github.bonigarcia.wdm.WebDriverManager;
import utils.ReadExcel;
public class ProjectSpecificMethods {
public ChromeDriver driver;
public String excelFileName;
public static ExtentHtmlReporter reporter;
public static ExtentReports extent;
public ExtentTest test,node;
public String testName,testDescription,testAuthor,testCategory;
public int takeSnap() throws IOException {
int ranNum = (int) (Math.random() * 99999);
File source = driver.getScreenshotAs(OutputType.FILE);
File target = new File("./snaps/img"+ranNum+".png");
FileUtils.copyFile(source, target);
return ranNum;
}
public void reportStep(String msg, String status) throws IOException {
if(status.equalsIgnoreCase("pass")) {
node.pass(msg,MediaEntityBuilder.createScreenCaptureFromPath(".././snaps/img"+takeSnap()+".png").build());
}
else if(status.equalsIgnoreCase("fail")) {
node.fail(msg,MediaEntityBuilder.createScreenCaptureFromPath(".././snaps/img"+takeSnap()+".png").build());
throw new RuntimeException();
}
}
@BeforeMethod
public void startApp() {
node = test.createNode(testName);
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://leaftaps.com/opentaps/control/main");
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
}
@BeforeSuite
public void startReport() {
reporter = new ExtentHtmlReporter("./reports/result.html");
reporter.setAppendExisting(true);
extent = new ExtentReports();
extent.attachReporter(reporter);
}
@BeforeClass
public void testDetails() {
test = extent.createTest(testName, testDescription);
test.assignCategory(testCategory);
test.assignAuthor(testAuthor);
}
@DataProvider(name = "fetchData")
public String[][] sendData() throws IOException {
return ReadExcel.excelRead(excelFileName);
}
@AfterMethod
public void closeBrowser() {
driver.close();
}
@AfterSuite
public void endReport() {
extent.flush();
}
}
package pages;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import com.aventstack.extentreports.ExtentTest;
import base.ProjectSpecificMethods;
public class CreateLeadPage extends ProjectSpecificMethods {
public CreateLeadPage(ChromeDriver driver,ExtentTest node) {
this.driver = driver;
this.node = node;
}
public CreateLeadPage enterCompanyName(String company) {
driver.findElement(By.id("createLeadForm_companyName")).sendKeys(company);
return this;
}
public CreateLeadPage enterFirstName(String fName) {
driver.findElement(By.id("createLeadForm_firstName")).sendKeys(fName);
return this;
}
public CreateLeadPage enterLastName(String lName) {
driver.findElement(By.id("createLeadForm_lastName")).sendKeys(lName);
return this;
}
public ViewLeadPage clickCreateLeadButton() {
driver.findElement(By.name("submitButton")).click();
return new ViewLeadPage(driver,node);
}
}
package pages;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import com.aventstack.extentreports.ExtentTest;
import base.ProjectSpecificMethods;
public class HomePage extends ProjectSpecificMethods {
public HomePage(ChromeDriver driver,ExtentTest node) {
this.driver = driver;
this.node = node;
}
public LoginPage clickLogoutButton() {
driver.findElement(By.className("decorativeSubmit")).click();
return new LoginPage(driver,node);
}
public MyHomePage clickCrmsfaLink() {
driver.findElement(By.linkText("CRM/SFA")).click();
return new MyHomePage(driver,node);
}
public HomePage verifyHomePage() throws IOException {
try {
driver.findElement(By.linkText("CRM/SFA")).isDisplayed();
reportStep("crmsfa link is displayed", "pass");
} catch (Exception e) {
reportStep("crmsfa link is not displayed", "fail");
}
return this;
}
}
package pages;
import java.io.IOException;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import com.aventstack.extentreports.ExtentTest;
import base.ProjectSpecificMethods;
public class LoginPage extends ProjectSpecificMethods {
public LoginPage(ChromeDriver driver,ExtentTest node) {
this.driver = driver;
this.node = node;
}
public LoginPage enterUsername(String username) throws InterruptedException, IOException {
try {
driver.findElement(By.id("username")).sendKeys(username);
reportStep("username "+username+" is entered successfully", "pass");
} catch (Exception e) {
reportStep("username "+username+" is not entered successfully", "fail");
}
return this;
}
public LoginPage enterPassword(String password) throws IOException {
try {
driver.findElement(By.id("password")).sendKeys(password);
reportStep("password "+password+" is entered successfully", "pass");
} catch (Exception e) {
reportStep("password "+password+" is not entered successfully", "fail");
}
return this;
}
public HomePage clickLoginButton() throws IOException {
try {
driver.findElement(By.className("decorativeSubmit")).click();
reportStep("login button clicked successfully", "pass");
} catch (Exception e) {
reportStep("login buttonn is not clicked successfully", "fail");
}
return new HomePage(driver,node);
}
public LoginPage clickLoginForNegativeData() {
driver.findElement(By.className("decorativeSubmit")).click();
return this;
}
}
package pages;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import com.aventstack.extentreports.ExtentTest;
import base.ProjectSpecificMethods;
public class MyHomePage extends ProjectSpecificMethods {
public MyHomePage(ChromeDriver driver,ExtentTest node) {
this.driver = driver;
this.node = node;
}
public MyLeadsPage clickLeadsLink() {
driver.findElement(By.linkText("Leads")).click();
return new MyLeadsPage(driver,node);
}
}
package pages;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import com.aventstack.extentreports.ExtentTest;
import base.ProjectSpecificMethods;
public class MyLeadsPage extends ProjectSpecificMethods {
public MyLeadsPage(ChromeDriver driver,ExtentTest node) {
this.driver = driver;
this.node = node;
}
public CreateLeadPage clickCreateLeadLink() {
driver.findElement(By.linkText("Create Lead")).click();
return new CreateLeadPage(driver,node);
}
}
package pages;
import org.openqa.selenium.chrome.ChromeDriver;
import com.aventstack.extentreports.ExtentTest;
import base.ProjectSpecificMethods;
public class ViewLeadPage extends ProjectSpecificMethods {
public ViewLeadPage(ChromeDriver driver,ExtentTest node) {
this.driver = driver;
this.node = node;
}
public ViewLeadPage verifyFirstName() {
System.out.println("first name displayed");
return this;
}
}
Test Cases
package testcases;
import java.io.IOException;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import base.ProjectSpecificMethods;
import pages.LoginPage;
public class CreateLead extends ProjectSpecificMethods{
@BeforeTest
public void setDetails() {
excelFileName ="CreateLead";
testName = "Create Lead";
testDescription = "Create lead with mandatory data";
testCategory = "functional";
testAuthor = "Hari";
}
@Test(dataProvider = "fetchData")
public void runCreateLead(String username,String password,String company,String fName, String lName) throws InterruptedException, IOException {
new LoginPage(driver,node)
.enterUsername(username)
.enterPassword(password)
.clickLoginButton()
.clickCrmsfaLink()
.clickLeadsLink()
.clickCreateLeadLink()
.enterCompanyName(company)
.enterFirstName(fName)
.enterLastName(lName)
.clickCreateLeadButton()
.verifyFirstName();
}
}
package testcases;
import java.io.IOException;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import base.ProjectSpecificMethods;
import pages.HomePage;
import pages.LoginPage;
public class VerifyLoginWithValidData extends ProjectSpecificMethods{
@BeforeTest
public void setDetails() {
excelFileName ="Login";
testName = "LoginWithValidData";
testDescription = "Verify login for valid data";
testCategory = "functional";
testAuthor = "Hari";
}
@Test(dataProvider = "fetchData")
public void loginLogout(String username, String password) throws InterruptedException, IOException {
new LoginPage(driver,node)
.enterUsername(username)
.enterPassword(password)
.clickLoginButton()
.verifyHomePage();
}
}
package utils;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadExcel {
public static String[][] excelRead(String fileName) throws IOException {
//step1: set up the excel document path
XSSFWorkbook wb = new XSSFWorkbook("./data/"+fileName+".xlsx");
//step2: set up the worksheet
XSSFSheet ws = wb.getSheet("Sheet1");
//to find the number of rows in the worksheet
int rowCount = ws.getLastRowNum();
//to find the number of cells in a row
int cellCount = ws.getRow(1).getLastCellNum();
//declare 2D array
String[][] data = new String[rowCount][cellCount];
for (int i = 1; i <= rowCount; i++) {
for (int j = 0; j < cellCount; j++) {
String text = ws.getRow(i).getCell(j).getStringCellValue();
System.out.println(text);
data[i-1][j] = text;
}
}
//to close the workbook
wb.close();
return data;
}
}