We use pytest with Selenium to write and run browser tests easily. It helps check if websites work correctly.
0
0
pytest with Selenium setup in Selenium Python
Introduction
You want to test if a website's buttons and links work.
You need to check a website on different browsers automatically.
You want to run many tests quickly without opening browsers manually.
You want clear reports showing which tests passed or failed.
Syntax
Selenium Python
import pytest from selenium import webdriver @pytest.fixture def browser(): driver = webdriver.Chrome() yield driver driver.quit() def test_example(browser): browser.get('https://example.com') assert 'Example Domain' in browser.title
Use @pytest.fixture to set up and clean the browser before and after tests.
The yield keyword pauses the fixture to run the test, then continues to close the browser.
Examples
This example uses Firefox browser instead of Chrome.
Selenium Python
import pytest from selenium import webdriver @pytest.fixture def browser(): driver = webdriver.Firefox() yield driver driver.quit()
A simple test that opens Google and checks the page title.
Selenium Python
def test_google_title(browser): browser.get('https://google.com') assert 'Google' in browser.title
Sample Program
This test opens example.com and checks if the page title contains 'Example Domain'. The browser opens, runs the test, then closes.
Selenium Python
import pytest from selenium import webdriver @pytest.fixture def browser(): driver = webdriver.Chrome() yield driver driver.quit() def test_example_domain_title(browser): browser.get('https://example.com') assert 'Example Domain' in browser.title
OutputSuccess
Important Notes
Make sure you have the correct WebDriver installed (like chromedriver for Chrome).
Keep your WebDriver version compatible with your browser version.
Use pytest command in terminal to run tests.
Summary
pytest with Selenium helps automate browser tests easily.
Use fixtures to open and close browsers cleanly.
Write simple tests to check website titles or elements.