Test Overview
This test opens a web page, finds a button element by its ID, retrieves its 'class' attribute, and verifies that the attribute value matches the expected class name.
This test opens a web page, finds a button element by its ID, retrieves its 'class' attribute, and verifies that the attribute value matches the expected class name.
from selenium import webdriver from selenium.webdriver.common.by import By import unittest class TestGetElementAttribute(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com') def test_button_class_attribute(self): button = self.driver.find_element(By.ID, 'submit-btn') class_attr = button.get_attribute('class') self.assertEqual(class_attr, 'btn primary') def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Chrome browser window is open and ready | - | PASS |
| 2 | Browser navigates to 'https://example.com' | Page 'https://example.com' is loaded in the browser | - | PASS |
| 3 | Find element with ID 'submit-btn' using driver.find_element(By.ID, 'submit-btn') | Button element with ID 'submit-btn' is located on the page | - | PASS |
| 4 | Get 'class' attribute of the button element using get_attribute('class') | Retrieved attribute value is 'btn primary' | Check if attribute value equals 'btn primary' | PASS |
| 5 | Assert that the class attribute equals 'btn primary' using self.assertEqual | Assertion compares actual and expected attribute values | class_attr == 'btn primary' | PASS |
| 6 | Close the browser and end the test | Browser window is closed | - | PASS |