User story testing in Testing Fundamentals - Build an Automation Script
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 LoginPage: def __init__(self, driver): self.driver = driver self.email_input = (By.ID, 'email') self.password_input = (By.ID, 'password') self.login_button = (By.ID, 'login-btn') def enter_email(self, email): WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.email_input)).clear() self.driver.find_element(*self.email_input).send_keys(email) def enter_password(self, password): WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.password_input)).clear() self.driver.find_element(*self.password_input).send_keys(password) def click_login(self): WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable(self.login_button)).click() class DashboardPage: def __init__(self, driver): self.driver = driver self.welcome_message = (By.ID, 'welcome-msg') def is_welcome_message_displayed(self): return WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.welcome_message)) is not None class TestLogin(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com/login') def test_valid_login(self): login_page = LoginPage(self.driver) login_page.enter_email('user@example.com') login_page.enter_password('Password123!') login_page.click_login() # Wait for URL to contain '/dashboard' WebDriverWait(self.driver, 10).until(EC.url_contains('/dashboard')) self.assertIn('/dashboard', self.driver.current_url, 'URL does not contain /dashboard after login') dashboard_page = DashboardPage(self.driver) self.assertTrue(dashboard_page.is_welcome_message_displayed(), 'Welcome message not displayed on dashboard') def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
This test script uses Selenium with Python and unittest framework to automate the user story test case.
We define two page classes: LoginPage and DashboardPage to keep locators and actions organized (Page Object Model).
In test_valid_login, we enter the email and password, click login, then wait explicitly for the URL to contain '/dashboard'.
We assert the URL contains '/dashboard' and that the welcome message is visible on the dashboard page.
Explicit waits ensure the test waits for elements or URL changes properly, avoiding flaky tests.
The setUp and tearDown methods handle browser start and close for clean test runs.
Now add data-driven testing with 3 different sets of login credentials: one valid, two invalid
Practice
user story testing in software development?Solution
Step 1: Understand user story testing focus
User story testing focuses on validating features from the user's perspective, ensuring the software solves real user problems.Step 2: Compare options to user story testing goal
Options A, B, and D focus on technical or internal aspects, not user needs.Final Answer:
To verify that the software meets the user's needs and expectations -> Option AQuick Check:
User needs = Verify software meets needs [OK]
- Confusing user story testing with performance testing
- Thinking it focuses on code quality
- Assuming it tests installation or setup
Solution
Step 1: Identify the user story goal
The story is about logging in to access the dashboard, so the test should check login success with valid credentials.Step 2: Match test scenarios to the story
Verify that entering valid credentials allows access to the dashboard directly tests the login functionality as per the story. Options A, C, and D focus on performance or backend, not user goal.Final Answer:
Verify that entering valid credentials allows access to the dashboard -> Option CQuick Check:
Test scenario matches user goal [OK]
- Testing technical details unrelated to user goal
- Confusing performance tests with functional tests
- Ignoring the 'so that' part of the user story
Solution
Step 1: Understand the user story's expected outcome
The story expects items to be added to the cart and visible for later purchase.Step 2: Identify the test result that matches this outcome
After adding an item, the cart shows the correct item and quantity confirms the cart updates correctly. Options A, B, and C do not confirm correct cart behavior.Final Answer:
After adding an item, the cart shows the correct item and quantity -> Option AQuick Check:
Cart updates correctly = Feature works [OK]
- Confusing performance with functional correctness
- Assuming errors mean success
- Ignoring UI feedback like cart icon visibility
Solution
Step 1: Analyze the test step and failure
The test clicks delete but the user remains, suggesting a missing confirmation step.Step 2: Identify common deletion flow issues
Many delete actions require confirming a popup; missing this blocks deletion. Options B, C, and D are less likely causes.Final Answer:
The test missed confirming the confirmation popup before deletion -> Option BQuick Check:
Missing confirmation popup blocks deletion [OK]
- Ignoring confirmation popups
- Assuming UI refresh means deletion
- Not verifying correct user permissions
Solution
Step 1: Understand the user story and edge cases
The story requires filtering by price, including cases where no products match.Step 2: Evaluate test cases for coverage
Test filtering with minimum price 0, maximum price 100, and no products in range tests a full range including zero and no matching products, covering normal and edge cases. Options B, C, and D miss some edge cases or use invalid inputs.Final Answer:
Test filtering with minimum price 0, maximum price 100, and no products in range -> Option DQuick Check:
Full range and no-match edge case tested [OK]
- Ignoring no-match scenarios
- Testing only partial filters
- Using invalid negative prices without story support
Ready to prove it?
Pass the test and this pattern moves unknown
