You just finished creating your web application and want to clean it from any possible bugs, imperfections, and performance issues. But you feel a bit overwhelmed with the choice of testing tools and frameworks and the articles singing their praises. In this article, we will explore using Selenium with Python for test automation.
Python is one of the oldest programming languages. It has a strong worldwide community that has already released quite a few packages for test automation, such as pytest, unittest, doctest, etc. Python can be used in multiple frameworks, code editors, and integrated development environments (IDEs).
The reasons this language managed to last so long and still keep evolving are its compactness, simplicity, and universality. The syntax is very intuitive, and coders can choose to use classes or functions because Python is both object-oriented and functional.
For test automation, it offers two more perks. Python works perfectly with command-line tools, enabling you to build continuous integration/continuous delivery (CI/CD) pipelines. This language executes compile and run steps in one leap, thus, becoming the number one choice for beginners.
Not to mention that it works with Selenium, currently one of the most used frameworks for testing web applications.
Selenium is a testing suite or set of tools. It is open-source and was developed specifically for automated cross-browser testing of web applications. Selenium became so popular that Apple even integrated it into Safari starting from macOS El Capitan.
One of Selenium’s tools is WebDriver. It is an application programming interface (API) and a collection of libraries that access different web browsers (using driver proxies) and perform actions from the testing script. Therefore, one can say that Selenium and WebDriver help your testing script—for instance, one written in Python—to talk to the web browser.
Selenium is not limited to running Python-based scripts. It has integrated language bindings that map functions and objects from one programming language with available functions and objects in another. For instance, Selenium has a click() command that simulates user clicks and allows you to use Cascading Style Sheets (CSS) selectors to point the automated test to where it should apply this command on a web page.
You will need a few components to run your Python-based automated tests:
A code editor
Selenium’s WebDriver for Python
A driver proxy
We will focus on the macOS installation in this tutorial. Two reasons are behind this choice: (1) macOS is quite popular among developers compared to Windows, (2) the installation is similar to how you would have done it on Linux.
In effect, we're taking care of two platforms at once.
All these steps should be done in your command-line tool. On macOS, it is typically Terminal.
2. Python installation:
brew install python
3. Selenium/WebDriver installation:
pip3 install selenium
In case it throws an error, try these commands:
curl -O https://bootstrap.pypa.io/get-pip.pysudo python3 get-pip.py
4. Driver proxy installation:
Go to the list of browser drivers for WebDriver, find the download link of the latest release for your browser version, and save the ZIP file.
After you unzip the downloaded archive, you get a folder with binary files. Those are extra executables that help Selenium talk with your browser. You can save the folder anywhere on your PC, but then you will have to switch the current directory in your command tool every time you want to start the driver.
A more convenient way is to add the folder to your system’s PATH environmental variable so the binaries can be started from any directory.
Create a directory and place the folder there. It can be something like:
Go to Terminal and type in (for ChromeDriver):
$ export PATH="$PATH:/path/to/chromedriver"
The exact PATH variable-changing commands for other browsers can be found here.
Close all active Terminal windows.
Open a new one.
Type in an example binary file name:
If the message “Starting ChromeDriver …” appears, then you did everything correctly.
Selenium needs a .py file with the code script saved locally on your machine where it can access it. After creating such a script using the code editor of your choice, make sure it can establish a browser session using the ChromeDriver. This can be done with the two simple lines that you need to put on top of your own code:
from selenium.webdriver import Chrome
with Chrome() as driver:
#your code starts here with an indent
We will stick to the ChromeDriver example. If you had a break in between, do the following:
Open a new Terminal window.
Then go to the directory where you saved your .py file:
Finally, execute the following command:
This will start the test. Breathe out, you’ve made it!
To test complex applications or run a few tests in parallel, you need serious computing power. Moving your automated tests to the cloud helps your developers avoid scalability issues. This way, they can shift their attention to bugs and performance exclusively.
In one of our Getting Started tutorials, you can learn about the Sauce Labs architecture and how it enables Selenium-based web application testing. If you need to test an application that runs on a private server and requires a proxy, you can use our Sauce Connect Proxy. The setup is a breeze, but if you need a detailed explanation, you will find the documentation here.
For Python newbies, we shared an automated test script that interacts with our website in the Chrome browser. You can find it on our GitHub page. No need to change it.