1
2 #1- We initialize the web elements using
3 PageFactory.initElements method
4
5 - We create connection in between our Driver
6 and the object of current class.
7
8 - So when we use the object of the class,
9 the object is already initialized with all
10 the web elements and able to use them.
11
12 PageFactory.initElements(Driver.getDriver(), this);
13
14 - #2 - We use @FindBy annotation to locate web elements.
15 @FindBy (xpath = "//something")
16 public WebElement exampleBox;
1Pom basically Creating java classes
2for the each page of our application,
3In my framework, I stored web elements and methods
4related to that Page into its own classes.
5
6Advantages of POM
7First of all, it makes our code REUSABLE.
8I don't have to repeatedly locate web elements
9in every single other class.
10Secondly it is EASY TO MAINTAIN. I store one
11webelement in one class and it is a lot easier to
12fix any issue with that web element because I know
13exactly where everything is stored.
14Thirdly is seperating webelements and our test cases.
15Our test cases will be cleaner therefore
16easy to manage overall.
17
18HOW I CREATE POM
19#1-I Create Public constructor of the class
20and pass PageFactory.initElements(objectOfClass, Driver)
21#2- I use @FindBy annotation to locate web elements
22 - FindBy annotation comes from Selenium.
1PageFactory is a class in selenium.
2It is used to achieve page factory design pattern,
3it helps to initialize the page objects in the
4class using initElements methods. When
5I create an object from the page class,
6pagefactory initializes the webelements
7just before interacting this web element.
8It means that once you call the element,
9PageFactory class will immediately locate the element
10and you will not get a staleElementReferenceException.
11
12When a page object is created,
13pagefactory driver (because of the constructor)
14will be linked to weblements @findby in that page.
15
16 public LoginPage(){
17 PageFactory.initElements(Driver.get(), this);
18 }