0
0
Testing Fundamentalstesting~15 mins

Testing vs debugging distinction in Testing Fundamentals - Automation Approaches Compared

Choose your learning style9 modes available
Verify that the calculator adds two numbers correctly and debug if it fails
Preconditions (2)
Step 1: Enter number 5 in the first input field
Step 2: Enter number 3 in the second input field
Step 3: Click the 'Add' button
Step 4: Observe the result displayed
✅ Expected Result: The result displayed should be 8
Automation Requirements - unittest with Selenium WebDriver
Assertions Needed:
Verify the displayed result equals '8'
Best Practices:
Use explicit waits to wait for result element
Use clear and maintainable locators (e.g., By.ID or By.NAME)
Separate test logic from debugging code
Use descriptive assertion messages
Automated Solution
Testing Fundamentals
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).

Common Mistakes - 3 Pitfalls
Using time.sleep() instead of explicit waits
Using brittle XPath locators that break easily
Mixing debugging print statements inside test assertions
Bonus Challenge

Now add data-driven testing with 3 different pairs of numbers to verify addition works for all.

Show Hint