Skip to content

Session 47 Introduction to POM

ЁЯза Page Object Model (POM) тАФ Concept Summary

тЬЕ What is POM?

  • POM рдореНрд╣рдгрдЬреЗ Framework рдирд╛рд╣реА, рддреЛ рдПрдХ Design Pattern рдЖрд╣реЗ.

  • POM рдЪрд╛ рдЙрдкрдпреЛрдЧ рдЖрдкрдг Web elements рдЖрдгрд┐ рддреНрдпрд╛рдВрдЪреНрдпрд╛рд╡рд░ рд╣реЛрдгрд╛рд▒реНрдпрд╛ actions рд╡реНрдпрд╡рд╕реНрдерд┐рдд рд╡реЗрдЧрд│рдВ рдареЗрд╡рдгреНрдпрд╛рд╕рд╛рдареА рдХрд░рддреЛ.

  • рдпрд╛рдордзреНрдпреЗ рдкреНрд░рддреНрдпреЗрдХ web page рд╕рд╛рдареА рдПрдХ Java class рддрдпрд╛рд░ рдХреЗрд▓реА рдЬрд╛рддреЗ тАФ рдЬреА рддреНрдпрд╛ page рдЪреЗ locators рдЖрдгрд┐ action methods contain рдХрд░рддреЗ.


ЁЯзй Structure of POM Class

рд╣рд░ рдПрдХ POM class рдордзреНрдпреЗ рдЦрд╛рд▓реАрд▓ 3 рднрд╛рдЧ рдЕрд╕рддрд╛рдд:

  1. Locators тЖТ WebElements рд╢реЛрдзрд╛рдпрд▓рд╛

  2. Constructor тЖТ Driver inject рдХрд░рд╛рдпрд▓рд╛

  3. Action Methods тЖТ WebElement рд╡рд░ .click(), .sendKeys() рд╡рдЧреИрд░реЗ actions perform рдХрд░рд╛рдпрд▓рд╛

public class LoginPage { WebDriver driver; // global driver

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// Constructor
public LoginPage(WebDriver driver) {
    this.driver = driver;
}

// Locator
By username = By.xpath("//input\[@name='username'\]");

// Action Method
public void enterUsername(String user) {
    driver.findElement(username).sendKeys(user);
}

}


ЁЯзн Step-by-Step Process (Without PageFactory)

  1. рдкреНрд░рддреНрдпреЗрдХ Page рд╕рд╛рдареА рдПрдХ Page Object Class рддрдпрд╛рд░ рдХрд░рд╛рдпрдЪреА (LoginPage, HomePage etc.)

  2. рддреНрдпрд╛ class рдордзреНрдпреЗ рддреНрдпрд╛ page рдЪреЗ Locators рд▓рд┐рд╣рд╛рдпрдЪреЗ.

  3. рдкреНрд░рддреНрдпреЗрдХ locator рд╕рд╛рдареА рддреНрдпрд╛рдЪрд╛ рдПрдХ action method рддрдпрд╛рд░ рдХрд░рд╛рдпрдЪрд╛.

  4. Constructor рдордзреНрдпреЗ driver pass рдХрд░рд╛рдпрдЪрд╛.

  5. Test class рдордзреНрдпреЗ рддреНрдпрд╛ Page class рдЪрд╛ object create рдХрд░реВрди test case рд▓рд┐рд╣рд╛рдпрдЪрд╛.


ЁЯЫая╕П Locators рдХрд╕реЗ рд╢реЛрдзрд╛рдпрдЪреЗ - SelectorHub Shortcut

  1. Webpage рд╡рд░ F12 рджрд╛рдмреВрди DevTools рдЙрдШрдб.

  2. >> рдХреНрд▓рд┐рдХ рдХрд░реВрди SelectorHub tab рдЙрдШрдб.

  3. рдбрд╛рд╡реАрдХрдбреЗ рдЕрд╕рд▓реЗрд▓рд╛ "Select element" arrow рдХреНрд▓рд┐рдХ рдХрд░реВрди element select рдХрд░.

  4. SelectorHub рдордзреНрдпреЗ "click to generate locators Page and multiple Selectors" рдХреНрд▓рд┐рдХ рдХрд░.

  5. рд╕рд░реНрд╡ locators рддрдпрд╛рд░ рдЭрд╛рд▓реНрдпрд╛рд╡рд░, Copy All рд╡рд░ рдХреНрд▓рд┐рдХ рдХрд░реВрди рддреЗ code рдордзреНрдпреЗ paste рдХрд░.

