Kickstart Your Journey in UI Automation Testing with Selenium
Written on
Chapter 1: Introduction to UI Automation Testing
This guide is crafted for those who are new to both Selenium and Python. If you have a basic familiarity with these technologies but have yet to develop a substantial project in either, you're in the right place. We will outline the complete process which includes: 1) establishing a Selenium project in Python, 2) composing an automation test, and 3) running that test in a live Chrome browser.
Inspired by the resource from BrowserStack, this guide incorporates updated sample code reflecting the latest Selenium release, along with additional explanations to enhance understanding.
Pre-requisites
A fundamental grasp of Python, Selenium, and basic HTML is necessary. Python stands out as a robust and widely-used programming language, with its own syntax, API, and libraries essential for writing and executing programs. On the other hand, Selenium is a powerful UI automation framework predominantly employed for UI testing. It is compatible with major web browsers like Chrome, Edge, Firefox, Safari, and Internet Explorer, functioning by mimicking real user interactions.
While you don't need to memorize every HTML element and attribute, familiarity with the basic components, such as form and input elements, is crucial.
Software Recommendation
For development, it's advisable to use PyCharm by JetBrains. The free Community Edition is recommended until you reach a level where the Professional Edition becomes necessary.
Section 1.1: Project Setup
To kick things off, initiate a new Python project in PyCharm. It’s prudent to create a Git repository for each project to effectively track changes. A simple main.py script will help quickly verify if your setup is functioning properly.
If you have a pre-installed Python version, you can select it; otherwise, opt for a version that PyCharm can download and install for you. Once the project is initialized, modify the main.py file to display the message "UI Automation with Selenium and Python". Run the file by clicking the green triangle button on the toolbar.
Section 1.2: Installing the Selenium Library
You can install the Selenium library through PyCharm’s Python Packages window, or alternatively, use Python's built-in package installer via the terminal. To do this, type pip install selenium and press Enter.
Section 1.3: Writing Your First Automation Test
from selenium import webdriver
driver = webdriver.Chrome()
print(driver.title)
driver.quit()
Video Tutorial: Hands-On UI Testing With Python
Section 1.4: Submitting a Search Inquiry
For our next test, we will organize the code into separate functions for clarity. This involves locating the search input box, clearing its content, and entering "getting started in Python". Subsequently, we’ll find and click the Go button, then confirm that the browser navigates to the expected URL.
Here’s the code snippet for this functionality:
search_input_field = driver.find_element(By.ID, "id-search-field")
search_input_field.clear()
search_input_field.send_keys("getting started in Python")
search_go_button = driver.find_element(By.ID, "submit")
search_go_button.click()
To find the search results, we will utilize a CSS selector, which can be structured using the Developer tools:
results = driver.find_elements(By.CSS_SELECTOR, ".list-recent-events li")
Here’s the complete function:
from selenium import webdriver
from selenium.webdriver.common.by import By
def tc2_search_getting_started():
driver = webdriver.Chrome()
try:
search_input_field = driver.find_element(By.ID, "id-search-field")
search_input_field.clear()
search_input_field.send_keys("getting started in Python")
search_go_button = driver.find_element(By.ID, "submit")
search_go_button.click()
print("current url: " + driver.current_url)
results = driver.find_elements(By.CSS_SELECTOR, ".list-recent-events li")
print(f"number of results: {len(results)}")
finally:
driver.quit()
Section 1.5: Executing Tests Using Python's Unittest Framework
For the final step, we’ll utilize Python's unittest framework instead of invoking test functions directly. This will streamline the process of running multiple tests. Modify main.py to create a unit test class that extends unittest.TestCase. Extract setup and teardown methods for browser management, and validate results through assertions.
Here’s how the final code looks:
import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
class TestPythonOrg(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
def tearDown(self):
self.driver.quit()
def test_tc1_get_python_org_title(self):
driver = self.driver
self.assertEqual("Welcome to Python.org", driver.title)
def test_tc2_search_getting_started(self):
driver = self.driver
search_input_field = driver.find_element(By.ID, "id-search-field")
search_input_field.clear()
search_input_field.send_keys("getting started in Python")
search_go_button = driver.find_element(By.ID, "submit")
search_go_button.click()
results = driver.find_elements(By.CSS_SELECTOR, ".list-recent-events li")
self.assertGreater(len(results), 0)
if __name__ == '__main__':
unittest.main()
PyCharm facilitates running tests either individually or as a whole, while also providing a test explorer to review execution results.
Video Tutorial: Project Setup and Getting Started with Selenium
Summary
This article serves as an introductory resource for newcomers eager to embark on UI automation testing using Selenium with Python. It encompasses project setup, writing automation tests, and executing them in a real browser environment. Drawing from existing resources, it has been updated with the most recent Selenium release and enriched with further clarifications.
For additional references: