Test Overview
This test checks that synchronization using explicit waits prevents flaky failures by waiting for a button to be clickable before clicking it. It verifies the button click leads to the expected page change.
This test checks that synchronization using explicit waits prevents flaky failures by waiting for a button to be clickable before clicking it. It verifies the button click leads to the expected page change.
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 TestSynchronization(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com/delayed_button') def test_click_button_with_wait(self): driver = self.driver wait = WebDriverWait(driver, 10) # Wait until the button is clickable button = wait.until(EC.element_to_be_clickable((By.ID, 'delayed-btn'))) button.click() # Verify the page shows success message success_msg = wait.until(EC.visibility_of_element_located((By.ID, 'success-msg'))) self.assertEqual(success_msg.text, 'Button clicked successfully!') 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 opened at URL https://example.com/delayed_button | - | PASS |
| 2 | WebDriverWait waits up to 10 seconds for button with ID 'delayed-btn' to be clickable | Page is loading, button appears after delay | Button is clickable | PASS |
| 3 | Clicks the button with ID 'delayed-btn' | Button clicked, page triggers success message display | - | PASS |
| 4 | WebDriverWait waits up to 10 seconds for element with ID 'success-msg' to be visible | Success message appears on page | Success message text equals 'Button clicked successfully!' | PASS |
| 5 | Test ends and browser closes | Browser window closed | - | PASS |