0
0
Selenium Pythontesting~10 mins

Network log capture in Selenium Python - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test opens a browser, navigates to a website, captures network logs during the page load, and verifies that the network log contains expected entries.

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

# Setup Chrome options to enable performance logging
chrome_options = Options()
chrome_options.set_capability('goog:loggingPrefs', {'performance': 'ALL'})

# Initialize WebDriver
service = Service()
driver = webdriver.Chrome(service=service, options=chrome_options)

try:
    # Navigate to example website
    driver.get('https://example.com')
    time.sleep(3)  # Wait for network activity

    # Capture performance logs
    logs = driver.get_log('performance')

    # Filter network request logs
    network_logs = []
    for entry in logs:
        message = json.loads(entry['message'])['message']
        if message['method'] == 'Network.requestWillBeSent':
            network_logs.append(message['params']['request']['url'])

    # Assert that the main page URL is in the network logs
    assert 'https://example.com/' in network_logs

finally:
    driver.quit()
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Initialize Chrome WebDriver with performance logging enabledChrome browser instance is ready with logging preferences set-PASS
2Navigate to 'https://example.com'Browser loads the example.com homepage-PASS
3Wait 3 seconds to allow network requests to completePage and network activity stabilize-PASS
4Retrieve performance logs from the browserCollected raw performance log entries-PASS
5Filter logs to extract URLs from 'Network.requestWillBeSent' eventsList of requested URLs during page load is prepared-PASS
6Assert that 'https://example.com/' is in the network logsVerified that main page URL was requestedassert 'https://example.com/' in network_logsPASS
7Close the browserBrowser instance closed-PASS
Failure Scenario
Failing Condition: The network logs do not contain the main page URL 'https://example.com/'
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test check in the network logs?
AThat the main page URL 'https://example.com/' was requested
BThat the page title is correct
CThat the browser window is maximized
DThat the page contains a specific button
Key Result
Enabling browser performance logging in Selenium allows capturing network requests, which helps verify that expected resources are loaded during a test.