Part 2. Framework Structure (PyTest-BDD)

Part 2. Framework Structure

Introduction

In this section, we will create our project along with the Python packages it needs and the appropriate framework structure

Let’s begin 🙂

Pipenv

Pipenv is a tool that aims to bring the best of all packaging worlds (bundler, composer, npm, cargo, yarn, etc.) to the Python world. It handles all our Python packages as well as creating our virtual environment in a new shell.

Installing Pipenv

  1. Enter the following command to install pipenv via pip on the user level, to prevent breaking any system-wide packages.
    pip install --user pipenv

Setting up Pipenv Shell

Next, let’s set up our virtual environment in a new shell

  1. Open up a Terminal (or Command prompt) and change directory in to your project root…
    cd path/to/python-selenium-framework
  2. Enter the following command to create the virtual environment shell…
    pipenv shell

Installing our Python packages

We will now use pipenv to install our Python packages locally to our project.  This way, any one else who clones our repository can easily install all necessary packages listed in our Pipfile by entering pipenv install 🙂

pytest-bdd

  1. Enter the following command, whilst in the root of the project to install the pytest-bdd framework…
    pipenv install pytest-bdd

selenium

  1. Enter the following command, whilst in the root of the project to install selenium
    pipenv install selenium

webdriver-manager

  1. Enter the following command, whilst in the root of the project to install webdriver-manager, which will automatically manage our webdriver binaries for us…
    pipenv install webdriver-manager

Now, we can open up our project in our chosen IDE, so let’s do that 🙂

Framework Structure

Good practice when using pytest-bdd is to create a parent test directory, and then have features and step_defs sub-directories/python packages contained within the test directory.

  1. Add the following directories and python packages so that your project tree looks the same/similar to below…

Creating our ConfTest file

Next, we will create our conftest.py python file which will contain our setUp and tearDown functions for our webdriver

  1. In the test python package, add a new Python file and name it conftest.py (this is where we will store our shared steps and fixtures)
  2. Add the following in the file (I have added comments to explain what is going on)…
    import pytest
    from selenium.webdriver import Chrome
    from webdriver_manager.chrome import ChromeDriverManager
    
    BASE_URL = "https://www.zagat.com"  # set the base url
    
    
    @pytest.fixture()
    def browser():
        # set the driver to the Chrome driver binary that is installed via our webdriver-manager package
        driver = Chrome(ChromeDriverManager().install())
    
        # wait for 5 seconds after launching the browser
        driver.implicitly_wait(5)
    
        # maximize the browser window
        driver.maximize_window()
    
        yield driver  # below gets executed 'after' tests
        driver.quit()  # quit the driver
    
    

The foundations are now setup.  In the next section, we will begin writing our first BDD scenario 🙂