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.