Testing vs debugging distinction in Testing Fundamentals - Automation Approaches Compared
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 CalculatorTest(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('http://localhost:8000/calculator') # Example URL def test_addition(self): driver = self.driver # Enter first number input1 = driver.find_element(By.ID, 'input1') input1.clear() input1.send_keys('5') # Enter second number input2 = driver.find_element(By.ID, 'input2') input2.clear() input2.send_keys('3') # Click Add button add_button = driver.find_element(By.ID, 'add-btn') add_button.click() # Wait for result to appear wait = WebDriverWait(driver, 10) result_element = wait.until(EC.visibility_of_element_located((By.ID, 'result'))) # Assert the result is '8' result_text = result_element.text self.assertEqual(result_text, '8', f'Expected result to be 8 but got {result_text}') def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
This test script uses Python's unittest framework with Selenium WebDriver to automate the calculator addition test.
setUp() opens the browser and navigates to the calculator page.
test_addition() enters the numbers 5 and 3, clicks the Add button, waits explicitly for the result element to appear, then asserts the displayed result equals '8'.
Explicit waits ensure the test waits for the result instead of using fixed delays.
Locators use By.ID for clarity and maintainability.
tearDown() closes the browser after the test.
This clear separation of test steps and assertions helps distinguish testing (checking the result) from debugging (fixing issues if the test fails).
Now add data-driven testing with 3 different pairs of numbers to verify addition works for all.