Creating a Selenium-Pytest Framework from scratch

Posted by Omar Ortega on Friday, January 3, 2025

In this first blog post we are going to learn on how to install the tools to start working with Pytest and Selenium. In this series of blogs I am going to go through on what is needed to create a POM (Page Object Model) framework from scratch, so you can create your own tests 🧐.

Installing all dependencies

We would need to first create a simple project, you can use whatever IDE you like, it can be VSCODE, PYCHARM (those instructions are out of the scope for this blog).

Then we would need to activate a python environment, to accomplish this we can use type in the terminal:

python -m venv venv

This command will create a new folder venv, now to activate the environment run this command:

macOS and Linux source venv/bin/activate

Windows To activate a Python virtual environment in Windows, you can run the batch file: env\scripts\activate.bat After activating the environment, you should see your environment name in parentheses before the directory name.

The next step is to create a requirements.txtfile, this file is used to manage your project’s dependencies, inside we are going to list them, in our case pytest and selenium.

pytest
selenium

Save the file, and to run the file we would use:

pip install -r requirements.text

Just make sure you are running on the same project root directory in your command line or terminal and that is it! 👍

Creating our first Selenium test

When I started using Pytest a question came to my mind “How does Pytest identify our tests?”, well… Pytest has a nice way to identify them, it has rules that you can even modify. Pytest will identify the rules as follows:

[pytest]
python_files = test_*
python_classes = *Tests
python_functions = test_*

It will identify as Tests the python files that start with test_ , the python classes that end with Tests, and the function’s signature that starts with tests_. Those are the default rules that you can modify if needed, just copy and paste the code above, and create a new file with name pytest.ini .

We now understand how pytest will identify our tests, now we are going to have some fun, lets create a file with name test_selenium_basic.py inside the root of our project. Inside the file we are going to type ( copy the next code if you like ):

from selenium import webdriver

def test_home_page_loaded():
    driver = webdriver.Chrome()
    driver.get("https://the-internet.herokuapp.com/")
    title = driver.title
    print(title)
    assert "The Internet" in title

The first line is going to import our selenium’s webdriver:

from selenium import webdriver

Next, we will create a function test_home_page_loaded() , inside the function the driver variable is going to be set to use an instance of a Google Chrome driver:

driver = webdriver.Chrome()

This is important to mention; selenium has different drivers for different web browsers, we can use chrome, firefox, safari, ms edge. Selenium now has a great capability of downloading in run time the drivers needed, and set them up, so you can just focus on creating your test scripts.

Selenium is going to launch automatically the Chrome browser for Testing, and it will navigate to the URL https://the-internet.herokuapp.com/, that action is being handled with the code line:

driver.get("https://the-internet.herokuapp.com/")

The final step is just to assert that everything is working correctly, Pytest has an assert library that can helps us to validate that the test we have in the Web page title is the one that we are expecting, it is always recommended to assert at the end of the Test Case, because that is the final goal of the Test Case, the below line is doing exactly that:

assert "The Internet" in title

Now the moment of truth, we need to run our tests, that is super simple, we just need to run the pytest command, pytest will look for the tests, remember that pytest has a default way to look for tests in files, classes, and functions?. Execute the command in your command line or terminal,

pytest

pytest

The test is executed!!, but what is that little green dot at the end of our python test file?, well… that is indicating that the test passed, but hold on… there is another way to execute our tests and to have more information,

pytest --verbose

We are passing the –verbose option, which is going to provide more information.

pytest2

It will display the test python file and the test function executed, and instead of the green dot, it will tell you the Test Cases status, in this case PASSED, that is it!!, we ran our first Test Case.

We will continue enhancing our Automation Framework in the next Posts!

I will be updating/maintaing the Automation Framework on Github, here is the link Pytest Selenium GH Project