by BelR with
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
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())
Proposed by @Abdessalam-Mouttaki from QSI Conseil 🙏
This use case consists of creating an expense in KiwiHR application :
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
- 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
- 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
- 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 withfirefox
, prepend CLI with settingBROWSER
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 withedge
, prepend CLI with settingBROWSER
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
-
- pytest | PyPI
- HtmlTestRunner | PyPI
- Unit testing framework | docs.python.org
- How to use unittest-based tests with pytest | docs.pytest.org
- Selenium | PyPI
- Selenium pytest plugin | PyPI
- Webdriver Manager for Python | PyPI
- Selenium with Python | Page Objects Model | selenium-python.readthedocs.io