0
0
Testing Fundamentalstesting~15 mins

When to automate vs manual test in Testing Fundamentals - Automation Approaches Compared

Choose your learning style9 modes available
Decide whether to automate or manually test a login feature
Preconditions (3)
Step 1: Open the login page in a browser
Step 2: Enter valid username 'user@example.com' in the username field
Step 3: Enter valid password 'Password123' in the password field
Step 4: Click the 'Login' button
Step 5: Observe if the user is redirected to the dashboard page
Step 6: Repeat the above steps with invalid credentials to check error message
Step 7: Consider if this test will be repeated often or needs quick feedback
Step 8: Decide if automating this test will save time and reduce human error
✅ Expected Result: User is successfully logged in with valid credentials and sees the dashboard page. With invalid credentials, an error message is shown. Decision made whether to automate based on test repetition and importance.
Automation Requirements - Selenium with Python
Assertions Needed:
Verify URL changes to dashboard page after login
Verify error message is displayed for invalid login
Best Practices:
Use explicit waits to handle page loading
Use clear and maintainable locators (id or name attributes)
Separate test logic from page element locators (Page Object Model)
Include assertions for both success and failure cases
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 LoginTest(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com/login')
        self.wait = WebDriverWait(self.driver, 10)

    def test_valid_login(self):
        driver = self.driver
        wait = self.wait
        driver.find_element(By.ID, 'username').send_keys('user@example.com')
        driver.find_element(By.ID, 'password').send_keys('Password123')
        driver.find_element(By.ID, 'login-button').click()
        wait.until(EC.url_contains('/dashboard'))
        self.assertIn('/dashboard', driver.current_url)

    def test_invalid_login(self):
        driver = self.driver
        wait = self.wait
        driver.get('https://example.com/login')
        driver.find_element(By.ID, 'username').send_keys('wronguser@example.com')
        driver.find_element(By.ID, 'password').send_keys('WrongPass')
        driver.find_element(By.ID, 'login-button').click()
        error = wait.until(EC.visibility_of_element_located((By.ID, 'error-message')))
        self.assertTrue(error.is_displayed())
        self.assertEqual(error.text, 'Invalid username or password.')

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

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

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

In setUp, the browser opens the login page and sets an explicit wait.

The test_valid_login method enters valid credentials, clicks login, waits for the dashboard URL, and asserts the URL changed.

The test_invalid_login method enters wrong credentials, clicks login, waits for an error message, and asserts it is visible and correct.

Explicit waits ensure the test waits for page changes or elements before asserting.

Using IDs for locators keeps selectors simple and reliable.

The tearDown method closes the browser after each test.

This structure follows best practices for maintainability and clear assertions.

Common Mistakes - 4 Pitfalls
Using time.sleep() instead of explicit waits
Using brittle locators like absolute XPath
Not asserting both success and failure cases
Hardcoding test data inside test methods
Bonus Challenge

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

Show Hint