Test Overview
This test checks a login button on a webpage. It uses a retry mechanism to handle flaky failures by trying the test up to 3 times before failing.
This test checks a login button on a webpage. It uses a retry mechanism to handle flaky failures by trying the test up to 3 times before failing.
import unittest from selenium import webdriver from selenium.webdriver.common.by import By from selenium.common.exceptions import NoSuchElementException import time class TestLoginButton(unittest.TestCase): MAX_RETRIES = 3 def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com/login') def test_login_button_present(self): retries = 0 while retries < self.MAX_RETRIES: try: button = self.driver.find_element(By.ID, 'login-btn') self.assertTrue(button.is_displayed()) break # Test passed, exit retry loop except (NoSuchElementException, AssertionError): retries += 1 if retries == self.MAX_RETRIES: raise time.sleep(1) # Wait before retrying def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and WebDriver opens Chrome browser | Browser window opens at https://example.com/login | - | PASS |
| 2 | Test tries to find element with ID 'login-btn' | Page loaded, looking for login button | - | FAIL |
| 3 | Test catches NoSuchElementException, waits 1 second, retries (retry 1) | Still on login page, retrying element search | - | PASS |
| 4 | Test tries to find element with ID 'login-btn' again | Page loaded, looking for login button | - | PASS |
| 5 | Test asserts the login button is displayed | Login button found and visible | button.is_displayed() is True | PASS |
| 6 | Test passes and browser closes | Browser closed | - | PASS |