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.
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.
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()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Chrome browser window is open and ready | - | PASS |
| 2 | Navigates to 'https://example.com' | Browser loads the example.com homepage | - | PASS |
| 3 | Executes JavaScript to get navigationStart timestamp | Retrieves navigation start time from browser performance timing API | - | PASS |
| 4 | Executes JavaScript to get loadEventEnd timestamp | Retrieves page load event end time from performance timing API | - | PASS |
| 5 | Calculates page load time as loadEventEnd - navigationStart | Page load time calculated in milliseconds | - | PASS |
| 6 | Asserts that page load time is less than 5000 ms | Verifies performance metric is within acceptable limit | Assert load_time < 5000 ms | PASS |
| 7 | Closes the browser and ends the test | Browser window closed | - | PASS |