0
0
Selenium Pythontesting~10 mins

pytest with Selenium setup in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a browser using Selenium with pytest, navigates to a website, checks the page title, and verifies it matches the expected title.

Test Code - pytest with Selenium
Selenium Python
import pytest
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options

@pytest.fixture

def driver():
    options = Options()
    options.add_argument('--headless')
    service = Service()
    driver = webdriver.Chrome(service=service, options=options)
    yield driver
    driver.quit()


def test_page_title(driver):
    driver.get('https://example.com')
    title = driver.title
    assert title == 'Example Domain', f"Expected title to be 'Example Domain' but got '{title}'"
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Pytest starts and sets up the Chrome WebDriver with headless optionChrome browser instance is created but not visible (headless mode)-PASS
2Browser navigates to 'https://example.com'Browser loads the Example Domain webpage-PASS
3Test retrieves the page title using driver.titlePage title is 'Example Domain'Check if page title equals 'Example Domain'PASS
4Assertion verifies the page title matches expectedTitle matches expected stringassert title == 'Example Domain'PASS
5Browser quits and test endsBrowser instance closed-PASS
Failure Scenario
Failing Condition: Page title does not match 'Example Domain' or page fails to load
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test check after opening the browser and navigating to the page?
AThe page title matches 'Example Domain'
BThe page URL contains 'example.com'
CThe page has a button with id 'submit'
DThe page background color is white
Key Result
Using pytest fixtures to set up and tear down Selenium WebDriver ensures clean browser sessions for each test and avoids leftover browser processes.