DWQA Ask QuestionCategory: SeleniumImplement Page Object Model with out Implementing Page Factory
admin Staff asked 5 years ago

Purpose of the Assignment: To implement the repository using Page object model. Also, to learn that there is different ways by which we can implement page object model. Mostly we use page factory implementation of page object model as it has been provided by selenium api itself. However we should also know other ways to implement the same.

URL to be used: http://parabank.parasoft.com/parabank/index.htm

Other tools: TestNG, Maven.

Problem Statement:

Automate three test cases of the above application, for Login page and Transfer Funds Page.

Create Page object Model file for Three Pages, Login, Menu items class and Transfer funds class.

Maintain Page objects in these classes but with out Page factory annotations.

Create 3-5 test cases in Testng and use these page object model class object for operations.

2 Answers
VaibhavD Staff answered 5 years ago

PO_LoginPage.java
package com.po;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.Reporter;
public class PO_LoginPage {
WebDriver driver;
public PO_LoginPage(WebDriver driver) {
this.driver = driver;
}
By txtbx_login = By.name(“username”);
By txtbx_password = By.name(“password”);
By btn_login = By.xpath(“//input[@type=’submit’ and @value=’Log In’]”);
By link_forget_login_info = By.linkText(“Forgot login info?”);
By link_register = By.linkText(“Register”);
By link_about_us_at_top = By.linkText(“About Us”);
By link_icon_mail = By.linkText(“contact”) ;
By link_read_more_latest_news = By.xpath(“//*[@id=\’rightPanel\’]/p[2]/a”);
By footer = By.xpath(“//div[@id=’footermainPanel’]”);
// Type email address
public void enterEmailAddress(String userName)
{
driver.findElement(txtbx_login).sendKeys(userName);
}
// Type password
public void enterPassword(String password)
{
driver.findElement(txtbx_password).sendKeys(password);
}
// Click on sign in button
public void clickOnSignInButton()
{
driver.findElement(btn_login).click();
}
// Single method to sign in
public void signIn(String userName, String password)
{
enterEmailAddress(userName);
enterPassword(password);
clickOnSignInButton();
}
// Click on link_forget_login_info
public void clickOnForgetLoginInfo()
{
driver.findElement(link_forget_login_info).click();
// validation
String expected = “ParaBank | Customer Lookup”;
String actual = driver.getTitle();
Assert.assertEquals(actual, expected);
}
// Click on link_register
public void clickOnRegister()
{
driver.findElement(link_register).click();
// validation
String expected = “ParaBank | Register for Free Online Account Access”;
String actual = driver.getTitle();
Assert.assertEquals(actual, expected);
}
// Click on link_about_us
public void clickOnAboutUs()
{
driver.findElement(link_about_us_at_top).click();
// validation
WebElement textAboutUs = driver.findElement(By.id(“rightPanel”));
Assert.assertEquals(true, textAboutUs.getText().contains(“a real bank!”));
}
// Click on link_icon_mail
public void clickOnMailIcon()
{
driver.findElement(link_icon_mail).click();
// validation after entering wrong credentials
String expected = “ParaBank | Customer Care”;
String actual = driver.getTitle();
Assert.assertEquals(actual, expected);
}
// Click on link_read_more_latest_news
public void clickOnReadMoreLatestNews()
{
driver.findElement(link_read_more_latest_news).click();
// validation after entering wrong credentials
String expected = “ParaBank | News”;
String actual = driver.getTitle();
Assert.assertEquals(actual, expected);
}
// verify Links Contained By Footer
public void verifyLinksContainedByFooter() throws InterruptedException, IOException
{
WebElement footerpanel = driver.findElement(footer);
// Get Footer element which contains all footer links
List<WebElement> col_all_links = footerpanel.findElements(By.xpath(“//a”));
int total_links_footer = footerpanel.findElements(By.tagName(“a”)).size();
Reporter.log(“Total links in footer= “+ total_links_footer, true);
int count_broken_links = 0;
//Saving all urls in the Array List
ArrayList<String> list_of_all_urls = new ArrayList<String>();
for(int i=0;i<total_links_footer;i++) {
list_of_all_urls.add(col_all_links.get(i).getAttribute(“href”));
//Creating URL object
URL url_link = new URL(list_of_all_urls.get(0));
//creating conn object for the URL
HttpURLConnection conn = (HttpURLConnection) url_link.openConnection();
conn.setRequestMethod(“GET”);
conn.setConnectTimeout(2000);
conn.connect();
if (conn.getResponseCode()!=200) {
count_broken_links++;
System.err.println(“Link is broken. Status code :” + conn.getResponseCode() + ” URL: ” + list_of_all_urls.get(i) );
}
}
Reporter.log(“Total broken_links in footer = “+ count_broken_links, true);
}
}
—————————————————–

PO_MenuPage.java

package com.po;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import org.testng.Reporter;

public class PO_MenuPage {

WebDriver driver;

public PO_MenuPage(WebDriver driver) {
this.driver = driver;
}

By link_open_new_account = By.linkText("Open New Account");
By link_account_overview = By.linkText("Accounts Overview");
By link_transfer_funds = By.linkText("Transfer Funds");
By link_logout = By.linkText("Log Out");

public void open_new_account() {
driver.findElement(link_open_new_account).click();
}

public void account_overview() {
driver.findElement(link_account_overview).click();
}

public void transfer_funds() {
driver.findElement(link_transfer_funds).click();
//validation
String s_title_expected = "ParaBank | Transfer Funds";
String s_title_actual = driver.getTitle();
Assert.assertEquals(s_title_expected, s_title_actual);
}

public void logout() {
driver.findElement(link_logout).click();
Reporter.log("Log out successful. " , true);
}

}

-------------------------------------------

PO_TransferFundPage.java

package com.po;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;
import org.testng.Reporter;

public class PO_TransferFundPage {

WebDriver driver;

public PO_TransferFundPage(WebDriver driver) {
this.driver = driver;
}

By txtbox_amount = By.id("amount");
By list_from_account = By.xpath("//*[@id=\'fromAccountId\']");
By list_to_account = By.className("toAccountId");
By btn_tranfer = By.xpath("//input[@value = 'Transfer']");
By txt_transfer_results = By.xpath("//div[@ng-if = 'showResult']");

public void set_amount_to_transfer(String arg) {
driver.findElement(txtbox_amount).sendKeys(arg);
Reporter.log("Amount to Transfer --> " + arg, true);
}

public void select_FromAccount(String arg) {
Select oFromList = new Select(driver.findElement(list_from_account));
oFromList.selectByVisibleText(arg);
Reporter.log("Transfer From Account --> " + arg, true);
}

public void select_ToAccount(String arg) {
Select oToList = new Select(driver.findElement(list_to_account));
oToList.selectByVisibleText(arg);
Reporter.log("Transfer To Account --> " + arg, true);
}

public void click_Transfer() {
driver.findElement(btn_tranfer).click();

//Validation for transfer success
WebElement result_after_transfer = driver.findElement(txt_transfer_results);
Assert.assertEquals(true, result_after_transfer.isDisplayed());
Reporter.log("Transfer successful....", true);
}


}

--------------------------------------------

TC_LoginPage.java
package com.tc;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.po.PO_LoginPage;
import com.utils.BrowserManager;
public class TC_LoginPage {
static WebDriver driver;
@BeforeTest
void setUp() throws Exception {
driver = BrowserManager.GetBrowser(“chrome”);
driver.get(“http://parabank.parasoft.com/parabank/index.htm&#8221;);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Reporter.log(“Test Case Execution started…”, true);
}
@AfterTest
void tearDown() throws Exception {
driver.close();
Reporter.log(“Test Case Excecuted.”, true);
}
@Test
void t01_verify_loginPage_title() {
String s_title_expected = “ParaBank | Welcome | Online Banking”;
String s_title_actual = driver.getTitle();
Assert.assertEquals(s_title_expected, s_title_actual);
Reporter.log(“Login Page title is verified”, true);
}
@Test
void t02_verify_website_logo_isDisplayed() {
WebElement web_logo = driver.findElement(By.className(“logo”));
Assert.assertTrue(web_logo.isDisplayed());
Reporter.log(“Website Logo is Displayed”, true);
}
@Test
void t03_login_into_parabank() {
PO_LoginPage poLogin = new PO_LoginPage(driver);
poLogin.signIn(“john”, “demo”);
// validation after login
String expected = “ParaBank | Accounts Overview”;
String actual = driver.getTitle();
Assert.assertEquals(actual, expected);
Reporter.log(“Login Successful”, true);
}
@Test
void t04_negative_tc_login_into_parabank() {
PO_LoginPage poLogin = new PO_LoginPage(driver);
poLogin.signIn(“john1”, “demo”);
// validation after entering wrong credentials
String expected = “ParaBank | Error”;
String actual = driver.getTitle();
Assert.assertEquals(actual, expected);
Reporter.log(“The usename and password could not be verified…”, true);
}
@Test
void t05_forget_login_info_parabank() {
PO_LoginPage poLogin = new PO_LoginPage(driver);
poLogin.clickOnForgetLoginInfo();
Reporter.log(“Forget Login Info verified…”, true);
}
@Test
void t06_find_about_us() {
PO_LoginPage poLogin = new PO_LoginPage(driver);
poLogin.clickOnAboutUs();
Reporter.log(“About Us …”, true);
}
@Test
void t07_register_to_parabank() {
PO_LoginPage poLogin = new PO_LoginPage(driver);
poLogin.clickOnRegister();
Reporter.log(“Fill form to register…”, true);
}

@Test
void t08_Mail_Customer_care() {
PO_LoginPage poLogin = new PO_LoginPage(driver);
poLogin.clickOnMailIcon();
Reporter.log(“Send mail to customer care…”, true);
}
@Test
void t09_Read_More_LatestNews() {
PO_LoginPage poLogin = new PO_LoginPage(driver);
poLogin.clickOnReadMoreLatestNews();
Reporter.log(“Read more latest news.”, true);
}
@Test
void t10_verifyLinksContainedByFooter() throws InterruptedException, IOException {
PO_LoginPage poLogin = new PO_LoginPage(driver);
poLogin.verifyLinksContainedByFooter();
Reporter.log(“Broken Links contained by footer is verified”, true);
}
}
————————————————————–
TC_TransferFundPage.java
package com.tc;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.testng.Reporter;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
import com.po.PO_LoginPage;
import com.po.PO_MenuPage;
import com.po.PO_TransferFundPage;
import com.utils.BrowserManager;
public class TC_TransferFundPage {
static WebDriver driver;
static PO_TransferFundPage poFundTransfer = new PO_TransferFundPage(driver);
static TC_LoginPage tcLogin = new TC_LoginPage();
@BeforeSuite
public static void setUpforTransferFund() throws Exception {
driver = BrowserManager.GetBrowser(“chrome”);
driver.get(“http://parabank.parasoft.com/parabank/index.htm&#8221;);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Reporter.log(“Test Case Execution started…”, true);
}
@AfterSuite
public void tearDown() throws Exception {
driver.close();
Reporter.log(“Test Case Excecuted.”, true);
}
@Test
public void t01_verify_TransferFund_Page_title() throws Exception {
PO_LoginPage poLogin = new PO_LoginPage(driver);
poLogin.signIn(“john”, “demo”);
Thread.sleep(1000);
PO_MenuPage poMenu = new PO_MenuPage(driver);
poMenu.transfer_funds();
Reporter.log(“TransferFund Page title is verified”, true);
}

@Test (dependsOnMethods = “t01_verify_TransferFund_Page_title”, enabled=false)
public void t02_transfer_funds_to_account() throws Exception {
PO_TransferFundPage poTransferFund = new PO_TransferFundPage(driver);
poTransferFund.set_amount_to_transfer(“10”);
poTransferFund.select_FromAccount(“54321”);
poTransferFund.select_ToAccount(“12345”);
poTransferFund.click_Transfer();
Reporter.log(“Fund Transfer is completed”, true);
}
}

Tejal Gadewar Staff answered 5 years ago

//Page Object Without Using Page Factory For Sign_In of  CS_Cart
//PO_POM_WithOutPageFactory
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class PO_POM_WithoutPageFactory {
WebDriver driver;
public PO_POM_WithoutPageFactory(WebDriver d)
{
this.driver=d;
}

//elements

private By my_acc=By.xpath(“//span[@class=’hidden-phone’]”);

private By sign_in=By.xpath(“//a[@data-ca-target-id=’login_block4′]”);

private By btn_sign_in=By.xpath(“//button[@name=’dispatch[auth.login]’]”);

public void click_On_SignIn()
{
driver.findElement(my_acc).click();
driver.findElement(sign_in).click();
driver.findElement(btn_sign_in).click();

}
}
 
 
//TC_Pom_WithOutPageFactory
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Reporter;
import org.testng.annotations.Test;
import po.PO_POM_WithoutPageFactory;
public class TC_Pom_WithOutPageFactory {

@Test
public void pomwithoutpagefactory()
{
System.setProperty(“webdriver.chrome.driver”, “F:\\automation\\chromedriver.exe”);
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get(“https://demo.cs-cart.com&#8221;);
Reporter.log(“cs-cart URL launched<br>”);

PO_POM_WithoutPageFactory pom=new PO_POM_WithoutPageFactory(driver);
pom.click_On_SignIn();
Reporter.log(“sign_in successfully”);
}

}

Supriya Kitukale Staff replied 5 years ago

//Page object model without using page factory for Parabank.
public class PO_LoginPage
{
WebDriver driver;
public PO_LoginPage(WebDriver d)
{
driver=d;
}

By username= By.xpath(“//input[@type=’text’ and @name=’username’]”);
By password =By.xpath(“//input[@type=’password’ and @name=’password’]”);
By button_login=By.xpath(“//input[@type=’submit’]”);

public void Set_username(String u)
{
try
{
driver.findElement(username).sendKeys(u);
}catch(Exception e)
{
e.printStackTrace();
}
}
public void Set_password(String p)
{
try
{
driver.findElement(password).sendKeys(p);
}catch(Exception e)
{
e.printStackTrace();
}
}
public void Click_Login()
{
try
{
driver.findElement(button_login).click();
}catch(Exception e)
{
e.printStackTrace();
}
}
public void KW_LoginPage(String u,String p)
{
Set_username(u);
Set_password(p);
Click_Login();
Reporter.log(“user login successfully”);
}

}

//TC for Login page

public class TC_Smoke
{
WebDriver driver;
@Test
public void TC_01_PageObjectModel_withoutPageFactory()
{
System.setProperty(“webdriver.chrome.driver”, “E:\\Vision IT\\chromedriver_win32\\chromedriver.exe”);
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
driver.get(“http://parabank.parasoft.com”);

PO_LoginPage login=new PO_LoginPage(driver);
login.KW_LoginPage(“john”, “demo”);