0
0
Testing Fundamentalstesting~15 mins

Pair testing in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Pair Testing on Login Functionality
Preconditions (3)
Step 1: Tester A enters 'user@example.com' in the email field
Step 2: Tester A enters 'Password123' in the password field
Step 3: Tester A clicks the Login button
Step 4: Tester B observes the behavior and notes any issues
Step 5: Both testers discuss and verify if the login was successful
Step 6: Repeat the above steps for invalid email format 'userexample.com' and verify error message
Step 7: Repeat the above steps for empty password field and verify error message
Step 8: Repeat the above steps for correct email and wrong password and verify error message
✅ Expected Result: Login succeeds only with valid credentials; appropriate error messages appear for invalid inputs; both testers agree on the test outcomes
Automation Requirements - Selenium with Python
Assertions Needed:
Verify URL changes to dashboard after successful login
Verify error message is displayed for invalid email format
Verify error message is displayed for empty password
Verify error message is displayed for wrong password
Best Practices:
Use explicit waits to handle page loading
Use clear and maintainable locators (By.ID, By.NAME)
Separate test data from test logic
Use assertions to validate expected outcomes
Write clean and readable code with comments
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 TestLogin(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com/login')
        self.wait = WebDriverWait(self.driver, 10)

    def login(self, email, password):
        email_field = self.wait.until(EC.presence_of_element_located((By.ID, 'email')))
        password_field = self.driver.find_element(By.ID, 'password')
        login_button = self.driver.find_element(By.ID, 'loginBtn')

        email_field.clear()
        email_field.send_keys(email)
        password_field.clear()
        password_field.send_keys(password)
        login_button.click()

    def test_valid_login(self):
        self.login('user@example.com', 'Password123')
        self.wait.until(EC.url_contains('/dashboard'))
        self.assertIn('/dashboard', self.driver.current_url)

    def test_invalid_email_format(self):
        self.login('userexample.com', 'Password123')
        error = self.wait.until(EC.visibility_of_element_located((By.ID, 'emailError')))
        self.assertEqual(error.text, 'Please enter a valid email address.')

    def test_empty_password(self):
        self.login('user@example.com', '')
        error = self.wait.until(EC.visibility_of_element_located((By.ID, 'passwordError')))
        self.assertEqual(error.text, 'Password cannot be empty.')

    def test_wrong_password(self):
        self.login('user@example.com', 'WrongPass')
        error = self.wait.until(EC.visibility_of_element_located((By.ID, 'loginError')))
        self.assertEqual(error.text, 'Incorrect email or password.')

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

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

This test script uses Selenium with Python's unittest framework to automate the login tests.

setUp: Opens the browser and navigates to the login page before each test.

login method: Enters email and password, then clicks login.

test_valid_login: Checks that after valid login, URL contains '/dashboard'.

test_invalid_email_format: Checks error message for invalid email format.

test_empty_password: Checks error message when password is empty.

test_wrong_password: Checks error message for wrong password.

tearDown: Closes the browser after each test.

Explicit waits ensure elements are ready before interaction. Assertions verify expected results. Locators use IDs for clarity and maintainability.

Common Mistakes - 4 Pitfalls
Using time.sleep() instead of explicit waits
Hardcoding test data inside test methods
Using brittle locators like absolute XPath
Not closing the browser after tests
Bonus Challenge

Now add data-driven testing with 3 different sets of login credentials (valid, invalid email, wrong password).

Show Hint