Week7 - Day 2
Week7 - Day 2 ~30 mins

Week 7 - Day 2 - POM with Page Factory

package base;

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

import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;

import io.github.bonigarcia.wdm.WebDriverManager;
import utils.ReadExcel;


public class ProjectSpecificMethods {
public ChromeDriver driver;
public String excelFileName;

@DataProvider(name="fetchData")
public String[][] sendData() throws IOException {
return ReadExcel.excelRead(excelFileName);
}

@BeforeMethod
public void startApp() {
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));

}

@AfterMethod
public void closeBrowser() {

driver.close();
}

}
package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;

import base.ProjectSpecificMethods;

public class CreateLeadPage extends ProjectSpecificMethods {

public CreateLeadPage(ChromeDriver driver) {
this.driver = driver;

}

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);
}
}
package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;

import base.ProjectSpecificMethods;

public class HomePage extends ProjectSpecificMethods {

public HomePage(ChromeDriver driver) {
this.driver = driver;

}

public LoginPage clickLogoutButton() {
driver.findElement(By.className("decorativeSubmit")).click();

return new LoginPage(driver);
}

public MyHomePage clickCrmsfaLink() {
driver.findElement(By.linkText("CRM/SFA")).click();

return new MyHomePage(driver);
}

public HomePage verifyHomePage() {
boolean displayed = driver.findElement(By.linkText("CRM/SFA")).isDisplayed();

if (displayed) {
System.out.println("Home Page is loaded");
}
return this;
}

}
package pages;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindAll;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.FindBys;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;

import base.ProjectSpecificMethods;

public class LoginPage extends ProjectSpecificMethods {

public LoginPage(ChromeDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);

}

@CacheLookup

@FindBy(how=How.CLASS_NAME, using="inputLogin") List<WebElement> eleUsername;

@CacheLookup
@FindBy(how=How.ID, using="password") WebElement elePassword;

//@FindBy(how=How.CLASS_NAME, using="decorativeSubmit") WebElement eleLoginButton;
@FindBy(how=How.XPATH, using="//input[@class='decorativeSubmit']") WebElement eleLoginButton;

public LoginPage enterUsername(String username) throws InterruptedException {

eleUsername.get(0).sendKeys(username);

//eleUsername.sendKeys(username);

//driver.findElement(By.id("username")).sendKeys(username);

//Thread.sleep(5000);
return this;
}

public LoginPage enterPassword(String password) {
elePassword.sendKeys(password);
//driver.findElement(By.id("password")).sendKeys(password);

return this;

}


public HomePage clickLoginButton() {
eleLoginButton.click();
//driver.findElement(By.className("decorativeSubmit")).click();

return new HomePage(driver);
}

public LoginPage clickLoginForNegativeData() {
eleLoginButton.click();
return this;
}

//AND condition
/*
* @FindBys( {
*
* @FindBy(how=How.CLASS_NAME, using="inputLogin"),
*
* @FindBy(how=How.XPATH, using="//input[@id='username']") }
*
* ) WebElement eleUsername;
*/



//OR condition
/*
* @FindAll( {
*
* @FindBy(how=How.CLASS_NAME, using="inputLogin"),
*
* @FindBy(how=How.XPATH, using="//input[@id='username123']") }
*
* ) WebElement eleUsername;
*/









}
package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;

import base.ProjectSpecificMethods;

public class MyHomePage extends ProjectSpecificMethods {

public MyHomePage(ChromeDriver driver) {
this.driver = driver;

}

public MyLeadsPage clickLeadsLink() {
driver.findElement(By.linkText("Leads")).click();

return new MyLeadsPage(driver);
}

}
package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;

import base.ProjectSpecificMethods;

public class MyLeadsPage extends ProjectSpecificMethods {
public MyLeadsPage(ChromeDriver driver) {
this.driver = driver;

}

public CreateLeadPage clickCreateLeadLink() {
driver.findElement(By.linkText("Create Lead")).click();

return new CreateLeadPage(driver);
}

}
package pages;

import org.openqa.selenium.chrome.ChromeDriver;

import base.ProjectSpecificMethods;

public class ViewLeadPage extends ProjectSpecificMethods {

public ViewLeadPage(ChromeDriver driver) {
this.driver = driver;

}

public ViewLeadPage verifyFirstName() {
System.out.println("first name displayed");
return this;
}
}

Test Cases

package testcases;

import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import base.ProjectSpecificMethods;
import pages.LoginPage;

public class CreateLead extends ProjectSpecificMethods{

@BeforeTest
public void setFileName() {
excelFileName ="CreateLead";

}


@Test(dataProvider = "fetchData")
public void runCreateLead(String username,String password,String company,String fName, String lName) throws InterruptedException {
new LoginPage(driver)
.enterUsername(username)
.enterPassword(password)
.clickLoginButton()
.clickCrmsfaLink()
.clickLeadsLink()
.clickCreateLeadLink()
.enterCompanyName(company)
.enterFirstName(fName)
.enterLastName(lName)
.clickCreateLeadButton()
.verifyFirstName();

}

}
package testcases;

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 setFileName() {
excelFileName ="Login";

}

@Test(dataProvider = "fetchData")
public void loginLogout(String username, String password) throws InterruptedException {

new LoginPage(driver)
.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;




}

}
<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>org.testleag</groupId>
<artifactId>POM</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0-alpha-7</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.3.0</version>
</dependency>

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.0.0</version>
</dependency>

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>6.10.0</version>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-testng</artifactId>
<version>6.10.0</version>
</dependency>

</dependencies>
</project>