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.