0
0
Testing Fundamentalstesting~15 mins

Error guessing in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Error guessing test for login form
Preconditions (1)
Step 1: Enter an invalid email format '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 an error message 'Invalid email format' is displayed
Step 5: Enter a valid email 'user@example.com' in the email field
Step 6: Leave the password field empty
Step 7: Click the Login button
Step 8: Verify that an error message 'Password cannot be empty' is displayed
Step 9: Enter a valid email 'user@example.com' in the email field
Step 10: Enter a password with less than 6 characters '123' in the password field
Step 11: Click the Login button
Step 12: Verify that an error message 'Password too short' is displayed
✅ Expected Result: The application shows appropriate error messages for each invalid input scenario.
Automation Requirements - Selenium with Python
Assertions Needed:
Verify error message text matches expected for invalid email
Verify error message text matches expected for empty password
Verify error message text matches expected for short password
Best Practices:
Use explicit waits to wait for error messages to appear
Use descriptive locators like By.ID or By.CSS_SELECTOR
Use Page Object Model to separate page actions and test logic
Avoid hardcoded sleeps
Use clear 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
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, 'login-btn')
        self.error_message = (By.ID, 'error-msg')

    def enter_email(self, email):
        self.driver.find_element(*self.email_input).clear()
        self.driver.find_element(*self.email_input).send_keys(email)

    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

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

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

    def test_invalid_email_format(self):
        self.login_page.enter_email('user@@example.com')
        self.login_page.enter_password('Password123!')
        self.login_page.click_login()
        error = self.login_page.get_error_message()
        self.assertEqual(error, 'Invalid email format', 'Error message for invalid email is incorrect')

    def test_empty_password(self):
        self.login_page.enter_email('user@example.com')
        self.login_page.enter_password('')
        self.login_page.click_login()
        error = self.login_page.get_error_message()
        self.assertEqual(error, 'Password cannot be empty', 'Error message for empty password is incorrect')

    def test_short_password(self):
        self.login_page.enter_email('user@example.com')
        self.login_page.enter_password('123')
        self.login_page.click_login()
        error = self.login_page.get_error_message()
        self.assertEqual(error, 'Password too short', 'Error message for short password is incorrect')

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

This test script uses Selenium WebDriver with Python's unittest framework.

The LoginPage class follows the Page Object Model. It stores locators and methods to interact with the login page elements. This keeps the test code clean and easy to maintain.

Each test method performs one error guessing scenario by entering invalid inputs and clicking the login button.

Explicit waits are used to wait for the error message to appear before reading its text. This avoids flaky tests.

Assertions check that the error messages shown match the expected text for each invalid input.

The setup and teardown methods open and close the browser for each test, ensuring a fresh start.

This approach follows best practices like using descriptive locators, explicit waits, and clear assertion messages.

Common Mistakes - 4 Pitfalls
Using time.sleep() instead of explicit waits
Using brittle XPath locators that break easily
Not clearing input fields before typing
Combining multiple test scenarios in one test method
Bonus Challenge

Now add data-driven testing with 3 different invalid input sets for email and password

Show Hint