0
0
Testing Fundamentalstesting~15 mins

What software testing is in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Verify that the software correctly identifies a simple bug in a calculator function
Preconditions (2)
Step 1: Open the calculator application
Step 2: Enter the number 2
Step 3: Press the addition (+) button
Step 4: Enter the number 2
Step 5: Press the equals (=) button
Step 6: Observe the result displayed
✅ Expected Result: The calculator displays the result as 4
Automation Requirements - unittest with Selenium WebDriver
Assertions Needed:
Verify the displayed result equals '4'
Best Practices:
Use explicit waits to wait for the result to appear
Use clear and maintainable locators (e.g., By.ID or By.CSS_SELECTOR)
Structure code with setup and teardown methods
Keep assertions simple and focused
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 TestCalculatorAddition(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
        wait = WebDriverWait(driver, 10)

        # Click number 2 button
        driver.find_element(By.ID, 'btn-2').click()
        # Click plus button
        driver.find_element(By.ID, 'btn-plus').click()
        # Click number 2 button again
        driver.find_element(By.ID, 'btn-2').click()
        # Click equals button
        driver.find_element(By.ID, 'btn-equals').click()

        # Wait for result to appear
        result_element = wait.until(EC.visibility_of_element_located((By.ID, 'result')))

        # Assert the result is '4'
        self.assertEqual(result_element.text, '4')

    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: Clicks buttons for '2', '+', '2', and '=' in order. Then waits explicitly for the result element to appear. Finally, it asserts that the displayed result text is '4'.

tearDown: Closes the browser after the test finishes.

This structure keeps the test clear, waits properly for UI updates, and uses maintainable locators by ID.

Common Mistakes - 3 Pitfalls
Using time.sleep() instead of explicit waits
Using brittle XPath locators like absolute paths
Not closing the browser after tests
Bonus Challenge

Now add data-driven testing with 3 different addition inputs and expected results

Show Hint