In last post, we have seen Introduction of Page Object Model in details.
In this post we will see how to implement page object model in selenium webdriver.
The main idea of POM is to separate web elements and test scripts to achieve one to many relationship. You define web element at single place and you can use that in many scripts. It makes maintenance of web elements easy if you want to modify or add new.
To achieve this, we can store web elements in a property file, an excel file, in a java class or in table of database. In this post, we will store web elements in a java class file.
POM does not say anything specifically on categorization of web elements but it is good practice to group web elements as per web page name. For example: Web elements present on login page should be stored in LoginPage.java class and web elements on registration page should be stored in RegistrationPage.java. Advantage of this approach will be easy accessing of web elements. You just need to navigate to LoginPage.class to access login page web elements.
We will implement POM for below scenario:
- Access this URL.
- Click on Sign In button which is in Home page.
- Type Email Address in Email Address text box which is in Login page.
- Type password in Password text box which is in Login page.
- Click on Sign In button which is in Login page.
In above scenario, we will access two web pages “Login” and “Home” pages. So, we will create two java class: HomePage.java and LoginPage.java.
There is another good practice in POM is to keep actions/methods on web elements on same page. For example: You need to click on SignIn button. So, you should create a method clickOnSignButton method and keep in HomePage.java class.
Note: SignIn button is in LoginPage and HomePage both. POM helps you in distinguish similar web elements as well.
Disadvantages of Plain POM:
1. Its not optimized. More memory utilization.
2. No Lazy initialization.
Selenium has inbuilt page object model called PageFactory which is more advanced and optimized which we will see in next posts.
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.