Risk-based 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.username_input = (By.ID, 'username') self.password_input = (By.ID, 'password') self.login_button = (By.ID, 'loginBtn') self.error_message = (By.ID, 'errorMsg') def enter_username(self, username): self.driver.find_element(*self.username_input).clear() self.driver.find_element(*self.username_input).send_keys(username) def enter_password(self, password): self.driver.find_element(*self.password_input).clear() self.driver.find_element(*self.password_input).send_keys(password) def click_login(self): self.driver.find_element(*self.login_button).click() def get_error_message(self): return self.driver.find_element(*self.error_message).text class TestRiskBasedLogin(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com/login') self.wait = WebDriverWait(self.driver, 10) self.login_page = LoginPage(self.driver) def tearDown(self): self.driver.quit() def test_valid_login(self): self.login_page.enter_username('user1') self.login_page.enter_password('Password123!') self.login_page.click_login() self.wait.until(EC.url_contains('/dashboard')) self.assertIn('/dashboard', self.driver.current_url, 'Dashboard URL not loaded after valid login') def test_invalid_username(self): self.login_page.enter_username('wronguser') self.login_page.enter_password('Password123!') self.login_page.click_login() self.wait.until(EC.visibility_of_element_located(self.login_page.error_message)) error = self.login_page.get_error_message() self.assertIn('Invalid username', error, 'Error message for invalid username not shown') def test_invalid_password(self): self.login_page.enter_username('user1') self.login_page.enter_password('WrongPass!') self.login_page.click_login() self.wait.until(EC.visibility_of_element_located(self.login_page.error_message)) error = self.login_page.get_error_message() self.assertIn('Invalid password', error, 'Error message for invalid password not shown') def test_empty_fields(self): self.login_page.enter_username('') self.login_page.enter_password('') self.login_page.click_login() self.wait.until(EC.visibility_of_element_located(self.login_page.error_message)) error = self.login_page.get_error_message() self.assertIn('Username and password required', error, 'Error message for empty fields not shown') def test_sql_injection_attempt(self): self.login_page.enter_username("' OR '1'='1") self.login_page.enter_password('Password123!') self.login_page.click_login() self.wait.until(EC.visibility_of_element_located(self.login_page.error_message)) error = self.login_page.get_error_message() self.assertIn('Invalid username', error, 'Error message for SQL injection attempt not shown') def test_very_long_input(self): long_input = 'a' * 500 self.login_page.enter_username(long_input) self.login_page.enter_password(long_input) self.login_page.click_login() self.wait.until(EC.visibility_of_element_located(self.login_page.error_message)) error = self.login_page.get_error_message() self.assertIn('Input too long', error, 'Error message for very long input not shown') if __name__ == '__main__': unittest.main()
The code uses Selenium with Python's unittest framework to automate risk-based testing of the login page.
The LoginPage class follows the Page Object Model pattern, storing locators and actions for the login page. This keeps the test code clean and maintainable.
Each test method targets a specific risk identified in the manual test case: valid login, invalid username, invalid password, empty fields, SQL injection attempt, and very long inputs.
Explicit waits (WebDriverWait) ensure the test waits for page elements or URL changes before asserting, avoiding flaky tests.
Assertions check for correct URL or error messages with clear messages to help diagnose failures.
The setup and teardown methods open and close the browser for each test, ensuring a fresh start.
Now add data-driven testing with 3 different sets of invalid credentials to verify error handling.
Practice
risk-based testing in software testing?Solution
Step 1: Understand the purpose of risk-based testing
Risk-based testing aims to prioritize testing efforts on areas with the highest risk to save time and improve effectiveness.Step 2: Compare options with this purpose
To focus testing on the most important and risky parts first matches this goal by focusing on important and risky parts first, while others do not.Final Answer:
To focus testing on the most important and risky parts first -> Option CQuick Check:
Risk-based testing = Focus on risky parts first [OK]
- Thinking all parts are tested equally
- Believing risk-based testing ignores important parts
- Confusing risk-based testing with UI-only testing
Solution
Step 1: Recall how risk is calculated in testing
Risk is usually calculated by combining the impact of a problem and how likely it is to happen.Step 2: Match this with the options
By combining the impact and likelihood of potential problems correctly states this combination, while others focus on unrelated criteria.Final Answer:
By combining the impact and likelihood of potential problems -> Option AQuick Check:
Risk = Impact x Likelihood [OK]
- Prioritizing by ease or alphabetical order
- Ignoring likelihood of problems
- Testing only new features without risk analysis
Feature A: Impact=5, Likelihood=4
Feature B: Impact=3, Likelihood=6
Feature C: Impact=4, Likelihood=2Which feature should be tested first based on risk?
Solution
Step 1: Calculate risk for each feature
Risk = Impact x Likelihood.
Feature A: 5 x 4 = 20
Feature B: 3 x 6 = 18
Feature C: 4 x 2 = 8Step 2: Identify highest risk
Feature A has the highest risk score (20), so it should be tested first.Final Answer:
Feature A -> Option BQuick Check:
Highest risk = Feature A (20) [OK]
- Choosing feature with highest likelihood only
- Ignoring impact in calculation
- Testing all features equally without prioritization
features = [{"name": "A", "impact": 5, "likelihood": 4},
{"name": "B", "impact": 3, "likelihood": 6}]
for feature in features:
risk = feature["impact"] + feature["likelihood"]
if risk > 8:
print(feature["name"])What is wrong with this code for risk-based testing?
Solution
Step 1: Review risk calculation method
Risk-based testing calculates risk by multiplying impact and likelihood, not adding them.Step 2: Identify error in code
The code adds impact and likelihood, which is incorrect for risk calculation.Final Answer:
Risk should be calculated by multiplying impact and likelihood, not adding -> Option AQuick Check:
Risk = Impact x Likelihood, not sum [OK]
- Adding instead of multiplying impact and likelihood
- Confusing dictionary access syntax
- Assuming sorting is required before risk check
Feature X: 12
Feature Y: 20
Feature Z: 20Due to limited time, you can only test two features. Which is the best risk-based testing approach?
Solution
Step 1: Identify features with highest risk
Feature Y and Feature Z both have the highest risk score of 20.Step 2: Prioritize testing based on risk and time limit
Since only two can be tested, choose the two with highest risk to maximize risk coverage.Final Answer:
Test Feature Y and Feature Z because they have the highest risk -> Option DQuick Check:
Test highest risk features first [OK]
- Testing lower risk features first
- Ignoring risk scores when time is limited
- Trying to test all features equally despite limits
Ready to prove it?
Pass the test and this pattern moves unknown
