CH02 - PageObjects, Fixtures, Pytest

Kunaal ThanikKunaal Thanik
3 min read

Chapter 02 - how to use Page Object Models, Fixtures, pyTest mark, Environment Variables, pytest.ini and conftest

Recap:

https://kunaal.hashnode.dev/automation-ch01-initialization

Previously, we set up the testing framework using Playwright and Python and ran test example.

Now:

Lets go one step further and make make it more robust using advanced features such as Page Object Models (tests, pages) , Fixtures, pyTest mark, Environment Variables, pytest.ini, conftest , and gitignore

  • Page Object Model:

    One of the biggest lessons I learned while scaling UI automation is the importance of maintainability. POM is a design pattern that simplifies this. By creating a separate class for each page of the application, I could organize locators and actions related to that page in one place. This abstraction has made my test scripts much easier to manage, reducing duplication and making updates a breeze when the UI changes.

  • Fixtures:

    When writing tests, setting up the environment can be repetitive. This is where pytest fixtures came to the rescue. Fixtures allow me to define reusable setups, like initializing a browser or creating test data, without duplicating code. They make tests cleaner, more maintainable, and save tons of time, especially when dealing with complex setups across multiple tests. Fixtures will be defined in conftest.

  • pyTest Mark:

    In pytest, pytest.mark is a way to add metadata to test functions. It helps in categorizing, organizing, and selecting tests to run based on specific conditions or criteria. This marking system enables running only a subset of tests, which can be useful in various testing scenarios.

    • Why to Use pytest.mark?

      1. Categorization: Group tests by functionality, feature, or any other criteria. For example, you can mark tests as @pytest.mark.login or @pytest.mark.api.

      2. Selective Running: Run specific sets of tests without executing the entire test suite.

      3. Conditional Execution: Control the execution of tests based on conditions, such as the presence of certain dependencies or environments.

      4. Parameterized Testing: Use marks to define and run parameterized tests efficiently, we will see this in tests.

      5. Tagging for Documentation: Tag tests for better documentation and understanding of what each test is intended to verify

        .

  • Environment file:

    Imagine managing multiple environments—staging, production, etc. I’ve found that storing URLs, API keys, and credentials in an environment file not only keeps things clean but also prevents hardcoding sensitive data directly in your tests.

    • Since it contains credentials so will not be pushed, available on local host only. You can create your own .env and store password such as PASSWORD=demo
  • pytest.ini

    This one’s a game-changer for me when working with pytest. It’s where I define plugins, set command-line options, and fine-tune how tests are discovered. Having this setup helps avoid cluttering the command line with repetitive flags.

  • conftest.py:

    If you’re into pytest, this file is pure gold. It lets you define fixtures and hooks that can be reused across your test suite. I’ve saved so much time by using conftest.py to manage test setups without repeating code.

  • .gitignore:

    When working with version control, I always make sure my .gitignore is up-to-date. It’s essential for keeping unnecessary or sensitive files—like environment configs and logs—out of your repository.

These files have become indispensable in my automation toolkit!

All Code available here on GitHub.

0
Subscribe to my newsletter

Read articles from Kunaal Thanik directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kunaal Thanik
Kunaal Thanik