0
0
Testing Fundamentalstesting~15 mins

Decision table testing in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Verify discount calculation using decision table
Preconditions (1)
Step 1: Enter purchase amount as 50
Step 2: Select customer type as 'Regular'
Step 3: Click Calculate Discount button
Step 4: Verify discount is 0
Step 5: Enter purchase amount as 150
Step 6: Select customer type as 'Regular'
Step 7: Click Calculate Discount button
Step 8: Verify discount is 10
Step 9: Enter purchase amount as 50
Step 10: Select customer type as 'Premium'
Step 11: Click Calculate Discount button
Step 12: Verify discount is 5
Step 13: Enter purchase amount as 150
Step 14: Select customer type as 'Premium'
Step 15: Click Calculate Discount button
Step 16: Verify discount is 20
✅ Expected Result: Discount is correctly calculated based on purchase amount and customer type according to the decision table rules.
Automation Requirements - Selenium with Python
Assertions Needed:
Verify discount value displayed matches expected discount for each input combination
Best Practices:
Use explicit waits to wait for elements
Use clear and maintainable locators (id or name preferred)
Separate test data from test logic
Use assertions from unittest or pytest
Structure code for readability
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 DiscountCalculatorTest(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('http://example.com/discount-calculator')
        self.wait = WebDriverWait(self.driver, 10)

    def test_discount_calculation(self):
        test_cases = [
            {'amount': '50', 'customer_type': 'Regular', 'expected_discount': '0'},
            {'amount': '150', 'customer_type': 'Regular', 'expected_discount': '10'},
            {'amount': '50', 'customer_type': 'Premium', 'expected_discount': '5'},
            {'amount': '150', 'customer_type': 'Premium', 'expected_discount': '20'}
        ]

        for case in test_cases:
            amount_input = self.wait.until(EC.presence_of_element_located((By.ID, 'purchaseAmount')))
            amount_input.clear()
            amount_input.send_keys(case['amount'])

            customer_select = self.wait.until(EC.presence_of_element_located((By.ID, 'customerType')))
            for option in customer_select.find_elements(By.TAG_NAME, 'option'):
                if option.text == case['customer_type']:
                    option.click()
                    break

            calculate_button = self.wait.until(EC.element_to_be_clickable((By.ID, 'calculateBtn')))
            calculate_button.click()

            discount_output = self.wait.until(EC.visibility_of_element_located((By.ID, 'discountValue')))
            self.assertEqual(discount_output.text, case['expected_discount'],
                             f"Discount for amount {case['amount']} and customer {case['customer_type']} should be {case['expected_discount']}")

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

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

This test script uses Selenium with Python's unittest framework to automate the discount calculator page.

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

The test_discount_calculation method loops through all decision table cases. For each case, it:

  • Finds and fills the purchase amount input.
  • Selects the customer type from dropdown.
  • Clicks the calculate button.
  • Waits for the discount output to appear.
  • Asserts the displayed discount matches the expected value.

Explicit waits ensure elements are ready before interacting.

Locators use IDs for clarity and maintainability.

The test data is separated in a list for easy updates.

Finally, tearDown closes the browser after tests.

Common Mistakes - 4 Pitfalls
Using time.sleep() instead of explicit waits
Using brittle XPath locators with full paths
Hardcoding test data inside test steps
Not clearing input fields before sending keys
Bonus Challenge

Now add data-driven testing with 3 more different purchase amounts and customer types.

Show Hint