1. The Testing Sandbox (Application Under Test)
Test your external Selenium scripts against these elements. Use the Code Reference section to quickly see the Python code needed for interaction.
Login & Search Test (ID/Name Locators)
Ready to login.
Locators: `id="username-field"`, `name="password"`, `id="login-button"`
Search ready.
Locators: `id="search-box"`, `data-qa="search-input"`
Data Selection & Checkbox/Radio (CSS Selector)
Options and Toggles:
Locators: `id="country-select"`, `[data-qa='option-checkbox']`, `[name='group-1']`
Table Data Extraction (XPath Locators)
| ID | Product | Price | Status |
|---|---|---|---|
| 101 | Laptop X | $1200.00 | Out of Stock |
| 102 | Mouse Pro | $45.50 | Available |
Locators: `id="data-table"`, XPath: `//table[@id='data-table']//tr[2]/td[2]` (for Mouse Pro)
Drag, Drop (Multiple Targets), and Slider Interaction (ActionChains)
ID: droppable-1
ID: droppable-2
Waiting for drop.
Value: 50
Locators: `id="volume-slider"`
Alert/Modal Simulation (Alert, Confirm, Prompt)
Alert Action Status: Ready
File Upload Simulation
No file selected.
Locators: `id="file-upload"`, `data-qa="file-input"`
iFrame/Frame Switching Test
Control must switch back to main page.
Locators: `id="test-iframe"`, XPath: `//iframe[@name='iframeName']`
Mouse Actions: Double & Right Click
No action yet.
Locators: `id="double-click-btn"`, `id="right-click-btn"`
Navigation History & External Link Test
Locators: `id="external-link-2"`, `id="download-link"`
2. Code Reference (Pytest & Selenium WebDriver)
Copy and paste these Pytest code examples to test the Sandbox elements on the left.
Code Snippet 1: Login Form & Search (ID/Name)
from selenium import webdriver
from selenium.webdriver.common.by import By
def test_login_and_search(driver):
# Locate elements by ID and Name
driver.find_element(By.ID, "username-field").send_keys("testuser")
driver.find_element(By.NAME, "password").send_keys("testpass")
driver.find_element(By.ID, "login-button").click()
# Assert login success message (assuming server validates)
login_status = driver.find_element(By.ID, "login-status").text
assert "Login successful" in login_status
# Search box test using data-qa attribute
search_input = driver.find_element(By.CSS_SELECTOR, "[data-qa='search-input']")
search_input.send_keys("Selenium Webdriver Tutorial")
driver.find_element(By.ID, "search-button").click()
search_result = driver.find_element(By.ID, "search-result").text
assert "Searching for: Selenium Webdriver Tutorial" in search_result
Code Snippet 2: Select, Checkbox, Radio, Date
from selenium.webdriver.support.ui import Select
def test_form_data_selection(driver):
# 1. Dropdown (Select Class)
select_element = driver.find_element(By.ID, "country-select")
select = Select(select_element)
select.select_by_value("ind") # Selects 'India'
assert select.first_selected_option.text == "India"
# 2. Checkbox (CSS Selector)
checkbox = driver.find_element(By.CSS_SELECTOR, "[data-qa='option-checkbox']")
if not checkbox.is_selected():
checkbox.click()
assert checkbox.is_selected()
# 3. Radio Buttons (Name attribute)
# This demonstrates locating a specific radio button by its value
radio_blue = driver.find_element(By.CSS_SELECTOR, "input[name='group-1'][value='Blue']")
radio_blue.click()
assert radio_blue.is_selected()
# 4. Date Picker
date_field = driver.find_element(By.ID, "date-input")
# Note: send_keys is the standard way to input dates in Selenium
date_field.send_keys("2026-01-15")
assert date_field.get_attribute("value") == "2026-01-15"
Code Snippet 3: Table Data Extraction (XPath)
def test_table_data_extraction(driver):
# Locate a specific cell using relative XPath
# Target: The price of 'Mouse Pro' ($45.50) in the second row
price_cell_xpath = "//table[@id='data-table']//tbody/tr[2]/td[3]"
price_element = driver.find_element(By.XPATH, price_cell_xpath)
assert price_element.text == "$45.50"
# Locate all 'Product' names using a generic XPath
product_names = driver.find_elements(By.XPATH, "//td[@data-qa='row-product']")
# Assert all products in the list
assert [p.text for p in product_names] == ["Laptop X", "Mouse Pro"]
# Locate 'Out of Stock' status using attribute filtering
out_of_stock_cell = driver.find_element(By.XPATH, "//td[text()='Out of Stock']")
assert out_of_stock_cell.is_displayed()
Code Snippet 4: Drag and Drop (Target 1) & Slider
from selenium.webdriver.common.action_chains import ActionChains
def test_drag_and_drop_success(driver):
actions = ActionChains(driver)
# 1. Drag and Drop to the SUCCESS target
draggable = driver.find_element(By.CSS_SELECTOR, "[data-qa='draggable-element']")
droppable_target_1 = driver.find_element(By.CSS_SELECTOR, "[data-qa='droppable-target-1']")
# Drag the draggable element onto the first droppable target
actions.drag_and_drop(draggable, droppable_target_1).perform()
# Assert successful drop (check status element)
drag_status = driver.find_element(By.ID, "drag-status").text
assert "Dropped successfully onto Target 1" in drag_status
# 2. Slider Control (Remains the same)
slider = driver.find_element(By.ID, "volume-slider")
actions.click_and_hold(slider).move_by_offset(-30, 0).release().perform()
slider_value = driver.find_element(By.ID, "slider-value").text
print(f"New Slider Value: {slider_value}")
assert slider_value != "Value: 50"
Code Snippet 5: File Upload Simulation
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import os # Necessary for file path
def test_file_upload(driver):
# 2. File Upload Simulation
upload_input = driver.find_element(By.ID, "file-upload")
# CRITICAL: Create a dummy file path (this file must exist on your local machine)
# E.g., create a file named 'test_file.txt' in the same directory as your script
dummy_file_path = os.path.abspath("test_file.txt")
# Send the absolute file path to the input element
upload_input.send_keys(dummy_file_path)
# Assert status change on the page
file_status = driver.find_element(By.ID, "file-status").text
assert "File selected: test_file.txt" in file_status
Code Snippet 6: iFrame Switching
# Note: You can switch by ID, Name, Index, or WebElement
def test_iframe_switching(driver):
# 1. Switch to the iframe using its Name attribute
driver.switch_to.frame("iframeName")
# 2. Locate an element and interact with it inside the frame
frame_input = driver.find_element(By.ID, "frame-input")
frame_input.send_keys("Text sent from Pytest!")
# Assert text was sent correctly inside the frame
assert "Text sent from Pytest!" in frame_input.get_attribute("value")
# 3. Switch back to the main (default) content
driver.switch_to.default_content()
# 4. Assert an element on the main page (to confirm switch back)
parent_status = driver.find_element(By.ID, "iframe-status-parent")
parent_status.text = "Control is back to the main page."
assert "Control is back" in parent_status.text
Code Snippet 7: Double & Right Click
from selenium.webdriver.common.action_chains import ActionChains
def test_mouse_actions(driver):
actions = ActionChains(driver)
double_click_btn = driver.find_element(By.ID, "double-click-btn")
right_click_btn = driver.find_element(By.ID, "right-click-btn")
mouse_status = driver.find_element(By.ID, "mouse-action-status")
# 1. Test Double Click
actions.double_click(double_click_btn).perform()
assert "Double Click Performed!" in mouse_status.text
# 2. Test Right Click (Context Click)
actions.context_click(right_click_btn).perform()
assert "Right Click (Context Click) Performed!" in mouse_status.text
Code Snippet 8: Browser History & External Link Test
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def test_navigation_and_download(driver):
# 1. Test Navigation History (Internal Link)
nav_link = driver.find_element(By.ID, "nav-link-1")
nav_link.click()
assert "#target-page" in driver.current_url
driver.back()
assert "#target-page" not in driver.current_url
driver.forward()
assert "#target-page" in driver.current_url
# 2. Test External Link (Requires Window Switching)
external_link = driver.find_element(By.ID, "external-link-2")
current_window = driver.current_window_handle
external_link.click()
# Wait for new window/tab to open and switch
WebDriverWait(driver, 10).until(EC.number_of_windows_to_be(2))
new_window = [window for window in driver.window_handles if window != current_window][0]
driver.switch_to.window(new_window)
# Assert URL in the new tab
assert "alfuzayel.netlify.app" in driver.current_url
# Close the new tab and switch back
driver.close()
driver.switch_to.window(current_window)
assert "FuzayelQA" in driver.title # Assert we are back on the main page
# 3. Test Download Link
download_link = driver.find_element(By.ID, "download-link")
download_link.click()
assert download_link.get_attribute("download") == "test_download.txt"
Code Snippet 9: Alert Handling (Accept, Dismiss, Send Keys)
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def test_alert_handling(driver):
# 1. Simple Alert (Accept / OK)
driver.find_element(By.ID, "show-simple-alert").click()
WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.ID, "simple-modal")))
# Simulate: driver.switch_to.alert.accept()
driver.find_element(By.ID, "simple-ok").click()
alert_status = driver.find_element(By.ID, "alert-status").text
assert "Simple Alert: Accepted" in alert_status
# 2. Confirmation Alert (Dismiss / Cancel)
driver.find_element(By.ID, "show-confirm-alert").click()
WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.ID, "confirm-modal")))
# Simulate: driver.switch_to.alert.dismiss()
driver.find_element(By.ID, "confirm-cancel").click()
alert_status = driver.find_element(By.ID, "alert-status").text
assert "Confirmation Alert: Dismissed" in alert_status
# 3. Prompt Alert (Send Keys and Accept / OK)
test_input = "Fuzayel Automation"
driver.find_element(By.ID, "show-prompt-alert").click()
WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.ID, "prompt-modal")))
# Simulate: driver.switch_to.alert.send_keys(test_input)
driver.find_element(By.ID, "prompt-input").send_keys(test_input)
# Simulate: driver.switch_to.alert.accept()
driver.find_element(By.ID, "prompt-ok").click()
alert_status = driver.find_element(By.ID, "alert-status").text
assert f"Prompt Alert: Accepted with text: {test_input}" in alert_status