Table of Contents
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
- Enter the following command to install pipenv via pip on the user level, to prevent breaking any system-wide packages.
1pip install --user pipenv
Setting up Pipenv Shell
Next, let’s set up our virtual environment in a new shell
- Open up a Terminal (or Command prompt) and change directory in to your project root…
1cd path/to/python-selenium-framework - Enter the following command to create the virtual environment shell…
1pipenv 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
- Enter the following command, whilst in the root of the project to install the pytest-bdd framework…
1pipenv install pytest-bdd
selenium
- Enter the following command, whilst in the root of the project to install selenium…
1pipenv install selenium
webdriver-manager
- Enter the following command, whilst in the root of the project to install webdriver-manager, which will automatically manage our webdriver binaries for us…
1pipenv 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.
- 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
- 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)
- Add the following in the file (I have added comments to explain what is going on)…
1234567891011121314151617181920import pytestfrom selenium.webdriver import Chromefrom webdriver_manager.chrome import ChromeDriverManagerBASE_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 packagedriver = Chrome(ChromeDriverManager().install())# wait for 5 seconds after launching the browserdriver.implicitly_wait(5)# maximize the browser windowdriver.maximize_window()yield driver # below gets executed 'after' testsdriver.quit() # quit the driver
The foundations are now setup. In the next section, we will begin writing our first BDD scenario 🙂
Hey man! We connected on LinkedIn and I’ve since discovered your blogs/articles here. I wanted to point something out (at least for mac users); for me:
pip install –user pipenv
caused issues with being able to run pipenv shell in any other directory. I found that this helps install it globally:
sudo -H pip install -U pipenv
Thanks for this article!
Hi Erick,
Thanks for your comment.
I actually wrote this a fair while ago and today would not recommend using PipEnv.
I would instead look at using Poetry which is much better and is still being maintained.
https://python-poetry.org/