SQL injection 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 TestSQLInjection(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://example.com/login') # Replace with actual login URL self.wait = WebDriverWait(self.driver, 10) def test_sql_injection_login(self): driver = self.driver wait = self.wait # Locate username field and enter SQL injection string username_field = wait.until(EC.visibility_of_element_located((By.ID, 'username'))) username_field.clear() username_field.send_keys("' OR '1'='1") # Locate password field and enter any password password_field = driver.find_element(By.ID, 'password') password_field.clear() password_field.send_keys('password') # Click the login button login_button = driver.find_element(By.ID, 'loginBtn') login_button.click() # Wait for error message or login failure indication error_message = wait.until(EC.visibility_of_element_located((By.ID, 'errorMsg'))) # Assert error message is displayed self.assertTrue(error_message.is_displayed(), 'Error message should be displayed') # Assert URL does not contain dashboard path current_url = driver.current_url self.assertNotIn('/dashboard', current_url, 'Should not navigate to dashboard on SQL injection') def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
This test script uses Selenium with Python's unittest framework.
setUp: Opens the browser and navigates to the login page.
test_sql_injection_login: Enters a classic SQL injection string in the username field and a dummy password, then clicks login.
It waits explicitly for the error message element to appear, ensuring the page has responded.
Assertions check that the error message is visible and the URL does not change to the dashboard, confirming login failure.
tearDown: Closes the browser after the test.
Explicit waits and meaningful locators (By.ID) are used for reliability and maintainability.
Now add data-driven testing with 3 different SQL injection strings in the username field
Practice
SQL injection testing in software testing?Solution
Step 1: Understand SQL injection testing purpose
SQL injection testing checks if user inputs can alter SQL queries in harmful ways.Step 2: Compare options with testing goal
Only To check if user input can change database queries and cause harm matches the goal of detecting harmful input effects on queries.Final Answer:
To check if user input can change database queries and cause harm -> Option AQuick Check:
SQL injection testing = check harmful input [OK]
- Confusing SQL injection testing with performance testing
- Thinking it tests UI or database design
- Ignoring the role of user input in testing
Solution
Step 1: Identify typical SQL injection input
Inputs like' OR '1'='1try to change query logic to always true.Step 2: Check other options
Options A, B, and D are full SQL commands, not typical injection inputs entered by users.Final Answer:
' OR '1'='1 -> Option DQuick Check:
Injection input = ' OR '1'='1 [OK]
- Choosing full SQL commands as injection input
- Not recognizing the use of quotes and OR in injection
- Confusing injection input with SQL statements
SELECT * FROM users WHERE username = '{input}' AND password = '{input}';
If the input for username is
admin and for password is ' OR '1'='1, what will happen?Solution
Step 1: Substitute inputs into query
The query becomes:SELECT * FROM users WHERE username = 'admin' AND password = '' OR '1'='1';Step 2: Analyze the WHERE clause logic
The OR '1'='1' makes the password condition always true, so the query returns all users matching username or always true condition.Final Answer:
The query will return all users, bypassing password check -> Option CQuick Check:
Injection bypasses password = returns all users [OK]
- Ignoring OR logic and thinking password check still applies
- Assuming syntax error without checking quotes
- Thinking only admin user is returned
query = "SELECT * FROM products WHERE id = '" + user_input + "'";
Which fix will prevent SQL injection?
Solution
Step 1: Identify the vulnerability
Concatenating user input directly into query allows injection.Step 2: Choose the correct fix
Using parameterized queries safely separates input from code, preventing injection.Final Answer:
Use parameterized queries or prepared statements -> Option BQuick Check:
Parameterized queries prevent injection [OK]
- Thinking adding quotes fixes injection
- Removing WHERE clause breaks functionality
- Changing case does not prevent injection
SELECT * FROM users WHERE name LIKE '%{input}%';Which input is the safest to test for SQL injection without causing errors or data loss?
Solution
Step 1: Understand safe testing input
Safe input should not break query syntax or cause harmful commands.Step 2: Evaluate options
Options A, B, and C contain injection attempts or dangerous commands; only D is a normal string safe for testing.Final Answer:
normaluser -> Option AQuick Check:
Safe test input = normaluser [OK]
- Using dangerous inputs that cause errors or data loss
- Assuming all injection inputs are safe to test
- Not understanding query context with LIKE and wildcards
Ready to prove it?
Pass the test and this pattern moves unknown
