0
0
Selenium Pythontesting~15 mins

Conftest for shared fixtures in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Automate login test using shared fixture from conftest.py
Preconditions (3)
Step 1: Open the browser using the shared fixture from conftest.py
Step 2: Navigate to 'https://example.com/login'
Step 3: Enter 'testuser' in the username input field with id 'username'
Step 4: Enter 'Test@1234' in the password input field with id 'password'
Step 5: Click the login button with id 'loginBtn'
Step 6: Wait until the URL changes to 'https://example.com/dashboard'
Step 7: Verify that the page title contains 'Dashboard'
✅ Expected Result: User is successfully logged in and redirected to the dashboard page with correct title
Automation Requirements - pytest with Selenium
Assertions Needed:
Assert current URL is 'https://example.com/dashboard'
Assert page title contains 'Dashboard'
Best Practices:
Use conftest.py to define a shared fixture for WebDriver setup and teardown
Use explicit waits to wait for URL change or page elements
Use pytest assertions for validation
Keep test code clean and readable
Automated Solution
Selenium Python
import pytest
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

# conftest.py
@pytest.fixture(scope='session')
def driver():
    driver = webdriver.Chrome()
    driver.maximize_window()
    yield driver
    driver.quit()

# test_login.py

def test_login(driver):
    driver.get('https://example.com/login')
    username_input = WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.ID, 'username'))
    )
    username_input.send_keys('testuser')
    password_input = driver.find_element(By.ID, 'password')
    password_input.send_keys('Test@1234')
    login_button = driver.find_element(By.ID, 'loginBtn')
    login_button.click()

    WebDriverWait(driver, 10).until(
        EC.url_to_be('https://example.com/dashboard')
    )

    assert driver.current_url == 'https://example.com/dashboard', 'URL did not change to dashboard'
    assert 'Dashboard' in driver.title, 'Page title does not contain Dashboard'

The conftest.py file defines a driver fixture that sets up the Chrome WebDriver once per test session and quits it after all tests finish. This avoids repeating setup code in every test.

The test test_login uses this shared driver fixture. It opens the login page, waits explicitly for the username input to be visible, then enters the username and password. It clicks the login button and waits explicitly until the URL changes to the dashboard URL.

Finally, it asserts that the current URL is exactly the dashboard URL and that the page title contains the word 'Dashboard'. These assertions confirm the login was successful.

Explicit waits ensure the test does not fail due to timing issues. Using the shared fixture keeps the code clean and reusable.

Common Mistakes - 4 Pitfalls
Not using a shared fixture and creating WebDriver instance inside each test
Using implicit waits or time.sleep instead of explicit waits
Hardcoding URLs or credentials inside the test without flexibility
Using incorrect locator strategies or invalid selectors
Bonus Challenge

Now add data-driven testing with 3 different sets of login credentials (valid and invalid) using pytest parametrize

Show Hint