0
0
Testing Fundamentalstesting~15 mins

Smoke testing and sanity testing in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Smoke and Sanity Testing for a Simple Calculator App
Preconditions (2)
Step 1: Verify the calculator app opens without errors (Smoke Test)
Step 2: Perform addition: enter 5, press '+', enter 3, press '='
Step 3: Verify the result displayed is 8 (Sanity Test)
Step 4: Perform subtraction: enter 10, press '-', enter 4, press '='
Step 5: Verify the result displayed is 6 (Sanity Test)
✅ Expected Result: Calculator app opens successfully. Addition and subtraction operations return correct results.
Automation Requirements - Selenium with Python
Assertions Needed:
Verify app launch by checking presence of calculator main screen element
Verify displayed result matches expected value after operations
Best Practices:
Use explicit waits to wait for elements
Use clear and maintainable locators (e.g., By.ID or By.ACCESSIBILITY_ID)
Separate test setup and teardown
Use assertions to validate results
Automated Solution
Testing Fundamentals
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.

Common Mistakes - 3 Pitfalls
Using hardcoded sleep instead of explicit waits
Using brittle locators like absolute XPath
Not verifying the app launch before proceeding
Bonus Challenge

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

Show Hint