Here's a comprehensive example demonstrating multimodal testing with Selenium, combining visual, functional, and performance testing approaches:
Scenario: Testing an E-commerce Product Page
python
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.webdriver.common.action_chains import ActionChains
import time
import cv2
import numpy as np
from PIL import Image
import requests
import speedtest
class EcommerceProductPageTest:
def __init__(self):
# Initialize Chrome WebDriver with options
options = webdriver.ChromeOptions()
options.add_argument('--start-maximized')
self.driver = webdriver.Chrome(options=options)
self.wait = WebDriverWait(self.driver, 10)
def test_product_page(self, url):
try:
# 1. Functional Testing
self.driver.get(url)
# Verify page title
assert "Product" in self.driver.title, "Page title doesn't contain 'Product'"
# Find and interact with elements
add_to_cart = self.wait.until(
EC.element_to_be_clickable((By.CSS_SELECTOR, ".add-to-cart-button"))
)
add_to_cart.click()
# Verify cart count updates
cart_count = self.wait.until(
EC.text_to_be_present_in_element((By.CSS_SELECTOR, ".cart-count"), "1")
)
assert cart_count, "Cart count didn't update"
# 2. Visual Testing
self.visual_testing()
# 3. Performance Testing
self.performance_testing()
# 4. Accessibility Testing
self.accessibility_testing()
print("All tests passed successfully!")
except Exception as e:
print(f"Test failed: {str(e)}")
self.driver.save_screenshot('test_failure.png')
finally:
self.driver.quit()
def visual_testing(self):
"""Compare screenshots to detect visual regressions"""
# Take screenshot of the product image
product_image = self.driver.find_element(By.CSS_SELECTOR, ".product-image")
product_image.screenshot('current_product.png')
# Load baseline image (previously approved version)
baseline = cv2.imread('baseline_product.png')
current = cv2.imread('current_product.png')
# Compare images
difference = cv2.subtract(baseline, current)
result = not np.any(difference)
if not result:
print("Visual regression detected in product image!")
# You could add more sophisticated image comparison here
else:
print("Visual test passed - no regressions detected")
def performance_testing(self):
"""Measure page load performance"""
# Measure page load time
load_time = self.driver.execute_script(
"return window.performance.timing.domContentLoadedEventEnd - window.performance.timing.navigationStart;"
)
print(f"Page load time: {load_time}ms")
# Check if load time is within acceptable range
assert load_time < 3000, "Page load time exceeds 3 seconds"
# Test network speed
st = speedtest.Speedtest()
download_speed = st.download() / 1_000_000 # in Mbps
print(f"Network download speed: {download_speed:.2f} Mbps")
assert download_speed > 5, "Network speed is too slow"
def accessibility_testing(self):
"""Basic accessibility checks"""
# Check for alt text on images
images = self.driver.find_elements(By.TAG_NAME, 'img')
for img in images:
alt_text = img.get_attribute('alt')
if not alt_text or alt_text.strip() == "":
print(f"Warning: Image missing alt text - {img.get_attribute('src')}")
# Check color contrast (simplified example)
# In a real implementation, you'd use a proper contrast checking library
product_price = self.driver.find_element(By.CSS_SELECTOR, ".product-price")
bg_color = product_price.value_of_css_property('background-color')
text_color = product_price.value_of_css_property('color')
print(f"Color contrast check: BG={bg_color}, Text={text_color}")
# Here you would implement actual contrast ratio calculation
# Run the test
if __name__ == "__main__":
test = EcommerceProductPageTest()
test.test_product_page("https://example.com/product/123")
Key Multimodal Testing Aspects in This Example:
Functional Testing:
Verifying UI elements and their interactions
Checking business logic (cart updates)
Element visibility and clickability
Visual Testing:
Screenshot comparison to detect visual regressions
Image processing to identify visual differences
Performance Testing:
Page load time measurement
Network speed testing
Performance metrics collection
Accessibility Testing:
Checking for alt text on images
Basic color contrast verification
Cross-Modal Verification:
Combining visual and functional checks
Relating performance metrics to user experience
Additional Multimodal Techniques You Could Add:
Voice Interaction Testing: If the site has voice search capabilities
Mobile Gesture Testing: For touch interfaces
Biometric Testing: Simulating user stress levels based on performance
Eye Tracking Simulation: Testing visual hierarchy and attention points
Haptic Feedback Testing: For mobile or specialized hardware
This example demonstrates how to combine different testing modalities to create a more comprehensive test suite that better represents real user experiences.