0
0
Testing Fundamentalstesting~15 mins

Transitioning to automation in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Automate login functionality test
Preconditions (3)
Step 1: Open the browser and navigate to 'https://example.com/login'
Step 2: Locate the username input field and enter 'testuser'
Step 3: Locate the password input field and enter 'Test@1234'
Step 4: Locate and click the login button
Step 5: Wait for the dashboard page to load
Step 6: Verify that the URL is 'https://example.com/dashboard'
Step 7: Verify that the page contains a welcome message with text 'Welcome, testuser!'
✅ Expected Result: User is successfully logged in and redirected to the dashboard page with a welcome message displayed.
Automation Requirements - Selenium with Python
Assertions Needed:
Verify current URL is 'https://example.com/dashboard'
Verify welcome message text is 'Welcome, testuser!'
Best Practices:
Use explicit waits to wait for elements to be visible or clickable
Use descriptive and maintainable locators (e.g., By.ID or By.CSS_SELECTOR)
Organize code with setup and teardown methods
Use assertions from unittest or pytest frameworks
Avoid hardcoded sleeps
Automated Solution
Testing Fundamentals
import unittest
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 TestLoginAutomation(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.wait = WebDriverWait(self.driver, 10)

    def test_login(self):
        driver = self.driver
        wait = self.wait

        # Step 1: Open login page
        driver.get('https://example.com/login')

        # Step 2: Enter username
        username_field = wait.until(EC.visibility_of_element_located((By.ID, 'username')))
        username_field.clear()
        username_field.send_keys('testuser')

        # Step 3: Enter password
        password_field = wait.until(EC.visibility_of_element_located((By.ID, 'password')))
        password_field.clear()
        password_field.send_keys('Test@1234')

        # Step 4: Click login button
        login_button = wait.until(EC.element_to_be_clickable((By.ID, 'loginBtn')))
        login_button.click()

        # Step 5 & 6: Wait for dashboard URL
        wait.until(EC.url_to_be('https://example.com/dashboard'))
        self.assertEqual(driver.current_url, 'https://example.com/dashboard')

        # Step 7: Verify welcome message
        welcome_message = wait.until(EC.visibility_of_element_located((By.ID, 'welcomeMsg')))
        self.assertEqual(welcome_message.text, 'Welcome, testuser!')

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

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

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

setUp() initializes the Chrome browser and sets an explicit wait of 10 seconds.

The test_login method automates the manual test steps precisely:

  • It opens the login page URL.
  • Waits for username and password fields to be visible, then enters the credentials.
  • Waits for the login button to be clickable and clicks it.
  • Waits until the URL changes to the dashboard URL and asserts it.
  • Waits for the welcome message element and asserts its text.

tearDown() closes the browser after the test.

Explicit waits ensure the test waits only as long as needed for elements or conditions, avoiding flaky tests. Using IDs as locators is reliable and maintainable. Assertions verify the expected outcomes clearly.

Common Mistakes - 4 Pitfalls
{'mistake': 'Using time.sleep() instead of explicit waits', 'why_bad': 'Hardcoded sleeps slow tests and can cause failures if the page loads slower or faster than expected.', 'correct_approach': "Use Selenium's explicit waits like WebDriverWait with expected conditions to wait dynamically."}
Using brittle XPath locators like absolute paths
Not clearing input fields before sending keys
Not closing the browser after test
Bonus Challenge

Now add data-driven testing with 3 different sets of username and password inputs to verify login success or failure.

Show Hint