0
0
Selenium Pythontesting~10 mins

Test functions and classes in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a web page, finds a button by its ID, clicks it, and verifies that a message appears. It uses a test class with setup and teardown methods and a test function inside the class.

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

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

    def test_button_click_shows_message(self):
        driver = self.driver
        button = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.ID, 'show-message-btn'))
        )
        button.click()
        message = WebDriverWait(driver, 10).until(
            EC.visibility_of_element_located((By.ID, 'message'))
        )
        self.assertEqual(message.text, 'Button clicked!')

if __name__ == '__main__':
    unittest.main()
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test starts by running unittest mainNo browser open yet-PASS
2setUp method opens Chrome browser and navigates to https://example.com/buttonBrowser opened at the button test page-PASS
3Waits up to 10 seconds for button with ID 'show-message-btn' to be presentButton element is found on the pageButton presence verifiedPASS
4Clicks the buttonButton clicked, page reacts by showing a message-PASS
5Waits up to 10 seconds for message with ID 'message' to be visibleMessage element is visible on the pageMessage visibility verifiedPASS
6Asserts that message text equals 'Button clicked!'Message text is 'Button clicked!'Text equality assertionPASS
7tearDown method closes the browserBrowser closed-PASS
Failure Scenario
Failing Condition: Button with ID 'show-message-btn' is not found on the page
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify after clicking the button?
AThat a message with text 'Button clicked!' appears
BThat the button disappears
CThat the page URL changes
DThat the browser closes automatically
Key Result
Using setup and teardown methods in a test class helps prepare and clean up the test environment, making tests organized and reliable.