0
0
Selenium Pythontesting~10 mins

Custom expected conditions in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test uses a custom expected condition to wait until a button's text changes to "Ready" before clicking it. It verifies that the button is clickable only after the text update.

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

class button_text_to_be:
    def __init__(self, locator, text):
        self.locator = locator
        self.text = text
    def __call__(self, driver):
        element = driver.find_element(*self.locator)
        if element.text == self.text:
            return element
        else:
            return False

def test_button_text_change_and_click():
    driver = webdriver.Chrome()
    driver.get("https://example.com/buttonpage")

    wait = WebDriverWait(driver, 10)
    button_locator = (By.ID, "start-button")

    # Wait until button text changes to 'Ready'
    button = wait.until(button_text_to_be(button_locator, "Ready"))

    button.click()

    # Verify some result after click
    result_element = driver.find_element(By.ID, "result")
    assert result_element.text == "Success"

    driver.quit()
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser window is open, ready to navigate-PASS
2Navigates to https://example.com/buttonpagePage with a button having ID 'start-button' is loaded-PASS
3Waits until button text changes to 'Ready' using custom expected conditionButton initially shows 'Start', then changes to 'Ready' within 10 secondsChecks if button text equals 'Ready'PASS
4Clicks the button after text is 'Ready'Button is clickable and clicked-PASS
5Finds element with ID 'result' to verify outcomeResult element is present on pageAsserts that result element text is 'Success'PASS
6Test ends and browser closesBrowser window is closed-PASS
Failure Scenario
Failing Condition: Button text does not change to 'Ready' within 10 seconds
Execution Trace Quiz - 3 Questions
Test your understanding
What does the custom expected condition 'button_text_to_be' check?
AIf the button's text matches the expected text
BIf the button is visible on the page
CIf the button is enabled for clicking
DIf the button's color changes
Key Result
Creating custom expected conditions helps wait for specific states or changes and return useful objects like the element itself (unlike built-in text_to_be which returns True), making tests more reliable and clear.