Test Overview
This test opens a web page, finds a button by its ID, clicks it, and verifies that a message appears. It uses a test class with setup and teardown methods and a test function inside the class.
This test opens a web page, finds a button by its ID, clicks it, and verifies that a message appears. It uses a test class with setup and teardown methods and a test function inside the class.
import unittest 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 class TestButtonClick(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com/button') def tearDown(self): self.driver.quit() def test_button_click_shows_message(self): driver = self.driver button = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, 'show-message-btn')) ) button.click() message = WebDriverWait(driver, 10).until( EC.visibility_of_element_located((By.ID, 'message')) ) self.assertEqual(message.text, 'Button clicked!') if __name__ == '__main__': unittest.main()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts by running unittest main | No browser open yet | - | PASS |
| 2 | setUp method opens Chrome browser and navigates to https://example.com/button | Browser opened at the button test page | - | PASS |
| 3 | Waits up to 10 seconds for button with ID 'show-message-btn' to be present | Button element is found on the page | Button presence verified | PASS |
| 4 | Clicks the button | Button clicked, page reacts by showing a message | - | PASS |
| 5 | Waits up to 10 seconds for message with ID 'message' to be visible | Message element is visible on the page | Message visibility verified | PASS |
| 6 | Asserts that message text equals 'Button clicked!' | Message text is 'Button clicked!' | Text equality assertion | PASS |
| 7 | tearDown method closes the browser | Browser closed | - | PASS |