0
0
Selenium Pythontesting~10 mins

Fixtures for browser setup/teardown in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test uses a fixture to open a browser before the test and close it after. It verifies that the page title is correct after navigating to the website.

Test Code - pytest
Selenium Python
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By

@pytest.fixture
def browser():
    driver = webdriver.Chrome()
    yield driver
    driver.quit()

def test_page_title(browser):
    browser.get('https://example.com')
    assert browser.title == 'Example Domain'
Execution Trace - 4 Steps
StepActionSystem StateAssertionResult
1Pytest fixture 'browser' starts: opens Chrome browserChrome browser window opens, ready for commands-PASS
2Test navigates browser to 'https://example.com'Browser loads the Example Domain webpage-PASS
3Test checks that page title equals 'Example Domain'Page title is 'Example Domain'assert browser.title == 'Example Domain'PASS
4Test ends, fixture 'browser' teardown runs: closes browserChrome browser window closes-PASS
Failure Scenario
Failing Condition: Page title does not match 'Example Domain' after navigation
Execution Trace Quiz - 3 Questions
Test your understanding
What does the fixture 'browser' do in this test?
AIt runs the test code inside the browser
BIt opens the browser before the test and closes it after
CIt only opens the browser but does not close it
DIt checks the page title automatically
Key Result
Using fixtures for browser setup and teardown ensures tests start with a fresh browser and clean up resources, making tests reliable and preventing leftover browser instances.