Identify performance bottlenecks in a web application
Preconditions (2)
✅ Expected Result: The performance report clearly shows which parts of the application cause delays, such as slow API responses, heavy scripts, or rendering bottlenecks
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 time import json class PerformanceTest: def setup_method(self): options = webdriver.ChromeOptions() options.set_capability('goog:loggingPrefs', {'performance': 'ALL'}) self.driver = webdriver.Chrome(options=options) self.wait = WebDriverWait(self.driver, 10) def teardown_method(self): self.driver.quit() def get_performance_logs(self): logs = self.driver.get_log('performance') return [json.loads(entry['message'])['message'] for entry in logs] def test_performance_bottlenecks(self): start_time = time.time() self.driver.get('https://example.com') self.wait.until(EC.presence_of_element_located((By.TAG_NAME, 'body'))) load_time = time.time() - start_time assert load_time < 3, f"Page load took too long: {load_time:.2f} seconds" # Simulate user navigation product_link = self.wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a.product-link'))) product_link.click() self.wait.until(EC.presence_of_element_located((By.ID, 'add-to-cart'))) add_to_cart_btn = self.driver.find_element(By.ID, 'add-to-cart') add_to_cart_btn.click() # Check for JS errors logs = self.driver.get_log('browser') js_errors = [log for log in logs if log['level'] == 'SEVERE'] assert not js_errors, f"JavaScript errors found: {js_errors}" # Analyze performance logs for slow network requests perf_logs = self.get_performance_logs() slow_requests = [] for message in perf_logs: if message['method'] == 'Network.responseReceived': request_id = message['params']['requestId'] timing = next((m for m in perf_logs if m['method'] == 'Network.loadingFinished' and m['params']['requestId'] == request_id), None) if timing: duration = timing['params']['timestamp'] - message['params']['timestamp'] if duration > 2: url = message['params']['response']['url'] slow_requests.append({'url': url, 'duration': duration}) assert not slow_requests, f"Slow network requests found: {slow_requests}"
This test uses Selenium with ChromeDriver to open the web application and measure page load time using Python's time module.
Explicit waits ensure elements are ready before interacting, avoiding flaky tests.
JavaScript errors are checked from browser logs to catch client-side issues.
Performance logs from Chrome capture network request timings. The test filters requests taking longer than 2 seconds and fails if any are found.
Setup and teardown methods manage browser lifecycle cleanly.
Now add data-driven testing with 3 different user actions sequences to measure performance for each
import time
def slow_function():
total = 0
for i in range(1000000):
total += i
start = time.time()
slow_function()
end = time.time()
print(round(end - start, 2))
What is the expected output type?import time
start = time.time()
for i in range(1000000):
pass
print(end - start)
What is the error and how to fix it?Pass the test and this pattern moves unknown