Reading list Switch to dark mode

    Page Object Model(POM) based Testing using Selenium WebDriver

    Updated 20 October 2020

    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.

    Start your headless eCommerce
    now.
    Find out More

    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.

    POM_structure

    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.

    /**
     * Webkul Software.
     *
     * @category  Webkul
     * @author    Webkul
     * @copyright Copyright (c) 2010-2020 Webkul Software Private Limited (https://webkul.com)
     * @license   https://store.webkul.com/license.html
     */
    
    public class BrowserFactory {
    	
    	public static WebDriver startapplication(WebDriver driver, String BrowserName, String appURL)
    	{
    		if(BrowserName.equals("Chrome"))
    		{
    			System.setProperty("webdriver.chrome.driver", "./Drivers/chromedriver123.exe");
    			driver= new ChromeDriver();
    			
    		}
    		else if(BrowserName.equals("FireFox"))
    		{
    			System.setProperty("webdriver.gecko.driver", "./Drivers/geckodriver.exe");
    			driver = new FirefoxDriver();
    		}
    		else
    		{
    			System.out.println("Sorry this browser is not supported !!");
    		}
    		
    		driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
    		driver.manage().window().maximize();
    		driver.get(appURL);
    		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    		
    		return driver;
    	}

    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.

    /**
     * Webkul Software.
     *
     * @category  Webkul
     * @author    Webkul
     * @copyright Copyright (c) 2010-2020 Webkul Software Private Limited (https://webkul.com)
     * @license   https://store.webkul.com/license.html
     */
    
    public static void QuitBrowser(WebDriver driver)
    	{
    		driver.quit();
    	}

    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.

    /**
     * Webkul Software.
     *
     * @category  Webkul
     * @author    Webkul
     * @copyright Copyright (c) 2010-2020 Webkul Software Private Limited (https://webkul.com)
     * @license   https://store.webkul.com/license.html
     */
    
    @FindBy(xpath ="//input[@id='login_main_login']")
    WebElement username;
    	
    @FindBy(xpath ="//input[@id='psw_main_login']")
    WebElement password;
    	
    @FindBy(xpath ="//body/div[@id='tygh_container']/div[@id='tygh_main_container']/div[3]/div[1]/div[2]/div[1]/div[1]/div[1]/div[1]/form[1]/div[3]/div[1]/button[1]")
    WebElement button;

    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.

    /**
     * Webkul Software.
     *
     * @category  Webkul
     * @author    Webkul
     * @copyright Copyright (c) 2010-2020 Webkul Software Private Limited (https://webkul.com)
     * @license   https://store.webkul.com/license.html
     */
    
    public void loginto(String usrnme, String pswrd)
    {
    	username.sendKeys(usrnme);
    	password.sendKeys(pswrd);
    	button.click();
    }

    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.

    /**
     * Webkul Software.
     *
     * @category  Webkul
     * @author    Webkul
     * @copyright Copyright (c) 2010-2020 Webkul Software Private Limited (https://webkul.com)
     * @license   https://store.webkul.com/license.html
     */
    
    package Webkul;
    
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.support.PageFactory;
    import org.testng.annotations.Test;
    
    public class LoginTest 
    {
    	WebDriver driver;
    	
    	@Test
    	public void loginApp()
    	{
    		driver = BrowserFactory.startapplication(driver, "Chrome",          "http://192.168.10.106/login/");
    		LoginPage lgnpage = PageFactory.initElements(driver, LoginPage.class);
    		lgnpage.loginto("[email protected]", "admin");
    		BrowserFactory.QuitBrowser(driver);
    	}
    }

    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 :

    POM_structure

    In case you have any queries then feel free to ask in the comment section below.
    This is all about Page Object Model(POM) based Testing using Selenium WebDriver.

    Thanks for reading this blog.

    Happy testing!!

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home