0
0
Selenium Pythontesting~10 mins

Checking element state (displayed, enabled, selected) in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a web page, finds a checkbox and a button, then checks if the checkbox is displayed, enabled, and selected. It also verifies if the button is enabled. The test confirms these element states to ensure the UI behaves as expected.

Test Code - Selenium
Selenium Python
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 TestElementState(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com/form')

    def test_element_states(self):
        driver = self.driver
        wait = WebDriverWait(driver, 10)

        # Wait for checkbox to be visible
        checkbox = wait.until(EC.visibility_of_element_located((By.ID, 'subscribe')))

        # Check if checkbox is displayed
        self.assertTrue(checkbox.is_displayed(), 'Checkbox should be visible')

        # Check if checkbox is enabled
        self.assertTrue(checkbox.is_enabled(), 'Checkbox should be enabled')

        # Check if checkbox is selected (unchecked by default)
        self.assertFalse(checkbox.is_selected(), 'Checkbox should not be selected initially')

        # Click checkbox to select it
        checkbox.click()

        # Verify checkbox is now selected
        self.assertTrue(checkbox.is_selected(), 'Checkbox should be selected after click')

        # Find submit button
        submit_btn = driver.find_element(By.ID, 'submit-btn')

        # Check if submit button is enabled
        self.assertTrue(submit_btn.is_enabled(), 'Submit button should be enabled')

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()
Execution Trace - 11 Steps
StepActionSystem StateAssertionResult
1Test starts and opens Chrome browserBrowser window opens with blank page-PASS
2Navigates to 'https://example.com/form'Page with form containing checkbox and submit button loads-PASS
3Waits up to 10 seconds for checkbox with ID 'subscribe' to be visibleCheckbox element is visible on the page-PASS
4Checks if checkbox is displayed (visible to user)Checkbox is visible on the pagecheckbox.is_displayed() == TruePASS
5Checks if checkbox is enabled (can be interacted with)Checkbox is enabledcheckbox.is_enabled() == TruePASS
6Checks if checkbox is selected (checked)Checkbox is initially uncheckedcheckbox.is_selected() == FalsePASS
7Clicks the checkbox to select itCheckbox becomes checked-PASS
8Verifies checkbox is now selectedCheckbox is checkedcheckbox.is_selected() == TruePASS
9Finds submit button with ID 'submit-btn'Submit button element is found-PASS
10Checks if submit button is enabledSubmit button is enabledsubmit_btn.is_enabled() == TruePASS
11Test ends and browser closesBrowser window closes-PASS
Failure Scenario
Failing Condition: Checkbox element with ID 'subscribe' is not found or not visible
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test check right after finding the checkbox element?
AIf the checkbox is disabled
BIf the checkbox is clicked
CIf the checkbox is visible on the page
DIf the checkbox is hidden
Key Result
Always verify element states like displayed, enabled, and selected to ensure UI elements behave as expected before interacting with them.