0
0
Testing Fundamentalstesting~10 mins

SQL injection testing in Testing Fundamentals - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks if the login form is vulnerable to SQL injection by entering a malicious input. It verifies that the system does not allow unauthorized access using SQL injection.

Test Code - Selenium with Python unittest
Testing Fundamentals
import unittest
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 TestSQLInjection(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.get('http://example.com/login')

    def test_sql_injection_login(self):
        driver = self.driver
        # Find username and password fields
        username_field = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.ID, 'username'))
        )
        password_field = driver.find_element(By.ID, 'password')

        # Enter SQL injection string
        username_field.clear()
        username_field.send_keys("' OR '1'='1'")
        password_field.clear()
        password_field.send_keys("anything")

        # Click login button
        login_button = driver.find_element(By.ID, 'login-btn')
        login_button.click()

        # Wait for response and check for login failure message
        error_message = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.ID, 'login-error'))
        )

        # Assert that error message is displayed, meaning injection failed
        self.assertTrue(error_message.is_displayed(), "SQL Injection succeeded, vulnerability found")

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()
Execution Trace - 9 Steps
StepActionSystem StateAssertionResult
1Test starts and opens Chrome browserBrowser opened at 'http://example.com/login' showing login form with username, password fields and login button-PASS
2Find username input field by ID 'username'Username input field is present and ready for inputWebDriverWait confirms presence of username fieldPASS
3Find password input field by ID 'password'Password input field is present and ready for input-PASS
4Enter SQL injection string "' OR '1'='1'" into username fieldUsername field contains malicious input-PASS
5Enter 'anything' into password fieldPassword field contains input-PASS
6Find and click login button by ID 'login-btn'Login button clicked, form submitted-PASS
7Wait for error message element with ID 'login-error' to appearError message displayed on page indicating login failureCheck that error message is displayed to confirm login failedPASS
8Assert error message is displayed to verify SQL injection did not bypass loginTest confirms login failure message is visibleassertTrue(error_message.is_displayed())PASS
9Close browser and end testBrowser closed-PASS
Failure Scenario
Failing Condition: The login form is vulnerable and accepts the SQL injection input, allowing unauthorized access.
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify after clicking the login button with SQL injection input?
AThat the login button is disabled
BThat the user is logged in successfully
CThat an error message is displayed indicating login failure
DThat the password field is cleared
Key Result
Always verify that input fields are protected against SQL injection by checking that malicious inputs do not bypass authentication and that proper error messages appear.