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 рднрд╛рдЧ рдЕрд╕рддрд╛рдд:
-
Locators тЖТ WebElements рд╢реЛрдзрд╛рдпрд▓рд╛
-
Constructor тЖТ Driver inject рдХрд░рд╛рдпрд▓рд╛
-
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 | |
}
ЁЯзн Step-by-Step Process (Without PageFactory)
-
рдкреНрд░рддреНрдпреЗрдХ Page рд╕рд╛рдареА рдПрдХ Page Object Class рддрдпрд╛рд░ рдХрд░рд╛рдпрдЪреА (LoginPage, HomePage etc.)
-
рддреНрдпрд╛ class рдордзреНрдпреЗ рддреНрдпрд╛ page рдЪреЗ Locators рд▓рд┐рд╣рд╛рдпрдЪреЗ.
-
рдкреНрд░рддреНрдпреЗрдХ locator рд╕рд╛рдареА рддреНрдпрд╛рдЪрд╛ рдПрдХ action method рддрдпрд╛рд░ рдХрд░рд╛рдпрдЪрд╛.
-
Constructor рдордзреНрдпреЗ driver pass рдХрд░рд╛рдпрдЪрд╛.
-
Test class рдордзреНрдпреЗ рддреНрдпрд╛ Page class рдЪрд╛ object create рдХрд░реВрди test case рд▓рд┐рд╣рд╛рдпрдЪрд╛.
ЁЯЫая╕П Locators рдХрд╕реЗ рд╢реЛрдзрд╛рдпрдЪреЗ - SelectorHub Shortcut
-
Webpage рд╡рд░
F12рджрд╛рдмреВрди DevTools рдЙрдШрдб. -
>>рдХреНрд▓рд┐рдХ рдХрд░реВрди SelectorHub tab рдЙрдШрдб. -
рдбрд╛рд╡реАрдХрдбреЗ рдЕрд╕рд▓реЗрд▓рд╛ "Select element" arrow рдХреНрд▓рд┐рдХ рдХрд░реВрди element select рдХрд░.
-
SelectorHub рдордзреНрдпреЗ "click to generate locators Page and multiple Selectors" рдХреНрд▓рд┐рдХ рдХрд░.
-
рд╕рд░реНрд╡ 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 рдареЗрд╡рддреЗ. -
рдЖрдкрдг
@FindByannotation рд╡рд╛рдкрд░рддреЛ рдЖрдгрд┐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 | |
}
ЁЯФм 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 | |
}
ЁЯУЭ 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.xmlfile. 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 рдХрд░рдгреНрдпрд╛рд╕рд╛рдареА
Byclass рд╡рд╛рдкрд░рддреЛ. -
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 | |
}
ЁЯзк 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 | |
}
ЁЯза 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