тЮбя╕П Final output рдЕрд╕рдВ рджрд┐рд╕реЗрд▓:

By username = By.xpath("//input[@name='username']"); By password = By.xpath("//input[@name='password']"); By loginBtn = By.xpath("//button[@type='submit']");


тЪЩя╕П POM with PageFactory

ЁЯФз What is PageFactory?

  • PageFactory рд╣реА Selenium рдЪреА рдПрдХ utility рдЖрд╣реЗ рдЬреА locators рд▓рд╛ initialize рдХрд░рддреЗ рдЖрдгрд┐ рдХреЛрдб neat рдареЗрд╡рддреЗ.

  • рдЖрдкрдг @FindBy annotation рд╡рд╛рдкрд░рддреЛ рдЖрдгрд┐ PageFactory.initElements() рдиреЗ initialize рдХрд░рддреЛ.

ЁЯТб Example:

public class LoginPage { WebDriver driver;

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Constructor
public LoginPage(WebDriver driver) {
    this.driver = driver;
    PageFactory.initElements(driver, this); // initialize all @FindBy
}

// Locators using @FindBy
@FindBy(xpath = "//input\[@name='username'\]")
WebElement username;

@FindBy(xpath = "//input\[@name='password'\]")
WebElement password;

@FindBy(xpath = "//button\[@type='submit'\]")
WebElement loginBtn;

// Action Methods
public void enterUsername(String user) {
    username.sendKeys(user);
}

public void enterPassword(String pass) {
    password.sendKeys(pass);
}

public void clickLogin() {
    loginBtn.click();
}

}


ЁЯФм Test Class (TestNG Class)

TestNG class рдордзреНрдпреЗ рдлрдХреНрдд test methods + validation/assertions рдЕрд╕рддрд╛рдд. PageObject class рдордзреНрдпреЗ рдХреБрдард▓рдВрдЪ validation рдареЗрд╡рд╛рдпрдЪрдВ рдирд╛рд╣реА.

public class LoginTest { WebDriver driver;

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
@BeforeMethod
public void setup() {
    driver = new ChromeDriver();
    driver.get("https://example.com/login");
}

@Test
public void testValidLogin() {
    LoginPage login = new LoginPage(driver);
    login.enterUsername("admin");
    login.enterPassword("admin123");
    login.clickLogin();
    // Add validation/assertion here
}

}


ЁЯУЭ Important Points Recap

  • тЬЕ POM is not a framework, it is a design pattern.

  • тЬЕ Create separate Page class for each screen/page.

  • тЬЕ Page class = Locators + Constructor + Action methods.

  • тЭМ Page class should not contain test cases or validations.

  • тЬЕ Test class = Only test logic & validations.

  • тЬЕ With PageFactory тЖТ use @FindBy, PageFactory.initElements()

  • тЭМ You donтАЩt add PageObject classes in testng.xml file. Only Test classes are added there.


рд╣реЛ рдирд╛ рдУрдордХрд╛рд░! рдЖрддрд╛ рдЦрд╛рд▓реА рдореА "Without PageFactory" рд╡рд╛рдкрд░реВрди Page Object Model (POM) рдХрд╕рд╛ implement рдХрд░рд╛рдпрдЪрд╛, рддреЗ рд╕рдВрдкреВрд░реНрдг format рдордзреНрдпреЗ рджрд┐рд▓рдВрдп тАФ рд╕рдЧрд│реНрдпрд╛ pointwise explanation рд╕реЛрдмрдд, рдЕрдЧрджреА рдЖрдкрд▓реНрдпрд╛ style рдордзреНрдпреЗ тАФ рдорд░рд╛рдареА + English mix.


ЁЯТ╗ ЁЯФз POM without PageFactory тАФ Full Notes

тЬЕ Basic Idea:

  • рдЗрдереЗ рдЖрдкрдг PageFactory рд╡рд╛рдкрд░рдд рдирд╛рд╣реА.

  • Locators define рдХрд░рдгреНрдпрд╛рд╕рд╛рдареА By class рд╡рд╛рдкрд░рддреЛ.

  • WebElement access рдХрд░рддрд╛рдирд╛ driver.findElement(locator) рд╡рд╛рдкрд░рддреЛ.

  • Each page = рдПрдХ class тЖТ Locators + Constructor + Action methods


