Test Overview
This test opens a web page, finds a button by its ID, clicks it, and verifies the expected message appears. It shows why locating elements correctly is essential for test success.
This test opens a web page, finds a button by its ID, clicks it, and verifies the expected message appears. It shows why locating elements correctly is essential for test success.
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 TestElementLocation(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com/testpage') def test_click_button_and_check_message(self): driver = self.driver # Wait until button is present button = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, 'submit-btn')) ) button.click() # Wait for message to appear message = WebDriverWait(driver, 10).until( EC.visibility_of_element_located((By.ID, 'result-msg')) ) self.assertEqual(message.text, 'Success!') 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, ready to load the test page | - | PASS |
| 2 | Navigates to 'https://example.com/testpage' | Page loads with a button having ID 'submit-btn' | - | PASS |
| 3 | Waits until element with ID 'submit-btn' is present and finds it | Button element is located in the page DOM | Element with ID 'submit-btn' is found | PASS |
| 4 | Clicks the button with ID 'submit-btn' | Button is clicked, triggering page action | - | PASS |
| 5 | Waits until element with ID 'result-msg' is visible and finds it | Message element appears on the page with text 'Success!' | Element with ID 'result-msg' is visible | PASS |
| 6 | Checks that the message text equals 'Success!' | Message text is 'Success!' | Assert message.text == 'Success!' | PASS |
| 7 | Test ends and browser closes | Browser window is closed | - | PASS |