In selenium, most frequently we hear page object model.
I have asked people why did they call it as page object model ? Is there any specific reason behind this ?
I got general answer saying “we store all locators in a page as static final strings in a page class”
Is this correct explanation ? are we doing page object model as is ? below is another view point.
In general we call page object model because every method in a page returns the page object instead of string/float/int or any standard return types. This will provide flexibility in writing test cases easily.
Step 1 : Define Page class
package test.pom; public class page1 extends pageRep { public page1() { super(); } public page1 testM1() { System.out.println("Method 1 Page 1"); return this; } public page1 testM2() { System.out.println("Method 2 Page 1"); return this; } }
Create another page to illustrate navigation between pages.
package test.pom; public class page2 extends pageRep { public page2() { super(); } public page2 test_M1() { System.out.println("Method 1 Page 2"); return this; } public page2 test_M2() { System.out.println("Method 2 Page 2"); return this; } }
If you observe clearly, the above classes are returning the page class as object and extending the class page repository. The class page repository is the class where we use to navigate between the pages.
Step 2: Create page repository for navigation between pages
package test.pom; public class pageRep { public pageRep() { System.out.println("Calling the super class"); } public page1 navigateToPage1() { return new page1(); } public page2 navigateTopage2() { return new page2(); } }
Step 4 : While creating the test case you can see it will be simple as we can keep “.” to see the methods in the class as each method returns the class object again, you will have visibility of all methods.
Page repository helps us to navigate between pages.
This is the best illustration of the Java extends concept which we can use to write our tests in selenium.
package test.testcases; import test.pom.pageRep; public class tests { public static void main(String[] args) { pageRep start = new pageRep(); start .navigateToPage1().testM1().testM2() .navigateTopage2().test_M1().test_M2() .navigateToPage1().testM1(); } }
output:
Calling the super class Calling the super class Method 1 Page 1 Method 2 Page 1 Calling the super class Method 1 Page 2 Method 2 Page 2 Calling the super class Method 1 Page 1