Security testing basics 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 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): WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.username_input)).clear() self.driver.find_element(*self.username_input).send_keys(username) 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() def get_error_message(self): return WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(self.error_message)).text def test_sql_injection_login(): driver = webdriver.Chrome() driver.get('https://example.com/login') login_page = LoginPage(driver) login_page.enter_username('admin') login_page.enter_password("' OR '1'='1") login_page.click_login() error_text = login_page.get_error_message() assert error_text == 'Invalid username or password', f"Expected error message not found, got: {error_text}" assert driver.current_url == 'https://example.com/login', "URL changed after failed login attempt" driver.quit() if __name__ == '__main__': test_sql_injection_login()
This test script uses Selenium with Python to automate the manual test case.
We define a LoginPage class to hold locators and actions, following the Page Object Model. This keeps the test clean and easy to maintain.
Explicit waits ensure the script waits for elements to be ready before interacting, avoiding flaky tests.
The test function test_sql_injection_login opens the login page, enters the username 'admin' and the SQL injection string in the password field, then clicks login.
It asserts that the error message is exactly 'Invalid username or password' and that the URL remains on the login page, confirming the injection attempt failed.
Finally, the browser closes cleanly.
Now add data-driven testing with 3 different SQL injection payloads in the password field
Practice
security testing in software development?Solution
Step 1: Understand the purpose of security testing
Security testing aims to find weak points that attackers could exploit.Step 2: Compare options to the goal
Only To find and fix weaknesses that could let attackers harm the software talks about finding and fixing weaknesses to protect software.Final Answer:
To find and fix weaknesses that could let attackers harm the software -> Option DQuick Check:
Security testing = find weaknesses [OK]
- Confusing security testing with performance testing
- Thinking security testing checks UI design
- Mixing security testing with spell checking
security test case?Solution
Step 1: Identify what a security test case checks
Security test cases verify protection against unauthorized access or attacks.Step 2: Match options to security focus
Check if the login page accepts only valid usernames and passwords tests login validation, which is a security check; others test performance, UI, or localization.Final Answer:
Check if the login page accepts only valid usernames and passwords -> Option AQuick Check:
Security test case = validate access control [OK]
- Choosing performance or UI tests as security tests
- Ignoring the focus on unauthorized access
- Confusing localization with security
def test_password_length(password):
return len(password) >= 8
result = test_password_length('pass123')
print(result)What will be the output when this code runs?
Solution
Step 1: Understand the function logic
The function returns True if password length is 8 or more, else False.Step 2: Check the input password length
'pass123' has 7 characters, which is less than 8, so function returns False.Final Answer:
False -> Option BQuick Check:
Password length 7 < 8 = False [OK]
- Assuming 'pass123' length is 8
- Confusing True/False return values
- Expecting an error due to missing input validation
def is_safe_input(user_input):
if ''))What is the problem with this test?
Solution
Step 1: Analyze the input check logic
The function looks only for the exact string '<script>' in input.Step 2: Consider other attack variations
Attackers can use variations like uppercase, spaces, or encoded forms that this check misses.Final Answer:
It only checks for exact '<script>' but misses other attack forms -> Option CQuick Check:
Exact string check misses variants [OK]
- Thinking the code has syntax errors
- Assuming it blocks all script attacks
- Believing it never returns False
Solution
Step 1: Understand the strengths of automated and manual testing
Automated tools quickly find many injection points; manual tests explore complex cases.Step 2: Evaluate options for best practice
Use an automated scanner to find injection points, then manually try custom payloads to confirm and explore combines both methods effectively; others ignore manual checks or focus on unrelated issues.Final Answer:
Use an automated scanner to find injection points, then manually try custom payloads to confirm and explore -> Option AQuick Check:
Combine tools + manual tests for best security [OK]
- Relying only on automated tools
- Skipping manual testing
- Ignoring SQL injection risks
Ready to prove it?
Pass the test and this pattern moves unknown
