Page Object Model(POM) based Testing using Selenium WebDriver
Introduction
Maintaining 1000 lines of code in a single class file is a heavy task and also it increases its complexity. In order to maintain the project structure and efficient performance of the selenium scripts, it is necessary to use different pages for different tasks.
To ease the access of distributing the code into different modules, the Page Object Model(POM) comes to the rescue. In this blog, we will be learning some of the core concepts of Page Object Model(POM).
What is Page Object Model?
Page Object Model is a design pattern which has become popular in test automation for enhancing test maintenance and reducing code duplication. A page object is an object-oriented class that serves as an interface to a page of your AUT.
The tests than use the methods of this page object class whenever they need to interact with the UI of that page. The benefit is that if UI changes for the page, the tests don’t need to be changed, only the code within the page object needs to change.
Subsequently all changes to support that new UI are located in one place so we don’t have to edit much.
How to Implement POM?
Create a New Maven Project
Create a Maven Project so we don’t have to add all jar files into the library which are needed for our project.
It will automatically download and add all the jar files into your project which are required by just adding the dependencies into the POM.xml file of your project.
Advantages of POM
- Makes our code cleaner and easy to understand – keeps our tests and element locators separately.
- Easy to visualise each step of the scenario, view and edit test cases intuitively.
- Test cases become short and optimised as we can reuse page object methods in the POM classes.
- Any UI change can easily be implement, update and maintain into the Page Objects and Classes.
- Re-usability of code – object repository is independent of test cases.
How to Implement POM?
Create a New Maven Project
Create a Maven Project so we don’t have to add all jar files into the library which are needed for our project.
It will automatically download and add all the jar files into your project which are required by just adding the dependencies into the POM.xml file of your project.
Create a new Package
After the above step create a new package under our project by any desired name of your choice. The package which you are creating is a user-defined package which will create a folder in your workspace.
Create a New Class
After creating the package you have to create a class under the package which you have created in above step. We can also create an object of a class and access it from another class.
Create a Class for Browser
We will create a method while passing parameters with it having driver, browser name and URL of the website.
Under this method we will provide multiple browser options for the user to choose between them and pass the requirement drivers under the specific conditions of the browser.
After above step we will maximise the browser and add ImplicitWait and pageLoadTimeout with it, so if the processor of the system is slow or internet is slow than elements can wait according to the given time.
Than we will pass the url of the website using driver.get(appURL); and in the appURL we will pass the website URL.
Now, we will create a method for browser quit having parameter driver. So when our script is completed than the browser will be quit automatically.
Create a Class for Login Page
We will now create all the elements which are on Login Page using @FindBy so we can directly call them using WebElement. Initialise all the elements together under the class so they can be accessible from anywhere.
Now create a method where all the WebElements of the Login Page will be called to perform some actions. In the method we will pass two parameters for username and password of the website.
Create a Class for Login Page Test
Now we will create another class where all other classes will be called. Before starting of a function we will call @Test which is used to run your script with TestNG.
Create a void method and first call startapplication() method from BrowserFactory class to start the browser while passing parameters which are defined in the method.
Create an object for Login Page with any name and initialise all WebElements using PageFactory. Now call the method where WebElements actions are defined under a method from an object which you have created.
Now, call the method QuitBrowser() from the class BrowserFactory which is used to close the browser after your script.
View of Page Object Model(POM)
Now, if the AUT undergoes any change at the login page or at any page we just need to change the page object. Thus we don’t need to change our test script again and again (even for the new release or built).
The project structure will look like :
No comments:
Post a Comment