ЁЯПЧя╕П Page Object Class Structure (Without PageFactory):

public class LoginPage { WebDriver driver;

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
// тЬЕ Constructor (inject driver)
public LoginPage(WebDriver driver) {
    this.driver = driver;
}

// ЁЯФН Locators
By usernameField = By.xpath("//input\[@name='username'\]");
By passwordField = By.xpath("//input\[@name='password'\]");
By loginBtn = By.xpath("//button\[@type='submit'\]");

// ЁЯза Action Methods
public void enterUsername(String user) {
    driver.findElement(usernameField).sendKeys(user);
}

public void enterPassword(String pass) {
    driver.findElement(passwordField).sendKeys(pass);
}

public void clickLogin() {
    driver.findElement(loginBtn).click();
}

}


ЁЯзк Test Class (with TestNG):

public class LoginTest { WebDriver driver;

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
@BeforeMethod
public void setup() {
    driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.get("https://example.com/login");
}

@Test
public void testLoginValidCredentials() {
    LoginPage login = new LoginPage(driver);
    login.enterUsername("admin");
    login.enterPassword("admin123");
    login.clickLogin();
    // validation/assertion рдЗрдереЗ рдХрд░рд╛рдпрдЪрд╛
}

@AfterMethod
public void tearDown() {
    driver.quit();
}

}


ЁЯза Breakdown in Marathi + English:

Component

Description (рдорд░рд╛рдареАрддреВрди рд╕рдордЬрд╛рд╡рд▓реЗрд▓рдВ)

By Locators

Web element рд╢реЛрдзрдгреНрдпрд╛рд╕рд╛рдареА рд╡рд╛рдкрд░рддрд╛рдд. рдЙрджрд╛: By.xpath(...)

Constructor

Driver рд▓рд╛ class рдордзреНрдпреЗ inject рдХрд░рддреЛ. Without PageFactory, рд╣реА step manually рдХрд░рд╛рд╡реА рд▓рд╛рдЧрддреЗ.

Action Methods

рд╡реЗрдЧрд╡реЗрдЧрд│реНрдпрд╛ actions (click(), sendKeys()) рд╡реЗрдЧрд│реНрдпрд╛ method рдордзреНрдпреЗ рд▓рд┐рд╣рд╛рдпрдЪреНрдпрд╛.

driver.findElement(loc)

рдкреНрд░рддреНрдпреЗрдХ locator рд╕рд╛рдареА manually element access рдХрд░рд╛рдпрдЪрдВ.


ЁЯУМ Advantages of Without PageFactory:

  • тЬЕ Code transparency тАФ рд╕рдЧрд│рдВ manually рд▓рд┐рд╣рд┐рддреЛ рдореНрд╣рдгреВрди control рдЖрдкрд▓реНрдпрд╛рдХрдбреЗ рдЕрд╕рддреЛ.

  • тЬЕ Beginners рд╕рд╛рдареА perfect to understand internals of Selenium.

тЪая╕П Disadvantages:

  • тЭМ Code lengthy рд╣реЛрддреЛ.

  • тЭМ Element initialize repeat рд╣реЛрддрдВ (every time findElement() call).

  • тЭМ Readability рдХрдореА рдЕрд╕рддреЗ large projects рдордзреНрдпреЗ.


ЁЯФБ Summary: POM Without PageFactory

Part

Description

Locator Format

By username = By.xpath("...")

Access Method

driver.findElement(username)

Setup

Constructor injects driver manually

Structure

Class = Constructor + Locators + Action Methods


ЁЯе│ рдЖрддрд╛ рддреБрдЭреНрдпрд╛рдХрдбреЗ рджреЛрдиреНрд╣реА version рдЪреЗ clear, formatted notes рдЖрд╣реЗрдд тАФ PageFactory рд╕реЛрдмрдд рдЖрдгрд┐ Without PageFactory.

ЁЯФД Comparison: Without vs With PageFactory

Feature

Without PageFactory

With PageFactory

Locator Declaration

By username = By.xpath(...)

@FindBy(xpath="...")

Element Access

driver.findElement()

Direct object тЖТ username.sendKeys()

Initialization

No special method

Use PageFactory.initElements()

Code Clarity

Medium

High тАУ More readable and clean