0
0
Testing Fundamentalstesting~15 mins

Root cause analysis in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Perform root cause analysis on a failed login test
Preconditions (2)
Step 1: Open the login page
Step 2: Enter username 'testuser' in the username field
Step 3: Enter password 'wrongpassword' in the password field
Step 4: Click the login button
Step 5: Observe the error message displayed
Step 6: Check the application logs for error details
Step 7: Identify the root cause of the login failure
✅ Expected Result: Login fails with an error message 'Invalid credentials'. Logs show authentication failure due to incorrect password.
Automation Requirements - Python Selenium
Assertions Needed:
Verify error message text is 'Invalid credentials'
Verify login button is disabled after failed attempt
Verify URL remains on login page
Best Practices:
Use explicit waits to wait for error message visibility
Use Page Object Model to separate page elements
Use clear and descriptive assertion messages
Automated Solution
Testing Fundamentals
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

class LoginPage:
    def __init__(self, driver):
        self.driver = driver
        self.username_input = (By.ID, 'username')
        self.password_input = (By.ID, 'password')
        self.login_button = (By.ID, 'loginBtn')
        self.error_message = (By.ID, 'errorMsg')

    def open(self, url):
        self.driver.get(url)

    def enter_username(self, username):
        self.driver.find_element(*self.username_input).clear()
        self.driver.find_element(*self.username_input).send_keys(username)

    def enter_password(self, password):
        self.driver.find_element(*self.password_input).clear()
        self.driver.find_element(*self.password_input).send_keys(password)

    def click_login(self):
        self.driver.find_element(*self.login_button).click()

    def get_error_message(self):
        wait = WebDriverWait(self.driver, 10)
        error_element = wait.until(EC.visibility_of_element_located(self.error_message))
        return error_element.text

    def is_login_button_enabled(self):
        return self.driver.find_element(*self.login_button).is_enabled()

    def current_url(self):
        return self.driver.current_url


def test_failed_login_root_cause_analysis():
    driver = webdriver.Chrome()
    login_page = LoginPage(driver)
    try:
        login_page.open('https://example.com/login')
        login_page.enter_username('testuser')
        login_page.enter_password('wrongpassword')
        login_page.click_login()

        error_text = login_page.get_error_message()
        assert error_text == 'Invalid credentials', f"Expected error message 'Invalid credentials' but got '{error_text}'"

        login_button_enabled = login_page.is_login_button_enabled()
        assert not login_button_enabled, "Login button should be disabled after failed login attempt"

        current_url = login_page.current_url()
        assert 'login' in current_url, f"Expected to remain on login page but URL is {current_url}"

    finally:
        driver.quit()

This script uses Selenium with Python to automate the root cause analysis of a failed login.

The LoginPage class follows the Page Object Model to keep selectors and actions organized.

Explicit waits ensure the error message is visible before reading it, avoiding timing issues.

Assertions check that the error message is correct, the login button is disabled, and the URL remains on the login page, confirming the failure behavior.

The try-finally block ensures the browser closes even if assertions fail.

Common Mistakes - 3 Pitfalls
Using time.sleep() instead of explicit waits
Hardcoding XPath selectors that are brittle
Not closing the browser after test
Bonus Challenge

Now add data-driven testing with 3 different username and password combinations to verify error messages.

Show Hint