Page Object Pattern in automation testing with Selenium WebDriver


Test automation with Selenium WebDriver and Page Object Pattern.

Today I want to talk about Page Object Pattern which very popular in test automation with Selenium WebDriver. In my post will be used Python binding for Selenium WebDriver and Google Chrome Driver.

There are several advantages of using Page Object Pattern:

Reduces the duplication of code: combine all actions to work with a web page in one place;
Makes tests more readable and robust: separation of test code and description of pages;
Improves the maintainability of tests, particularly when there are frequent changes on the pages.
For example, take a look how it works with login action in YouTube. Create a class with a description of this page using the Page Object Pattern:


"""Module MainPage for mapping the main page of Youtube."""

from page_objects import PageObject, PageElement


class MainPage(PageObject):
    """Class MainPage for mapping the main page of Youtube."""

    # Mapping web elements on the page.
    login_button = PageElement(css="#button>#text.style-scope.ytd-button-renderer.style-brand")

    def goto_login_page(self):
        """A method for opening the login page on the Youtube."""

        self.login_button.click()

This is mapping of Main Page of YouTube (https://youtube.com): one button (“SIGN IN”) and one method, which lead to the Login Page below:


"""Module LoginPage for mapping the login page of Youtube."""

import time

from selenium.webdriver.support import ui
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

from page_objects import PageObject, PageElement
import config
from utilities import Utilities


class LoginPage(PageObject, Utilities):
    """Class LoginPage for mapping the login page of Youtube."""

    # Mapping web elements on the page.
    email_field = PageElement(id_="identifierId")
    next_email_button = PageElement(id_="identifierNext")
    password_field = PageElement(css=".whsOnd.zHQkBf")
    next_password_button = PageElement(id_="passwordNext")

    def login(self, email=config.USER1['email'], password=config.USER1['password']):
        """A method for login on the Youtube."""

        self.email_field = email
        self.next_email_button.click()
        time.sleep(config.DELAY2)
        self.wait_visibility_by_css(".whsOnd.zHQkBf")
        self.password_field = password
        self.next_password_button.click()
        self.wait_visibility_by_css("#avatar-btn")

    def wait_visibility_by_css(self, css_path, timeout=config.DELAY1):
        """The overridden method for waiting visibility of web element on the page
        from Utilities class."""

        return ui.WebDriverWait(self.w, timeout).until(
            EC.visibility_of_element_located((By.CSS_SELECTOR, css_path)))

As you can see here we declared all input fields and button needed for test. Also here are methods, an important one is “login()”.And, finally, code of our test:


"""Module YoutubeTest for testing different features of Youtube."""

import unittest

import config
from utilities import Utilities
from page_object_model.main_page import MainPage
from page_object_model.login_page import LoginPage


class YoutubeTest(unittest.TestCase, Utilities):
    """Class YoutubeTest for testing different features of Youtube."""

    # Set URL.
    URL = ""

    def setUp(self):
        """"Set up"- method which is running before each test."""

        self._set_up()

    def test_login_to_youtube(self):
        """A method for login on the Youtube."""

        main_page = MainPage(self.driver, root_uri=config.DOMAIN)
        main_page.goto_login_page()
        login_page = LoginPage(self.driver)
        login_page.login(config.USER1['email'], config.USER1['password'])

        self.make_screenshot()

        print("Test 1: User is successfully logged in.")

    def tearDown(self):
        """"Tear down"- method which is running after each test."""

        self._tear_down()


if __name__ == '__main__':
    unittest.main()

In the testing method (“test_login_to_youtube()”), we create our objects and call their corresponding methods.

As you can see -- everything is simple.

Click here for source code.
Used references: