0
0
Selenium Pythontesting~15 mins

Performance metrics collection in Selenium Python - Build an Automation Script

Choose your learning style9 modes available
Collect and verify page load performance metrics
Preconditions (2)
Step 1: Open the browser and navigate to 'https://example.com'
Step 2: Wait until the page is fully loaded
Step 3: Collect the page load performance metrics using the browser's performance API
Step 4: Verify that the 'loadEventEnd' timing is greater than 0
Step 5: Verify that the 'domContentLoadedEventEnd' timing is greater than 0
✅ Expected Result: The performance metrics 'loadEventEnd' and 'domContentLoadedEventEnd' are collected and both have values greater than zero, indicating the page loaded successfully.
Automation Requirements - Selenium with Python
Assertions Needed:
'loadEventEnd' > 0
'domContentLoadedEventEnd' > 0
Best Practices:
Use explicit waits to ensure page load completion
Use Selenium's execute_script method to access performance timing
Use clear and maintainable locators and code structure
Automated Solution
Selenium Python
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.

Common Mistakes - 3 Pitfalls
{'mistake': 'Not waiting for the page to fully load before collecting performance metrics', 'why_bad': 'Performance metrics may be incomplete or zero if collected too early, causing false test failures.', 'correct_approach': "Use explicit waits to check that document.readyState is 'complete' before collecting metrics."}
{'mistake': 'Using hardcoded sleep (time.sleep) instead of explicit waits', 'why_bad': 'Hardcoded sleeps slow down tests and are unreliable if page load times vary.', 'correct_approach': "Use Selenium's WebDriverWait with conditions for better reliability and speed."}
{'mistake': 'Accessing performance metrics with incorrect JavaScript or wrong keys', 'why_bad': 'This leads to errors or missing data, causing test failures or incorrect results.', 'correct_approach': "Use correct JavaScript to access window.performance.timing and check keys like 'loadEventEnd'."}
Bonus Challenge

Now add data-driven testing with 3 different URLs to collect and verify performance metrics for each.

Show Hint