Test Overview
This test opens a simple web page, finds a button using a CSS selector, clicks it, and verifies the button's text changes. It shows how using a precise selector ensures the test finds the right element and works reliably.
This test opens a simple web page, finds a button using a CSS selector, clicks it, and verifies the button's text changes. It shows how using a precise selector ensures the test finds the right element and works reliably.
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 TestButtonClick(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com/testpage') def test_button_click_changes_text(self): driver = self.driver # Wait until button is present using a precise CSS selector button = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.CSS_SELECTOR, 'button#submit-btn.primary')) ) button.click() # Verify the button text changed after click changed_text = WebDriverWait(driver, 10).until( EC.text_to_be_present_in_element((By.CSS_SELECTOR, 'button#submit-btn.primary'), 'Clicked') ) self.assertTrue(changed_text) def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser window is open and ready | - | PASS |
| 2 | Browser navigates to https://example.com/testpage | Page loads with a button having id 'submit-btn' and class 'primary' | - | PASS |
| 3 | Waits until button with CSS selector 'button#submit-btn.primary' is present | Button is found on the page | Element located by CSS selector exists | PASS |
| 4 | Clicks the button | Button is clicked, triggering text change | - | PASS |
| 5 | Waits until button text changes to include 'Clicked' | Button text updates to 'Clicked' or similar | Button text contains 'Clicked' | PASS |
| 6 | Test ends and browser closes | Browser window closes | - | PASS |