0
0
Selenium Pythontesting~10 mins

Performance metrics collection in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test collects performance metrics from a web page using Selenium WebDriver in Python. It verifies that the page load time is within an acceptable limit.

Test Code - unittest
Selenium Python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time
import unittest

class PerformanceMetricsTest(unittest.TestCase):
    def setUp(self):
        service = Service()
        self.driver = webdriver.Chrome(service=service)
        self.driver.implicitly_wait(10)

    def test_page_load_performance(self):
        self.driver.get('https://example.com')
        navigation_start = self.driver.execute_script('return window.performance.timing.navigationStart')
        load_event_end = self.driver.execute_script('return window.performance.timing.loadEventEnd')
        load_time = load_event_end - navigation_start
        print(f'Page load time: {load_time} ms')
        self.assertLess(load_time, 5000, f'Page load time is too high: {load_time} ms')

    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main()
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensChrome browser window is open and ready-PASS
2Navigates to 'https://example.com'Browser loads the example.com homepage-PASS
3Executes JavaScript to get navigationStart timestampRetrieves navigation start time from browser performance timing API-PASS
4Executes JavaScript to get loadEventEnd timestampRetrieves page load event end time from performance timing API-PASS
5Calculates page load time as loadEventEnd - navigationStartPage load time calculated in milliseconds-PASS
6Asserts that page load time is less than 5000 msVerifies performance metric is within acceptable limitAssert load_time < 5000 msPASS
7Closes the browser and ends the testBrowser window closed-PASS
Failure Scenario
Failing Condition: Page load time exceeds 5000 milliseconds
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify about the web page?
AThe page loads within 5 seconds
BThe page title is correct
CThe page contains a specific button
DThe page URL is redirected
Key Result
Always measure and assert performance metrics like page load time to catch slow user experiences early.