Skip to content

This project shows how to apply Page Object Model pattern in Selenium with Python. There are some test cases which are written against to KiwiHR web application (based on https://github.com/gunesmes/page-object-python-selenium).

Notifications You must be signed in to change notification settings

belr20/selenium-page-objects-model-with-python

Repository files navigation

by BelR with Python badge Selenium badge Pytest badge Allure badge Gitmoji badge

selenium_pom_with_python_cover

Page Objects Model (POM) is a design pattern that you can apply to develop web applications efficient test automation :

  • Easy to read test cases
  • Creating reusable code that can share across multiple test cases
  • Reducing the amount of duplicated code
  • If the user interface changes, the fix needs changes in only one place

Python concept

Basically POM consists of that each page is inherited from a base class which includes basic functionalities for all pages.
If you have some new functionality that each page has, you can simple add it to the base class.

BasePage class includes basic functionality and driver initialization

# base_page.py
class BasePage(object):
    def __init__(self, driver, base_url='https://qsi-conseil.kiwihr.com'):
        self.base_url = base_url
        self.driver = driver
        self.timeout = 30

    def find_element(self, *locator):
        return self.driver.find_element(*locator)

MainPage is derived from the BasePage class, it contains methods related to this page, which will be used to create test steps.

# main_page.py
class MainPage(BasePage):
    def __init__(self, driver):
        self.locator = MainPageLocators
        super().__init__(driver)  # Python3 version

    def check_page_loaded(self):
        return True if self.find_element(*self.locator.LOGO) else False

When you want to write tests, you should derive your test class from BaseTest which holds basic functionalities for your tests.
Then you can call pages and related methods in accordance with the steps in the test cases.

@allure.testcase(BASE_URL, 'LogIn page')
@pytest.mark.usefixtures("db_class")
class TestLogInPage(BaseTest):

    @allure.step("LogIn with VALID user")
    def test_login_with_valid_user(self):
        print("\n" + str(formal_test_cases(4)))
        login_page = LogInPage(self.driver)
        result = login_page.login_with_valid_user("valid_user")
        self.assertIn(BASE_URL, result.get_url())

KiwiHR use case

Proposed by @Abdessalam-Mouttaki from QSI Conseil 🙏
This use case consists of creating an expense in KiwiHR application :

kiwihr_expenses_fr_screenshot

You can get a free instance for 14 days here.
Then you have to copy/paste .env.example to .env & modify the corresponding variables with your :

  • KiwiHR instance URL
  • KiwiHR username/email
  • KiwiHR password

supplier, purchase_date & amount of the expense that will be created are accessible in tests\test_nouvelle_note_de_frais_page.py

Environment

  • First clone this repository (you can get help here)
git clone https://github.com/belr20/selenium-page-objects-model-with-unittest.git
cd selenium-page-objects-model-with-unittest
  • Then you should create & activate a virtual environment called venv
python -m venv venv
source venv/bin/activate  # On Linux
.\venv\Scripts\activate  # On Windows
  • Finally install dependencies
python -m pip install --upgrade pip wheel setuptools
pip install -r requirements.txt

Running Tests

  • If you want to run all tests with unittest
python -m unittest
  • If you want to run all tests with pytest
python -m pytest
  • If you want to run all tests with allure report available in reports/allure-results folder
pytest --alluredir=reports/allure-results
allure serve reports/allure-results

Allure report example

⚠️ How to install allure CLI here

  • If you want to run all tests with HTML report available in reports folder
python tests/base_test.py
  • If you want to run just a class
python -m unittest tests.test_login_page.TestLogInPage
  • If you want to run just a test method
python -m unittest tests.test_login_page.TestLogInPage.test_login_with_valid_user
  • Default browser is chrome, if you want to run test with firefox, prepend CLI with setting BROWSER to 'firefox'

    • On Linux

      BROWSER='firefox' python -m pytest
    • On Windows CMD

      set BROWSER = 'firefox'
      python -m pytest
    • On PowerShell

      $Env:BROWSER = 'firefox'
      python -m pytest
  • Default browser is chrome, if you want to run test with edge, prepend CLI with setting BROWSER to 'edge'

    • On Linux

      BROWSER='edge' python -m pytest
    • On Windows CMD

      set BROWSER = 'edge'
      python -m pytest
    • On PowerShell

      $Env:BROWSER = 'edge'
      python -m pytest

Resources






About

This project shows how to apply Page Object Model pattern in Selenium with Python. There are some test cases which are written against to KiwiHR web application (based on https://github.com/gunesmes/page-object-python-selenium).

Topics

Resources

Stars

Watchers

Forks