Bird
Raised Fist0
Testing Fundamentalstesting~15 mins

Data validation rules in Testing Fundamentals - Build an Automation Script

Choose your learning style10 modes available
Validate user registration form data
Preconditions (1)
Step 1: Enter 'John' in the First Name field
Step 2: Enter 'Doe' in the Last Name field
Step 3: Enter 'john.doe@example.com' in the Email field
Step 4: Enter 'Password123!' in the Password field
Step 5: Enter 'Password123!' in the Confirm Password field
Step 6: Click the Submit button
✅ Expected Result: Form submits successfully and confirmation message 'Registration successful' is displayed
Automation Requirements - Selenium with Python
Assertions Needed:
Verify that the confirmation message 'Registration successful' is displayed after submission
Verify that no validation error messages are shown
Best Practices:
Use explicit waits to wait for elements to be visible or clickable
Use meaningful locators such as By.ID or By.NAME
Use Page Object Model to separate page structure from test logic
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 RegistrationPage:
    def __init__(self, driver):
        self.driver = driver
        self.first_name = driver.find_element(By.ID, 'firstName')
        self.last_name = driver.find_element(By.ID, 'lastName')
        self.email = driver.find_element(By.ID, 'email')
        self.password = driver.find_element(By.ID, 'password')
        self.confirm_password = driver.find_element(By.ID, 'confirmPassword')
        self.submit_button = driver.find_element(By.ID, 'submitBtn')

    def fill_form(self, first, last, email, pwd, confirm_pwd):
        self.first_name.clear()
        self.first_name.send_keys(first)
        self.last_name.clear()
        self.last_name.send_keys(last)
        self.email.clear()
        self.email.send_keys(email)
        self.password.clear()
        self.password.send_keys(pwd)
        self.confirm_password.clear()
        self.confirm_password.send_keys(confirm_pwd)

    def submit(self):
        self.submit_button.click()

class TestRegistration(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://example.com/registration')
        self.wait = WebDriverWait(self.driver, 10)
        self.page = RegistrationPage(self.driver)

    def test_valid_registration(self):
        self.page.fill_form('John', 'Doe', 'john.doe@example.com', 'Password123!', 'Password123!')
        self.page.submit()

        confirmation = self.wait.until(
            EC.visibility_of_element_located((By.ID, 'confirmationMessage'))
        )
        self.assertEqual(confirmation.text, 'Registration successful')

        # Verify no validation errors
        errors = self.driver.find_elements(By.CLASS_NAME, 'validation-error')
        self.assertEqual(len(errors), 0)

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

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

The RegistrationPage class uses the Page Object Model to keep element locators and actions together. This makes the test easier to maintain.

In test_valid_registration, we fill the form with valid data and submit it.

We use WebDriverWait with visibility_of_element_located to wait for the confirmation message to appear, ensuring the test does not fail due to timing issues.

Assertions check that the confirmation message text is exactly 'Registration successful' and that no validation error messages are present.

The tearDown method closes the browser after the test.

Common Mistakes - 3 Pitfalls
Using hardcoded sleeps like time.sleep() instead of explicit waits
Using brittle locators like absolute XPath
Mixing test logic and page structure in the test method
Bonus Challenge

Now add data-driven testing with 3 different sets of valid user data to verify the registration form works for multiple inputs.

Show Hint

Practice

(1/5)
1. What is the main purpose of data validation rules in software testing?
easy
A. To check if input data is correct and useful
B. To improve the speed of the software
C. To change the data format automatically
D. To delete old data from the system

Solution

  1. Step 1: Understand the role of data validation

    Data validation rules are designed to check input data for correctness and usefulness before processing.
  2. Step 2: Compare options with the definition

    Only To check if input data is correct and useful correctly states this purpose; others describe unrelated tasks.
  3. Final Answer:

    To check if input data is correct and useful -> Option A
  4. Quick Check:

    Data validation = Check correctness [OK]
Hint: Validation means checking data correctness early [OK]
Common Mistakes:
  • Confusing validation with data transformation
  • Thinking validation improves software speed
  • Assuming validation deletes data
2. Which of the following is a correct example of a data validation rule for an email field?
easy
A. The field must contain only numbers
B. The field must not be empty and must contain '@' symbol
C. The field must be exactly 5 characters long
D. The field must be left blank

Solution

  1. Step 1: Identify common email validation rules

    Email fields require non-empty input and must include '@' to be valid.
  2. Step 2: Evaluate each option

    The field must not be empty and must contain '@' symbol matches these rules; A, B, and D do not fit email validation.
  3. Final Answer:

    The field must not be empty and must contain '@' symbol -> Option B
  4. Quick Check:

    Email validation = Non-empty + '@' [OK]
Hint: Emails always need '@' and something before/after [OK]
Common Mistakes:
  • Checking length without '@' for emails
  • Allowing empty email fields
  • Restricting emails to numbers only
3. Consider this validation rule in a test script:
assert age >= 18 and age <= 65

What will happen if age = 70 during test execution?
medium
A. Test will ignore the age value
B. Test will pass because 70 is a valid age
C. Test will raise a syntax error
D. Test will fail because 70 is outside the valid range

Solution

  1. Step 1: Understand the validation condition

    The rule requires age to be between 18 and 65 inclusive.
  2. Step 2: Check the given age value

    Age 70 is greater than 65, so the condition fails and assertion fails.
  3. Final Answer:

    Test will fail because 70 is outside the valid range -> Option D
  4. Quick Check:

    Age 70 > 65 = Fail [OK]
Hint: Check if value fits the range in assertion [OK]
Common Mistakes:
  • Assuming test passes if value is above max
  • Confusing syntax error with assertion failure
  • Ignoring assertion conditions
4. A tester wrote this validation rule:
assert len(username) > 0 and len(username) < 15

What is the problem with this rule if the username length is exactly 15?
medium
A. The test will fail because 15 is not less than 15
B. The test will pass because 15 is allowed
C. The test will cause a runtime error
D. The test will ignore the username length

Solution

  1. Step 1: Analyze the length condition

    The rule requires username length to be greater than 0 and less than 15.
  2. Step 2: Check the boundary value 15

    Length 15 is not less than 15, so assertion fails.
  3. Final Answer:

    The test will fail because 15 is not less than 15 -> Option A
  4. Quick Check:

    Length 15 < 15? No = Fail [OK]
Hint: Check if boundary values are included or excluded [OK]
Common Mistakes:
  • Assuming less than includes the number itself
  • Ignoring boundary conditions
  • Thinking test causes error instead of fail
5. You need to validate a form field that accepts only positive integers less than 1000 and not empty. Which combined validation rule is correct?
hard
A. assert value.isalpha() and int(value) > 0
B. assert value != '' and int(value) >= 1000
C. assert value.isdigit() and 0 < int(value) < 1000
D. assert value == '' or int(value) < 0

Solution

  1. Step 1: Understand the requirements

    Value must be non-empty, only digits, positive, and less than 1000.
  2. Step 2: Evaluate each option

    assert value.isdigit() and 0 < int(value) < 1000 checks digits only, converts to int, and tests range correctly. Others fail one or more conditions.
  3. Final Answer:

    assert value.isdigit() and 0 < int(value) < 1000 -> Option C
  4. Quick Check:

    Digits + positive + <1000 = assert value.isdigit() and 0 < int(value) < 1000 [OK]
Hint: Check digits, non-empty, and range in one assertion [OK]
Common Mistakes:
  • Using isalpha() instead of isdigit()
  • Allowing empty strings
  • Checking wrong numeric ranges
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