0
0
Testing Fundamentalstesting~15 mins

Equivalence partitioning in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Test input field with equivalence partitioning
Preconditions (1)
Step 1: Enter a value less than 18 in the age input field and submit the form.
Step 2: Enter a value between 18 and 60 inclusive in the age input field and submit the form.
Step 3: Enter a value greater than 60 in the age input field and submit the form.
✅ Expected Result: For values less than 18 or greater than 60, the form shows an error message 'Age must be between 18 and 60'. For values between 18 and 60 inclusive, the form submits successfully and shows a success message.
Automation Requirements - Selenium with Python
Assertions Needed:
Verify error message is displayed for invalid age inputs (less than 18 and greater than 60).
Verify success message is displayed for valid age inputs (between 18 and 60 inclusive).
Best Practices:
Use explicit waits to wait for elements to be visible.
Use clear and maintainable locators (e.g., By.ID or By.CSS_SELECTOR).
Separate test data from test logic.
Use assertions that clearly check the expected 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 TestAgeInputEquivalencePartitioning(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('http://example.com/age-form')  # Replace with actual URL
        self.wait = WebDriverWait(self.driver, 10)

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

    def submit_age_and_get_message(self, age):
        age_input = self.wait.until(EC.visibility_of_element_located((By.ID, 'age')))
        age_input.clear()
        age_input.send_keys(str(age))
        submit_button = self.driver.find_element(By.ID, 'submit')
        submit_button.click()
        message = self.wait.until(EC.visibility_of_element_located((By.ID, 'message')))
        return message.text

    def test_age_less_than_18(self):
        message = self.submit_age_and_get_message(17)
        self.assertEqual(message, 'Age must be between 18 and 60')

    def test_age_between_18_and_60(self):
        for valid_age in [18, 30, 60]:
            message = self.submit_age_and_get_message(valid_age)
            self.assertEqual(message, 'Form submitted successfully')

    def test_age_greater_than_60(self):
        message = self.submit_age_and_get_message(61)
        self.assertEqual(message, 'Age must be between 18 and 60')

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

This test script uses Selenium with Python's unittest framework to automate the equivalence partitioning test for an age input field.

setUp opens the browser and navigates to the form page.

tearDown closes the browser after each test.

The helper method submit_age_and_get_message enters the age, submits the form, and waits for the message to appear, returning its text.

Three test methods cover the partitions:

  • test_age_less_than_18 tests an invalid low age and expects an error message.
  • test_age_between_18_and_60 tests valid ages including boundary values and expects a success message.
  • test_age_greater_than_60 tests an invalid high age and expects an error message.

Explicit waits ensure elements are ready before interaction. Locators use IDs for clarity and maintainability. Assertions check the exact expected messages to confirm correct behavior.

Common Mistakes - 4 Pitfalls
Using hardcoded sleeps instead of explicit waits
Using brittle XPath locators that depend on full element paths
Not clearing the input field before entering new data
Testing only one value per partition
Bonus Challenge

Now add data-driven testing with 3 different inputs for each partition (less than 18, valid range, greater than 60).

Show Hint