Introduction
This article is a part of a 30 day Python challenge series. You can find the links to all the previous posts of this series here:
Today I explored end-to-end Browser Automation Tests using Python by creating a basic automation testing project.
When it comes to implementing browser automation, Selenium is one of the most popular and widely used libraries. The Selenium Python library makes it very easy to use it with Python and run scripts to perform automated tests on websites.
Automation tests are essential for testing web applications in a real-world scenario, just as how a user would normally use the application. It is also used to perform cross-browser tests to ensure the application works on all target browsers.
I created a basic project to understand and implement the basics of browser automation testing with Selenium.
Creating a project
I created a new directory python-selenium and added a virtual environment to the project.
python -m venv venv
Installing Selenium
The next step is to install the Selenium package which can be installed in the project environment using the command:
pip install selenium
Installing the Drivers
Selenium uses drivers to interface with the browser. So browser-specific drivers need to be installed so that Selenium is able to create the browser instance for running the automation tests.
| Chrome: | https://sites.google.com/a/chromium.org/chromedriver/downloads |
| Edge: | https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/ |
| Firefox: | https://github.com/mozilla/geckodriver/releases |
| Safari: | https://webkit.org/blog/6900/webdriver-support-in-safari-10/ |
I downloaded the Firefox driver for this particular project. The driver needs to be the executable path.
(In Windows 10, place the driver file in the path where the Python application is installed, or any other location whose path is there in the environment variables)
Creating a Sample Automation Script
I created a Python script file automation.py in the root project directory.
Here is a simple code for opening a webpage using Selenium:
automation.py
- from selenium import web-driver
-
- browser = webdriver.Firefox()
- browser.get('https://tabandspace.com')
Selenium Cheat-sheet: Here is a cheat-sheet for commonly-used Selenium methods:
Configure Automation Testing in a Build Pipeline
I found a great article that describes the steps to implement web automation scripts in a build pipeline such as Github Workflow.
Selenium Base is a great wrapper on top of selenium that makes it easy to install drivers and add the automation script to a build pipeline.
https://dev.to/seleniumbase/running-browser-tests-from-github-workflows-with-seleniumbase-1oic
That’s all for today. Tomorrow is the last day of this challenge, where I shall be sharing the various Python resources I have collected during this month while learning Python.
Have a great one!