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

What Is Page Object Model Using Selenium Webdriver 20230227 Lyst5796

Uploaded by

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

What Is Page Object Model Using Selenium Webdriver 20230227 Lyst5796

Uploaded by

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

What is Page Object model using Selenium Webdriver?

1- It is design pattern in which will help you to maintain the code and code duplication,
which is a crucial thing in Test automation.
2- You can store all locators and respective methods in the separate class and Call them
from the test in which you have to use. So the benefit from this will be if any changes in
Page then you do not have to modify the test simply modify the respective page and that
all.
3- You can create a layer between your test script and application page, which you have
to automate.
4- In other words, it will behave as Object repository where all locators are saved.
Implementation of Page Object model using Selenium Webdriver
1 – Page Object model without PageFactory
2- Page Object Model with Pagefactory.
What is Page Factory in Selenium?

Page Factory in Selenium is an inbuilt extension of Page Object Model but it is


very optimized. It is a class where we use @FindBy annotation to find
WebElement and initElements() method to initialize web elements
automatically.
Advantages of using Page Object Model
 Increases code reusability - code to work with events of a page is written only
once and used in different test cases
 Improves code maintainability - any UI change leads to updating the code in page
object classes only leaving the test classes unaffected
 Makes code more readable and easy to understand.

@FindBy(locataorname=”value”)
Webelement user;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.support.CacheLookup;

import org.openqa.selenium.support.FindBy;

import org.openqa.selenium.support.How;

* This class will store all the locator and methods of login page

*/

public class LoginPage

WebDriver driver;
By username=By.id("user_login");

By password=By.xpath(".//*[@id='user_pass']");

By loginButton=By.name("wp-submit");

public LoginPage(WebDriver driver)

this.driver=driver;

public void loginToWordpress(String userid,String pass)

driver.findElement(username).sendKeys(userid);

driver.findElement(password).sendKeys(pass);

driver.findElement(loginButton).click();

public void typeUserName(String uid)

{
driver.findElement(username).sendKeys(uid);

public void typePassword(String pass)

driver.findElement(password).sendKeys(pass);

public void clickOnLoginButton()

driver.findElement(loginButton).click();

package com.wordpress.Testcases;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.testng.annotations.Test;

import com.wordpress.Pages.LoginPage;
public class VerifyWordpressLogin

@Test

public void verifyValidLogin()

WebDriver driver=new FirefoxDriver();

driver.manage().window().maximize();

driver.get("http://demosite.center/wordpress/wp-login.php");

LoginPage login=new LoginPage(driver);

login.clickOnLoginButton();

driver.quit();
}

Page Object Model using Selenium Webdriver with Page Factory


Selenium has built in class called PageFactory, which they mainly created for Page
Object purpose, which allows you to store elements in cache lookup.
The only difference, which you will get without PageFactory and with PageFactory, is
just initElement statement.Let us check the code and will see what changes required for
with PageFactory Approach
Code for Page Object Model Using Selenium Webdriver using Page Factory

http://artoftesting.com/automationTesting/pageObjectModel.html

You might also like