0
0
Testing Fundamentalstesting~15 mins

Regression testing in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Verify that the login functionality works after recent code changes
Preconditions (2)
Step 1: Open the application login page
Step 2: Enter 'testuser' in the username field
Step 3: Enter 'Test@1234' in the password field
Step 4: Click the 'Login' button
Step 5: Observe the page after login
✅ Expected Result: User is successfully logged in and redirected to the dashboard page with URL containing '/dashboard'
Automation Requirements - Selenium with Python
Assertions Needed:
Verify the current URL contains '/dashboard' after login
Verify that the logout button is visible on the dashboard page
Best Practices:
Use explicit waits to wait for elements to be visible or clickable
Use Page Object Model to separate page locators and actions
Use meaningful and stable locators like IDs or data-test attributes
Avoid hardcoded sleeps
Clean up by closing the browser after test
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')

    def enter_username(self, username):
        WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.username_input)).send_keys(username)

    def enter_password(self, password):
        WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.password_input)).send_keys(password)

    def click_login(self):
        WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable(self.login_button)).click()

class DashboardPage:
    def __init__(self, driver):
        self.driver = driver
        self.logout_button = (By.ID, 'logoutBtn')

    def is_logout_visible(self):
        return WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.logout_button)) is not None


def test_login_functionality():
    driver = webdriver.Chrome()
    driver.get('https://example.com/login')

    login_page = LoginPage(driver)
    login_page.enter_username('testuser')
    login_page.enter_password('Test@1234')
    login_page.click_login()

    # Verify URL contains '/dashboard'
    WebDriverWait(driver, 10).until(EC.url_contains('/dashboard'))
    assert '/dashboard' in driver.current_url, f"Expected '/dashboard' in URL but got {driver.current_url}"

    dashboard_page = DashboardPage(driver)
    assert dashboard_page.is_logout_visible(), "Logout button should be visible on dashboard"

    driver.quit()

if __name__ == '__main__':
    test_login_functionality()

This script uses Selenium with Python to automate the regression test for login functionality.

We use the Page Object Model to keep locators and actions organized in LoginPage and DashboardPage classes.

Explicit waits ensure the script waits for elements to appear or be clickable, avoiding flaky tests.

The test opens the login page, enters credentials, clicks login, then verifies the URL contains '/dashboard' and the logout button is visible.

Finally, the browser is closed to clean up.

Common Mistakes - 4 Pitfalls
Using hardcoded sleep like time.sleep(5) instead of explicit waits
Using brittle locators like absolute XPath
Not closing the browser after test
Mixing test logic and page locators in one place
Bonus Challenge

Now add data-driven testing with 3 different sets of valid username and password combinations

Show Hint