Performance metrics collection in Selenium Python - Build an Automation Script
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By # Setup Chrome options chrome_options = Options() chrome_options.add_argument('--headless=new') # Run headless for faster execution # Setup WebDriver service service = Service() driver = webdriver.Chrome(service=service, options=chrome_options) try: driver.get('https://example.com') # Wait until the document.readyState is 'complete' WebDriverWait(driver, 10).until( lambda d: d.execute_script('return document.readyState') == 'complete' ) # Collect performance timing metrics performance_timing = driver.execute_script('return window.performance.timing') load_event_end = performance_timing.get('loadEventEnd', 0) dom_content_loaded_event_end = performance_timing.get('domContentLoadedEventEnd', 0) assert load_event_end > 0, f"Expected loadEventEnd > 0 but got {load_event_end}" assert dom_content_loaded_event_end > 0, f"Expected domContentLoadedEventEnd > 0 but got {dom_content_loaded_event_end}" finally: driver.quit()
This script uses Selenium WebDriver with Python to open the browser and navigate to 'https://example.com'.
It waits explicitly until the page's document.readyState is 'complete', ensuring the page is fully loaded before collecting performance data.
Using execute_script, it accesses the browser's window.performance.timing object to get timing metrics.
The script asserts that both loadEventEnd and domContentLoadedEventEnd are greater than zero, which means the page loaded successfully.
Finally, the browser is closed properly in a finally block to clean up resources.
Now add data-driven testing with 3 different URLs to collect and verify performance metrics for each.
Practice
Solution
Step 1: Understand performance metrics
Performance metrics focus on timing data related to page loading and resource fetching.Step 2: Identify the purpose in Selenium context
In Selenium, performance metrics help measure loading speed, not layout or input correctness.Final Answer:
To measure how fast a web page and its resources load -> Option CQuick Check:
Performance metrics = loading speed [OK]
- Confusing performance metrics with UI layout checks
- Thinking performance metrics test form validation
- Assuming performance metrics check user actions
Solution
Step 1: Identify correct JavaScript property
The navigation start time is under window.performance.timing.navigationStart.Step 2: Check correct Selenium syntax
Use return keyword and full property path with semicolon for valid JS execution.Final Answer:
driver.execute_script('return window.performance.timing.navigationStart;') -> Option AQuick Check:
Correct JS property and return statement = driver.execute_script('return window.performance.timing.navigationStart;') [OK]
- Omitting 'return' keyword in execute_script
- Using wrong property path without 'timing'
- Missing semicolon causing JS errors
nav_start = driver.execute_script('return window.performance.timing.navigationStart;')
load_end = driver.execute_script('return window.performance.timing.loadEventEnd;')
load_time = load_end - nav_start
print(load_time)Solution
Step 1: Understand the variables
nav_start = 1000, load_end = 3500 as given.Step 2: Calculate load_time
load_time = load_end - nav_start = 3500 - 1000 = 2500.Final Answer:
2500 -> Option AQuick Check:
3500 - 1000 = 2500 [OK]
- Adding instead of subtracting timestamps
- Using wrong variable names
- Confusing loadEventEnd with other timing values
nav_start = driver.execute_script('return window.performance.timing.navigationStart')
load_end = driver.execute_script('return window.performance.timing.loadEventEnd')
load_time = nav_start - load_end
print(load_time)Solution
Step 1: Check subtraction logic
Load time should be load_end - nav_start, not nav_start - load_end.Step 2: Understand impact of reversed subtraction
Reversed order results in negative load time, which is incorrect.Final Answer:
The subtraction order is reversed, causing negative load time -> Option DQuick Check:
Correct subtraction order = load_end - nav_start [OK]
- Reversing subtraction causing negative results
- Assuming missing semicolons cause errors in this context
- Thinking execute_script can't get performance data
Solution
Step 1: Verify JavaScript property paths
Performance timing properties are under window.performance.timing with return keyword.Step 2: Check calculation logic
Total load time is loadEventEnd minus navigationStart, so load - nav is correct.Final Answer:
nav = driver.execute_script('return window.performance.timing.navigationStart;') resp = driver.execute_script('return window.performance.timing.responseStart;') load = driver.execute_script('return window.performance.timing.loadEventEnd;') total_load = load - nav print(total_load) -> Option BQuick Check:
Correct property path and subtraction order = nav = driver.execute_script('return window.performance.timing.navigationStart;') resp = driver.execute_script('return window.performance.timing.responseStart;') load = driver.execute_script('return window.performance.timing.loadEventEnd;') total_load = load - nav print(total_load) [OK]
- Using wrong property paths without 'timing'
- Calculating load time with wrong subtraction order
- Omitting 'return' in execute_script calls
Ready to prove it?
Pass the test and this pattern moves unknown
