0
0
Testing Fundamentalstesting~15 mins

Test automation pyramid in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Verify login functionality with valid credentials
Preconditions (2)
Step 1: Open the login page URL
Step 2: Enter 'user@example.com' in the email input field
Step 3: Enter 'Password123!' in the password input field
Step 4: Click the login button
Step 5: Wait for the dashboard page to load
✅ Expected Result: User is redirected to the dashboard page with URL 'https://example.com/dashboard' and a welcome message is displayed
Automation Requirements - Selenium WebDriver with Python
Assertions Needed:
Verify current URL is 'https://example.com/dashboard'
Verify welcome message element is visible and contains text 'Welcome'
Best Practices:
Use explicit waits to wait for elements
Use Page Object Model to separate page interactions
Use clear and maintainable locators (id or name preferred)
Avoid hardcoded sleeps
Keep assertions focused and meaningful
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.email_input = (By.ID, 'email')
        self.password_input = (By.ID, 'password')
        self.login_button = (By.ID, 'loginBtn')

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

    def login(self, email, password):
        WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.email_input)).send_keys(email)
        self.driver.find_element(*self.password_input).send_keys(password)
        self.driver.find_element(*self.login_button).click()

class DashboardPage:
    def __init__(self, driver):
        self.driver = driver
        self.welcome_message = (By.ID, 'welcomeMsg')

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

    def welcome_text(self):
        return WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.welcome_message)).text


def test_login():
    driver = webdriver.Chrome()
    try:
        login_page = LoginPage(driver)
        dashboard_page = DashboardPage(driver)

        login_page.open()
        login_page.login('user@example.com', 'Password123!')

        assert dashboard_page.is_loaded(), 'Dashboard page did not load as expected'
        welcome = dashboard_page.welcome_text()
        assert 'Welcome' in welcome, f"Expected 'Welcome' in message but got '{welcome}'"

        print('Test passed: Login functionality works as expected.')
    finally:
        driver.quit()

if __name__ == '__main__':
    test_login()

This script uses Selenium WebDriver with Python to automate the login test case.

We define two page classes: LoginPage and DashboardPage to separate page interactions, following the Page Object Model.

The LoginPage class has methods to open the login page and perform login by entering email and password and clicking the login button. We use explicit waits to wait until the email input is visible before typing.

The DashboardPage class checks if the dashboard URL is loaded and retrieves the welcome message text, again using explicit waits.

The test_login function runs the test: it opens the login page, performs login, then asserts the dashboard page loaded and the welcome message contains 'Welcome'.

Finally, the driver quits to close the browser.

This approach avoids hardcoded sleeps, uses maintainable locators by ID, and keeps assertions clear and focused.

Common Mistakes - 3 Pitfalls
Using time.sleep() instead of explicit waits
Using brittle XPath locators that rely on full element paths
Mixing page actions and assertions in the test function without page objects
Bonus Challenge

Now add data-driven testing with 3 different sets of valid credentials to verify login works for multiple users.

Show Hint