XSS 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 from selenium.common.exceptions import TimeoutException, NoAlertPresentException # Initialize the WebDriver (assumes chromedriver is in PATH) driver = webdriver.Chrome() try: driver.get('http://example.com/comment') # Replace with actual URL # Locate the comment input field and submit button comment_input = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, 'comment')) ) submit_button = driver.find_element(By.ID, 'submit-comment') # Enter the XSS test script xss_payload = "<script>alert('XSS')</script>" comment_input.clear() comment_input.send_keys(xss_payload) # Submit the comment submit_button.click() # Wait for the comment to appear on the page comment_display = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, 'comment-display')) ) # Check for alert popup - should NOT appear try: WebDriverWait(driver, 3).until(EC.alert_is_present()) alert = driver.switch_to.alert alert_text = alert.text alert.dismiss() raise AssertionError(f"Unexpected alert appeared with text: {alert_text}") except TimeoutException: # No alert appeared, which is expected pass # Verify the displayed comment contains the script tags as plain text displayed_text = comment_display.text assert xss_payload in displayed_text, "The script tags are not displayed as plain text" finally: driver.quit()
This script opens the web page with the comment form.
It waits until the comment input and submit button are present, then enters the XSS payload <script>alert('XSS')</script>.
After clicking submit, it waits for the comment to appear on the page.
It then checks if any alert popup appears. If an alert appears, the test fails because the script executed.
If no alert appears, it verifies that the comment text includes the script tags as plain text, meaning the input was safely handled.
Explicit waits ensure the test waits for elements and alerts properly.
Using By.ID locators is a best practice for stable element selection.
Now add data-driven testing with 3 different XSS payloads to verify the application handles all safely.
Practice
XSS testing in software testing?Solution
Step 1: Understand XSS testing purpose
XSS testing aims to find if malicious scripts can run on a website, which can harm users or data.Step 2: Compare options to XSS goal
Only "To check if harmful scripts can run on a website" matches the goal of detecting harmful script execution on websites.Final Answer:
To check if harmful scripts can run on a website -> Option AQuick Check:
XSS testing = harmful script check [OK]
- Confusing XSS testing with performance testing
- Thinking XSS tests database or UI only
- Ignoring script execution risks
Solution
Step 1: Identify typical XSS test input
XSS tests use script tags like<script>alert('XSS')</script>to check if scripts run on the site.Step 2: Eliminate unrelated inputs
Options B and C are SQL commands, not XSS scripts.console.log('test')is JavaScript code but not wrapped in script tags for injection.Final Answer:
<script>alert('XSS')</script>-> Option BQuick Check:
Script tags = XSS test input [OK]
- Using SQL commands as XSS inputs
- Not wrapping JavaScript in script tags
- Confusing console logs with injection
<img src=x onerror=alert('XSS')>, what is the expected result if the site is vulnerable to XSS?Solution
Step 1: Understand the input behavior
The input uses an image tag with an invalid source and an onerror event that triggers an alert. If the site is vulnerable, this script runs.Step 2: Predict the output on vulnerability
If vulnerable, the alert('XSS') will execute, showing a popup message.Final Answer:
The alert box with message 'XSS' will appear -> Option DQuick Check:
onerror alert triggers popup if vulnerable [OK]
- Assuming input is saved as text without execution
- Thinking image loads normally despite invalid src
- Confusing form errors with script execution
<script>alert('test')</script>. The alert does not appear, but the input is stored and shown as text on the page. What is the likely reason?Solution
Step 1: Analyze why alert did not appear
The alert script is stored and shown as text, meaning it was not executed but escaped.Step 2: Identify cause of script escaping
Proper XSS protection escapes script tags so they display as text, preventing execution.Final Answer:
The site properly escapes script tags to prevent execution -> Option AQuick Check:
Escaping scripts = no execution [OK]
- Assuming browser blocked all JavaScript
- Thinking input was rejected when it was saved
- Believing alert function is disabled
Solution
Step 1: Understand testing both input types
XSS can happen via URL parameters or form inputs, so both must be tested with script injections.Step 2: Choose effective test input and verification
Injecting<script>alert(1)</script>is a simple way to check if scripts run. Observing alert confirms vulnerability.Final Answer:
Inject<script>alert(1)</script>in URL and form fields, then check if alert appears -> Option CQuick Check:
Test scripts in URL and forms to find XSS [OK]
- Testing only one input type
- Using unrelated inputs like SQL commands
- Disabling JavaScript instead of testing execution
Ready to prove it?
Pass the test and this pattern moves unknown
