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
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
from selenium import web-driver browser = webdriver.Firefox() browser.get('https://tabandspace.com')
Here is a cheat-sheet for the commonly used selenium methods
I found a great article which describes the steps to implement web automation scripts in a build pipeline such as Github Workflow. Selenium Base https://github.com/seleniumbase/SeleniumBase is a great wrapper on top of selenium that makes it easy to install drivers and add the automation script to a build pipeline.
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!