0
0
Testing Fundamentalstesting~15 mins

Tester mindset and thinking in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Verify login functionality with valid and invalid inputs
Preconditions (1)
Step 1: Enter a valid email 'user@example.com' in the email field
Step 2: Enter a valid password 'Password123!' in the password field
Step 3: Click the Login button
Step 4: Verify that the user is redirected to the dashboard page
Step 5: Logout to return to the login page
Step 6: Enter an invalid email 'invalidemail' in the email field
Step 7: Enter a password 'Password123!' in the password field
Step 8: Click the Login button
Step 9: Verify that an error message 'Please enter a valid email address' is displayed
Step 10: Enter a valid email 'user@example.com' in the email field
Step 11: Leave the password field empty
Step 12: Click the Login button
Step 13: Verify that an error message 'Password cannot be empty' is displayed
✅ Expected Result: The login succeeds with valid credentials and redirects to dashboard. Appropriate error messages appear for invalid email and empty password.
Automation Requirements - Selenium with Python
Assertions Needed:
Verify current URL is dashboard URL after successful login
Verify error message text for invalid email
Verify error message text for empty password
Best Practices:
Use explicit waits to wait for elements to be visible or clickable
Use descriptive and stable locators (id or name attributes preferred)
Separate test logic and page locators using Page Object Model
Use assertions with clear messages
Clean up by logging out after successful login
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
import unittest

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')
        self.error_message = (By.ID, 'errorMsg')

    def enter_email(self, email):
        WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.email_input)).clear()
        self.driver.find_element(*self.email_input).send_keys(email)

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

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

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

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

    def click_logout(self):
        WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable(self.logout_button)).click()

class TestLogin(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com/login')
        self.login_page = LoginPage(self.driver)
        self.dashboard_page = DashboardPage(self.driver)

    def tearDown(self):
        self.driver.quit()

    def test_login_valid_and_invalid(self):
        # Valid login
        self.login_page.enter_email('user@example.com')
        self.login_page.enter_password('Password123!')
        self.login_page.click_login()
        WebDriverWait(self.driver, 10).until(EC.url_contains('/dashboard'))
        self.assertIn('/dashboard', self.driver.current_url, 'Failed to navigate to dashboard after login')

        # Logout
        self.dashboard_page.click_logout()
        WebDriverWait(self.driver, 10).until(EC.url_contains('/login'))

        # Invalid email
        self.login_page.enter_email('invalidemail')
        self.login_page.enter_password('Password123!')
        self.login_page.click_login()
        error_text = self.login_page.get_error_message()
        self.assertEqual(error_text, 'Please enter a valid email address', 'Incorrect error message for invalid email')

        # Empty password
        self.login_page.enter_email('user@example.com')
        self.login_page.enter_password('')
        self.login_page.click_login()
        error_text = self.login_page.get_error_message()
        self.assertEqual(error_text, 'Password cannot be empty', 'Incorrect error message for empty password')

if __name__ == '__main__':
    unittest.main()

The code uses Selenium WebDriver with Python's unittest framework to automate the login test case.

We define Page Object Model classes LoginPage and DashboardPage to keep locators and actions organized.

Explicit waits ensure elements are ready before interacting, avoiding flaky tests.

The test method test_login_valid_and_invalid covers all manual test steps: valid login, logout, invalid email, and empty password cases.

Assertions check the URL after login and verify error messages for invalid inputs with clear messages.

Setup and teardown methods open and close the browser cleanly for each test run.

Common Mistakes - 4 Pitfalls
Using hardcoded sleep (time.sleep) instead of explicit waits
Using brittle XPath locators that break easily
Mixing test logic and locators in the test method
Not cleaning up after tests (e.g., not logging out)
Bonus Challenge

Now add data-driven testing with 3 different sets of email and password inputs (valid, invalid email, empty password).

Show Hint