Hey everyone! This is Sam! As a recent QA tester, I've spent a lot of time ensuring that the software I build actually works as it should. Other developers may actually hate me, but it's part of the job. Quite frankly though, QA is there to make sure that in a long run, developers would not worry so much! A crucial aspect to do this is testing, and one particular type of testing—end-to-end testing—has always been a strong focus for me. Almost all of QA testers I know started with Selenium-so let's discuss that here as an example
Selenium is a suite of tools designed to automate web browsers. Think of it as a robot that can navigate websites, interact with elements, input data, and verify behavior, all just like a human would, but through code. The basic idea is to have code, instead of manual effort, carry out tests on your web application through a browser.
End-to-end (E2E) testing is a type of software testing that validates the entire application workflow from start to finish. It simulates a real user interacting with the application from the frontend through the backend.
Combining Selenium with end-to-end tests allows you to automate the verification of user workflows in a real browser environment. This is where we get the best of both worlds.
Let’s walk through setting up Selenium with Python.
Make sure you have Python and pip (Python's package installer) installed. Download them from the official Python website.
Create a new directory to store your project (e.g., e2e_testing
).
mkdir e2e_testing cd e2e_testing
I recommend setting up a virtual environment to isolate your project's dependencies:
python -m venv venv source venv/bin/activate # On macOS/Linux venv\Scripts\activate # On Windows
Install the Selenium library using pip:
pip install selenium
You need to download a specific WebDriver implementation for the browser you want to automate (e.g., ChromeDriver for Chrome, GeckoDriver for Firefox). Place the driver executable in a location that is in your system's PATH. Download these drivers from their official websites.
Create a file named test_example.py
and add the following code:
# test_example.py from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import unittest class TestExample(unittest.TestCase): def setUp(self): # Initialize WebDriver self.driver = webdriver.Chrome() # Change to Firefox(), Safari(), or Edge() if you use those browsers self.driver.get("https://example.com") def tearDown(self): self.driver.quit() def test_verify_title(self): WebDriverWait(self.driver, 10).until( EC.title_contains("Example Domain") ) self.assertEqual(self.driver.title, "Example Domain") def test_verify_h1_text(self): h1 = WebDriverWait(self.driver, 10).until( EC.presence_of_element_located((By.TAG_NAME, "h1")) ) self.assertEqual(h1.text, "Example Domain") if __name__ == '__main__': unittest.main()
Run your test with the following command:
python -m unittest test_example.py
The terminal response should show if the test has passed or failed.
..
----------------------------------------------------------------------
Ran 2 tests in 1.212s
OK
test_example.py
)# test_example.py from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import unittest class TestExample(unittest.TestCase): def setUp(self): # Initialize WebDriver self.driver = webdriver.Chrome() # Change to Firefox(), Safari(), or Edge() if you use those browsers self.driver.get("https://example.com") def tearDown(self): self.driver.quit() def test_verify_title(self): WebDriverWait(self.driver, 10).until( EC.title_contains("Example Domain") ) self.assertEqual(self.driver.title, "Example Domain") def test_verify_h1_text(self): h1 = WebDriverWait(self.driver, 10).until( EC.presence_of_element_located((By.TAG_NAME, "h1")) ) self.assertEqual(h1.text, "Example Domain") if __name__ == '__main__': unittest.main()
from selenium import ...
: Imports required Selenium modules.webdriver
: To control the browser.By
: To locate HTML elements.WebDriverWait
: To wait for elements to be available.expected_conditions
: To check for specific conditions of an element.import unittest
: Imports unittest
which is a testing framework in Python.class TestExample(unittest.TestCase):
: Defines a class that inherits from unittest.TestCase
. This is how we tell Python this class is meant for testing.setUp
: Initializes each test by opening a browser instance to a specific page.self.driver = webdriver.Chrome()
: Initializes a Chrome driver to automate a Chrome browser.tearDown
: Cleans up each test after it has finished.self.driver.quit()
: Closes the browser window.test_verify_title
: A test case verifying page title.WebDriverWait(self.driver, 10).until(EC.title_contains("Example Domain"))
: Waits up to 10 seconds until the page title has the string "Example Domain" in it.test_verify_h1_text
: A test case that locates an h1
HTML element and validates its text content.WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, "h1")))
: Waits up to 10 seconds until an h1
element is located in the DOM.if __name__ == '__main__': unittest.main()
: Starts the tests when the script is executed.Blocker | Solution |
---|---|
Browser driver executable not found | Ensure the correct driver is downloaded and is in a directory that's included in your system's PATH. |
Selenium module not found | Double-check that you have installed Selenium with pip install selenium . |
Browser crashes or hangs | Check browser and driver versions are compatible. Check you have the correct driver for your specific browser. |
Test script failing | Check the locators, assertions, and browser interactions in the script. Use debugging tools. |
WebDriverWait timeouts | Check if the element locator is correct, and if there are any delays that would cause it to timeout. |
Page elements not located properly | Double-check the page source code. Ensure you are using the correct CSS selectors or Xpaths. |
Inconsistent test results | Ensure test environments are the same or create logic that can handle variation. |
Feature | Selenium IDE | Selenium WebDriver |
---|---|---|
Setup | Easy, browser extension | Requires installing browser drivers and libraries |
Programming | No coding required | Coding required in programming languages like Python, Java, etc. |
Flexibility | Limited, mainly for simple recording and replay | Highly flexible, powerful for complex test scenarios |
Cross-Browser | Limited support, not all browsers | Full support for all major browsers |
Maintainability | Less maintainable for large complex tests | Well-suited for large, maintainable test suites |
Integration | Limited integration with other tools | Easily integrated into CI/CD pipelines and test frameworks |
Suitability | Quick prototyping and simple tests | Comprehensive E2E testing and large automation projects |
Selenium and end-to-end tests are essential.With automation, you can be more confident that your system works as you intended it. There is just simple no escape for this, HAHA! Everyone is bound to test anyways!