Bird
Raised Fist0
Testing Fundamentalstesting~15 mins

Transitioning to automation in Testing Fundamentals - Build an Automation Script

Choose your learning style10 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

Practice

(1/5)
1. Why is automation testing preferred for repeated test cases?
easy
A. Because it requires more manual effort
B. Because it cannot be reused
C. Because it is slower than manual testing
D. Because it runs tests quickly and without human error

Solution

  1. Step 1: Understand the nature of repeated tests

    Repeated tests are often boring and time-consuming when done manually.
  2. Step 2: Identify benefits of automation for repeated tests

    Automation runs tests quickly and consistently without human mistakes.
  3. Final Answer:

    Because it runs tests quickly and without human error -> Option D
  4. Quick Check:

    Automation speeds up repeated tests = B [OK]
Hint: Automation is best for fast, error-free repeated tests [OK]
Common Mistakes:
  • Thinking automation needs more manual work
  • Believing automation is slower than manual testing
  • Assuming automation cannot be reused
2. Which of the following is the correct way to start an automation script in Python?
easy
A. def test_function():\n assert True
B. function test_function() { assert true; }
C. test_function() => { assert true }
D. start test_function() { assert true }

Solution

  1. Step 1: Identify correct Python function syntax

    Python functions start with 'def' followed by the function name and parentheses.
  2. Step 2: Check the options for Python syntax

    def test_function():\n assert True uses 'def' and a colon, which is correct Python syntax.
  3. Final Answer:

    def test_function():\n assert True -> Option A
  4. Quick Check:

    Python functions start with def = D [OK]
Hint: Python functions start with 'def' keyword and colon [OK]
Common Mistakes:
  • Using JavaScript or other language syntax in Python
  • Missing colon after function definition
  • Incorrect arrow function syntax in Python
3. What will be the output of this Python test script?
def test_sum():
    total = 0
    for i in range(3):
        total += i
    assert total == 3
medium
A. SyntaxError occurs
B. Test passes successfully
C. AssertionError occurs
D. Test is skipped

Solution

  1. Step 1: Calculate the sum in the loop

    Loop runs i=0,1,2; total starts at 0, then adds 0+1+2 resulting in 3.
  2. Step 2: Verify the assertion total == 3

    total = 0+1+2 = 3, so assertion total == 3 is True, no error.
  3. Final Answer:

    Test passes successfully -> Option B
  4. Quick Check:

    Sum 0+1+2=3, assertion true = C [OK]
Hint: Sum range(3) is 0+1+2=3, so assertion passes [OK]
Common Mistakes:
  • Miscounting the sum of range(3)
  • Assuming assertion fails without calculation
  • Confusing syntax error with logic error
4. Identify the error in this automation test snippet:
def test_login():
    username = "user"
    password = "pass"
    assert username = "user"
medium
A. Using single equals '=' instead of '==' in assertion
B. Missing parentheses in assert statement
C. Incorrect variable names
D. Password variable is unused

Solution

  1. Step 1: Check the assertion syntax

    In Python, '=' is assignment, '==' is comparison. Assertion needs '==' to compare.
  2. Step 2: Identify the error type

    Using '=' in assert causes SyntaxError because assignment is not allowed there.
  3. Final Answer:

    Using single equals '=' instead of '==' in assertion -> Option A
  4. Quick Check:

    Assertions need '==' for comparison = A [OK]
Hint: Use '==' in assert, not '=' [OK]
Common Mistakes:
  • Confusing assignment '=' with comparison '=='
  • Ignoring syntax errors in assert statements
  • Thinking unused variables cause assertion errors
5. You want to automate a test that runs daily and checks if a website's homepage loads correctly. Which approach is best to start your automation?
hard
A. Manually check the homepage daily instead of automating
B. Write a complex script that tests every link on the homepage before scheduling
C. Write a simple script that opens the homepage and checks the title, then schedule it to run daily
D. Automate only after the website has no changes for a month

Solution

  1. Step 1: Choose a simple test to automate first

    Starting with a simple script to check the homepage title is easy and effective.
  2. Step 2: Schedule the script for daily runs

    Scheduling the script automates the daily check without manual effort.
  3. Final Answer:

    Write a simple script that opens the homepage and checks the title, then schedule it to run daily -> Option C
  4. Quick Check:

    Start simple and schedule = A [OK]
Hint: Start with simple checks before adding complexity [OK]
Common Mistakes:
  • Trying to automate too many things at once
  • Ignoring automation benefits and doing manual checks
  • Waiting too long to start automation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard