- The Page Object pattern represents the screens of your web app as a series of objects and encapsulates the features represented by a page.
- It allows us to model the UI in our tests.
- A page object is an object-oriented class that serves as an interface to a page of your AUT.
- Reduces the duplication of code
- Makes tests more readable and robust
- Improves the maintainability of tests, particularly when there is frequent change in the AUT. (Useful in Agile methodology based projects)
let's take one scenario and see how we can automate it without and with Page Object Pattern Scenario:
- Open http://newtours.demoaut.com/ site
- Login to the site.
- Log out. Now if we are not using any design patten the testcase will look like below
01.
@Test
02.
public
void
testLogin() {
03.
driver.findElement(By.id(
"userName"
)).clear();
04.
driver.findElement(By.id(
"userName"
)).sendKeys(
"testuser33"
);
05.
06.
driver.findElement(By.id(
"password"
)).clear();
07.
driver.findElement(By.id(
"password"
)).sendKeys(
"password123"
);
08.
09.
driver.findElement(By.id(
"login"
)).click();
10.
11.
driver.findElement(By.linkText(
"SIGN-OFF"
)).click();
12.
13.
}
Now let's see how the code will look like if we will have to use the Page Object Pattern with Page Factory.
LoginPage.java - contains Login page web elements and functions
01.
package
Pages;
02.
03.
import
org.openqa.selenium.WebDriver;
04.
import
org.openqa.selenium.WebElement;
05.
import
org.openqa.selenium.support.FindBy;
06.
import
org.openqa.selenium.support.How;
07.
import
org.openqa.selenium.support.PageFactory;
08.
09.
public
class
LoginPage {
10.
11.
final
WebDriver driver;
12.
13.
@FindBy
(how = How.NAME, using =
"userName"
)
14.
private
WebElement usernameEditbox;
15.
16.
@FindBy
(how = How.NAME, using =
"password"
)
17.
private
WebElement passwordEditbox;
18.
19.
@FindBy
(how = How.NAME, using =
"login"
)
20.
private
WebElement singinButton;
21.
22.
public
LoginPage(WebDriver driver) {
23.
this
.driver = driver;
24.
}
25.
26.
public
void
enterUsername(String login) {
27.
usernameEditbox.clear();
28.
usernameEditbox.sendKeys(login);
29.
}
30.
31.
public
void
enterPassword(String password) {
32.
passwordEditbox.clear();
33.
passwordEditbox.sendKeys(password);
34.
}
35.
36.
public
void
clickSigninButton() {
37.
singinButton.click();
38.
}
39.
40.
public
FindFlightPage login(String login, String password) {
41.
enterUsername(login);
42.
enterPassword(password);
43.
clickSigninButton();
44.
return
PageFactory.initElements(driver, FindFlightPage.
class
);
45.
}
46.
}
FindFlightPage.java - contains Find Flight page web elements and functions
01.
package
Pages;
02.
03.
import
org.openqa.selenium.WebDriver;
04.
import
org.openqa.selenium.WebElement;
05.
import
org.openqa.selenium.support.FindBy;
06.
import
org.openqa.selenium.support.How;
07.
import
org.openqa.selenium.support.PageFactory;
08.
09.
public
class
FindFlightPage {
10.
11.
final
WebDriver driver;
12.
13.
public
FindFlightPage(WebDriver driver) {
14.
this
.driver = driver;
15.
}
16.
17.
@FindBy
(how = How.LINK_TEXT, using=
"SIGN-OFF"
)
18.
private
WebElement signOff;
19.
20.
public
LoginPage singOff(){
21.
signOff.click();
22.
return
PageFactory.initElements(driver, LoginPage.
class
);
23.
}
24.
}
LoginTest.java - Actual class which contains tests for login page
01.
package
Tests;
02.
03.
import
org.openqa.selenium.WebDriver;
04.
import
org.openqa.selenium.firefox.FirefoxDriver;
05.
import
org.openqa.selenium.support.PageFactory;
06.
import
org.testng.annotations.AfterClass;
07.
import
org.testng.annotations.BeforeClass;
08.
import
org.testng.annotations.Test;
09.
10.
import
Pages.FindFlightPage;
11.
import
Pages.LoginPage;
12.
13.
public
class
LoginTest {
14.
15.
WebDriver driver;
16.
private
static
String login =
"testuser33"
;
17.
private
static
String pass =
"password123"
;
18.
19.
@BeforeClass
20.
public
void
setUp()
throws
Exception {
21.
driver =
new
FirefoxDriver();
22.
driver.get(
"http://newtours.demoaut.com/mercuryreservation.php"
);
23.
}
24.
25.
@Test
26.
public
void
testLogin() {
27.
28.
LoginPage loginPage = PageFactory.initElements(driver, LoginPage.
class
);
29.
FindFlightPage findPage = loginPage.login(login, pass);
30.
31.
loginPage = findPage.singOff();
32.
}
33.
34.
@AfterClass
35.
public
void
tearDown()
throws
Exception {
36.
driver.quit();
37.
}
38.
}
Page Object Modeling Online Training
ReplyDeletehttp://www.21cssindia.com/courses/page-object-modeling-online-training-231.html
Page Object Modeling Course Contents
Why POM and what is POM
About page objet gem and its features
Creating POM Structure
Finding elements and creating pages for application
Creating panels, dialogues and other reusable components
Creating and Identifying reusable actions and verifications in pages
Page Factory
Using pages and panels in you test cases
About watir helper and handling Ajax elements
Creating routes for your application
Passing test data from yaml files
Data Magic
Using test data with Data Magic
Creating your tests and running your tests
Call Us +919000444287