Test Overview
This test checks if a web page's main button is accessible by keyboard and has the correct ARIA label. It verifies that users who rely on keyboard navigation and screen readers can use the button properly.
This test checks if a web page's main button is accessible by keyboard and has the correct ARIA label. It verifies that users who rely on keyboard navigation and screen readers can use the button properly.
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import unittest class AccessibilityTest(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com') def test_button_accessibility(self): driver = self.driver wait = WebDriverWait(driver, 10) # Wait for the button to be present button = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'button#submit-btn'))) # Check ARIA label aria_label = button.get_attribute('aria-label') self.assertEqual(aria_label, 'Submit form', 'ARIA label is incorrect') # Check keyboard focus by sending TAB keys body = driver.find_element(By.TAG_NAME, 'body') body.send_keys(Keys.TAB) # Focus first element focused = driver.switch_to.active_element self.assertEqual(focused, button, 'Button is not focusable by keyboard') 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 | Navigates to https://example.com | Page loads with a button having id 'submit-btn' | - | PASS |
| 3 | Waits for button with CSS selector 'button#submit-btn' to be present | Button is present in the DOM | Button presence confirmed | PASS |
| 4 | Retrieves 'aria-label' attribute of the button | Button has aria-label='Submit form' | aria-label equals 'Submit form' | PASS |
| 5 | Sends TAB key to body to focus first focusable element | Keyboard focus moves to the button | Focused element is the button with id 'submit-btn' | PASS |
| 6 | Test ends and browser closes | Browser window closed | - | PASS |