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 CalculatorSmokeSanityTest(unittest.TestCase):
def setUp(self):
# Setup WebDriver for the calculator app (assuming Appium or similar)
# For demonstration, using ChromeDriver to open a web calculator
self.driver = webdriver.Chrome()
self.driver.get('https://www.online-calculator.com/full-screen-calculator/')
self.wait = WebDriverWait(self.driver, 10)
def test_smoke_and_sanity(self):
driver = self.driver
wait = self.wait
# Smoke Test: Verify calculator main screen loads
calculator_frame = wait.until(EC.presence_of_element_located((By.ID, 'fullframe')))
self.assertTrue(calculator_frame.is_displayed(), 'Calculator main screen did not load')
# Switch to calculator iframe if needed
driver.switch_to.frame('fullframe')
# Sanity Test: Perform addition 5 + 3 = 8
# Using keyboard input for simplicity
body = driver.find_element(By.TAG_NAME, 'body')
body.send_keys('5')
body.send_keys('+')
body.send_keys('3')
body.send_keys('=')
# Wait and verify result (the calculator shows result in the title attribute)
# This is a simplification; real app may differ
wait.until(lambda d: '8' in d.title)
self.assertIn('8', driver.title, 'Addition result incorrect')
# Sanity Test: Perform subtraction 10 - 4 = 6
body.send_keys('C') # Clear
body.send_keys('1')
body.send_keys('0')
body.send_keys('-')
body.send_keys('4')
body.send_keys('=')
wait.until(lambda d: '6' in d.title)
self.assertIn('6', driver.title, 'Subtraction result incorrect')
def tearDown(self):
self.driver.quit()
if __name__ == '__main__':
unittest.main()This test script uses Selenium with Python's unittest framework to automate smoke and sanity tests for a calculator app.
setUp: Initializes the browser and opens the calculator webpage.
test_smoke_and_sanity: First, it waits for the calculator main screen to load (smoke test). Then it performs addition and subtraction operations by sending keys and verifies the results by checking the page title, which reflects the result (sanity tests).
tearDown: Closes the browser after tests.
Explicit waits ensure elements are ready before interacting. Assertions check that the app loads and calculations are correct.