0
0
Selenium Pythontesting~15 mins

Headless mode for CI in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Verify login functionality in headless mode for CI
Preconditions (2)
Step 1: Open the browser in headless mode
Step 2: Navigate to 'https://example.com/login'
Step 3: Enter 'testuser' in the username field with id 'username'
Step 4: Enter 'Test@1234' in the password field with id 'password'
Step 5: Click the login button with id 'loginBtn'
Step 6: Wait until the dashboard page loads and URL changes to 'https://example.com/dashboard'
Step 7: Verify that the element with id 'welcomeMessage' contains text 'Welcome, testuser!'
✅ Expected Result: User is successfully logged in and dashboard page is displayed with welcome message confirming login
Automation Requirements - Selenium with Python
Assertions Needed:
Verify current URL is 'https://example.com/dashboard'
Verify welcome message text is 'Welcome, testuser!'
Best Practices:
Use explicit waits to wait for elements or URL changes
Use headless browser mode for CI environment
Use proper locators by id
Structure code for readability and maintainability
Automated Solution
Selenium Python
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

def test_login_headless():
    options = Options()
    options.headless = True

    driver = webdriver.Chrome(options=options)
    wait = WebDriverWait(driver, 10)

    try:
        driver.get('https://example.com/login')

        username_input = wait.until(EC.presence_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()

        wait.until(EC.url_to_be('https://example.com/dashboard'))

        welcome_message = wait.until(EC.presence_of_element_located((By.ID, 'welcomeMessage')))
        assert welcome_message.text == 'Welcome, testuser!'

    finally:
        driver.quit()

if __name__ == '__main__':
    test_login_headless()

This script uses Selenium WebDriver with Chrome in headless mode to simulate a browser without opening a window, ideal for CI environments.

We set options.headless = True to enable headless mode.

Explicit waits ensure the script waits for elements to appear or URL to change before interacting or asserting, avoiding flaky tests.

We locate elements by their id attributes, which is a best practice for stable selectors.

The test inputs the username and password, clicks login, waits for the dashboard URL, then verifies the welcome message text.

Finally, the browser is closed with driver.quit() to free resources.

Common Mistakes - 4 Pitfalls
Not using headless mode in CI environment
Using implicit waits or no waits at all
Using brittle locators like absolute XPath
Not closing the browser after test
Bonus Challenge

Now add data-driven testing with 3 different username and password combinations to verify login success or failure.

Show Hint