0
0
Selenium Pythontesting~15 mins

Test reporting in CI in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Verify login functionality and generate test report in CI
Preconditions (3)
Step 1: Open the login page at 'https://example.com/login'
Step 2: Enter 'testuser' in the username input field with id 'username'
Step 3: Enter 'Test@1234' in the password input field with id 'password'
Step 4: Click the login button with id 'loginBtn'
Step 5: Wait until the URL changes to 'https://example.com/dashboard'
Step 6: Verify that the page contains a welcome message with id 'welcomeMsg' and text 'Welcome, testuser!'
✅ Expected Result: User is successfully logged in, redirected to dashboard, and the welcome message is displayed. Test report is generated in JUnit XML format for CI consumption.
Automation Requirements - pytest with Selenium WebDriver
Assertions Needed:
Assert current URL is 'https://example.com/dashboard'
Assert welcome message text equals 'Welcome, testuser!'
Best Practices:
Use explicit waits to wait for page elements or URL changes
Use pytest fixtures for setup and teardown of WebDriver
Generate JUnit XML test report using pytest's --junitxml option
Keep locators clear and maintainable using By.ID
Avoid hardcoded sleeps; use WebDriverWait
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

@ pytest.fixture
 def driver():
    driver = webdriver.Chrome()
    driver.implicitly_wait(5)  # short implicit wait
    yield driver
    driver.quit()

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

    username_input = driver.find_element(By.ID, 'username')
    password_input = driver.find_element(By.ID, 'password')
    login_button = driver.find_element(By.ID, 'loginBtn')

    username_input.send_keys('testuser')
    password_input.send_keys('Test@1234')
    login_button.click()

    # Wait until URL changes to dashboard
    WebDriverWait(driver, 10).until(EC.url_to_be('https://example.com/dashboard'))

    assert driver.current_url == 'https://example.com/dashboard', f"Expected URL to be dashboard but got {driver.current_url}"

    # Wait for welcome message
    welcome_msg = WebDriverWait(driver, 10).until(
        EC.visibility_of_element_located((By.ID, 'welcomeMsg'))
    )

    assert welcome_msg.text == 'Welcome, testuser!', f"Expected welcome message text to be 'Welcome, testuser!' but got '{welcome_msg.text}'"

# To run and generate JUnit XML report for CI:
# pytest test_login.py --junitxml=report.xml

This test script uses pytest and selenium to automate the login test.

The driver fixture sets up and tears down the Chrome WebDriver cleanly.

The test opens the login page, enters the username and password, and clicks the login button.

It uses WebDriverWait to wait explicitly for the URL to change to the dashboard URL, avoiding unreliable fixed waits.

Then it waits for the welcome message element to appear and asserts its text matches the expected greeting.

Assertions verify the URL and welcome message text to confirm successful login.

For CI integration, running pytest with --junitxml=report.xml generates a test report in XML format that CI tools can read.

This approach follows best practices: explicit waits, clear locators, fixture usage, and proper reporting.

Common Mistakes - 4 Pitfalls
Using time.sleep() instead of explicit waits
Hardcoding XPath locators that are brittle
Not quitting the WebDriver after tests
Not generating test reports for CI
Bonus Challenge

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

Show Hint