🧩 What Are Assertions?
Assertions are checks or validations used in tests to confirm that:
A web element exists or is visible
The text or title matches expectations
A URL, attribute, or condition is correct
If an assertion fails, the test fails immediately and reports the mismatch.
🧰 1. Setup Example
Install Selenium (if you haven’t already):
pip install selenium
Basic setup for a test script:
from selenium import webdriver
from selenium.webdriver.common.by import By
import unittest
🧪 2. Using Assertions in a Test Class
Here’s a full example using Python’s built-in unittest framework:
import unittest
from selenium import webdriver
from selenium.webdriver.common.by import By
class TestGoogle(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.get("https://www.google.com")
def test_title(self):
driver = self.driver
self.assertIn("Google", driver.title) # ✅ Assert title contains "Google"
def test_search_box_visible(self):
driver = self.driver
search_box = driver.find_element(By.NAME, "q")
self.assertTrue(search_box.is_displayed()) # ✅ Assert element is visible
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main()
🧠 Explanation
setUp() → runs before each test (opens browser).
tearDown() → runs after each test (closes browser).
assertIn(), assertTrue() → check conditions and fail if not met.
🧩 3. Common Assertion Types (in unittest)
Assertion Description Example
assertEqual(a, b) Checks if a == b assertEqual(driver.title, "Home Page")
assertNotEqual(a, b) Checks if a != b assertNotEqual(url, old_url)
assertTrue(x) Checks if condition is True assertTrue(button.is_enabled())
assertFalse(x) Checks if condition is False assertFalse(element.is_selected())
assertIn(a, b) Checks if a in b assertIn("Login", driver.title)
assertNotIn(a, b) Checks if a not in b assertNotIn("Error", page_text)
assertIsNone(x) Checks if value is None assertIsNone(optional_element)
🧭 4. Example: Verifying Page Elements
def test_login_button(self):
driver = self.driver
login_btn = driver.find_element(By.ID, "loginButton")
# Check if visible
self.assertTrue(login_btn.is_displayed())
# Check if text matches
self.assertEqual(login_btn.text, "Login")
🔗 5. Example: Verifying URLs and Titles
def test_navigation(self):
driver = self.driver
driver.get("https://example.com")
self.assertEqual(driver.current_url, "https://example.com/")
self.assertIn("Example Domain", driver.title)
⚡ 6. Example: Assertions in pytest
If you prefer pytest, you can use simple assert statements (no unittest needed):
from selenium import webdriver
from selenium.webdriver.common.by import By
def test_google_title():
driver = webdriver.Chrome()
driver.get("https://www.google.com")
assert "Google" in driver.title # ✅ Same idea, simpler syntax
search_box = driver.find_element(By.NAME, "q")
assert search_box.is_displayed()
driver.quit()
✅ pytest automatically handles test discovery and better reporting.
🧠 7. Common Use Cases
Goal Example
Verify title assert "Dashboard" in driver.title
Verify element visible assert element.is_displayed()
Verify text value assert element.text == "Submit"
Verify checkbox checked assert checkbox.is_selected()
Verify URL assert driver.current_url.endswith("/home")
Verify attribute assert element.get_attribute("value") == "test"
🧩 8. Combine Assertions with Waits
To avoid false failures when elements load slowly:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
login_btn = wait.until(EC.visibility_of_element_located((By.ID, "loginButton")))
assert login_btn.is_displayed()
assert login_btn.text == "Login"
✅ Summary
Assertion Goal Technique
Check title/text assertEqual, assertIn, assert
Check visibility .is_displayed()
Check enabled .is_enabled()
Check selection .is_selected()
Check URL driver.current_url
Check attributes .get_attribute("...")
Learn Selenium with JAVA Training in Hyderabad
Read More
Working with Links and Buttons in Selenium
Handling Checkboxes and Radio Buttons in Selenium
How to Verify Page Title and URL with Selenium
Navigating Between Pages Using Selenium WebDriver
Visit Our Quality Thought Institute in Hyderabad
Subscribe by Email
Follow Updates Articles from This Blog via Email
No Comments