Verify Google Search Page Title
Preconditions (4)
✅ Expected Result: The browser opens Google homepage and the page title is exactly 'Google'. The browser closes after verification.
import pytest from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options @pytest.fixture def driver(): options = Options() options.add_argument('--headless=new') # Run in headless mode for faster tests service = Service() # Assumes chromedriver is in PATH driver = webdriver.Chrome(service=service, options=options) yield driver driver.quit() def test_google_title(driver): driver.get('https://www.google.com') assert driver.title == 'Google', f"Expected title 'Google' but got '{driver.title}'"
This test uses pytest and selenium to automate opening Google homepage and checking its title.
The driver fixture sets up the Chrome WebDriver in headless mode for speed and isolation. It yields the driver to the test and ensures the browser closes after the test finishes.
The test function test_google_title navigates to Google and asserts the page title is exactly 'Google'. If the title is different, the assertion message will show the actual title.
This setup follows best practices by using fixtures for setup/teardown and clear assertions.
Now add data-driven testing to verify titles of three different websites: Google, Bing, and DuckDuckGo.
pytest fixtures in Selenium tests?@pytest.fixture.import pytest
from selenium import webdriver
@pytest.fixture
def browser():
driver = webdriver.Chrome()
yield driver
driver.quit()
def test_google_title(browser):
browser.get('https://www.google.com')
assert 'Google' in browser.titleimport pytest
from selenium import webdriver
@pytest.fixture
def browser():
driver = webdriver.Chrome()
return driver
driver.quit()
def test_example(browser):
browser.get('https://example.com')
assert 'Example' in browser.titlePass the test and this pattern moves unknown