0
0
Testing Fundamentalstesting~15 mins

Identifying performance bottlenecks in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Identify performance bottlenecks in a web application
Preconditions (2)
Step 1: Open the web application homepage in a browser
Step 2: Start the performance recording tool in the browser DevTools
Step 3: Perform typical user actions such as loading the homepage, navigating to a product page, and adding a product to the cart
Step 4: Stop the performance recording after these actions
Step 5: Analyze the recorded timeline for long tasks, slow network requests, and rendering delays
Step 6: Identify the slowest operations or resources causing delays
✅ Expected Result: The performance report clearly shows which parts of the application cause delays, such as slow API responses, heavy scripts, or rendering bottlenecks
Automation Requirements - Selenium with Python
Assertions Needed:
Verify page load time is under acceptable threshold (e.g., 3 seconds)
Verify no network request takes longer than a set limit (e.g., 2 seconds)
Verify no JavaScript errors occur during user actions
Best Practices:
Use explicit waits to handle dynamic content
Use browser performance logs to capture network and timing data
Structure code with setup and teardown methods for browser management
Automated Solution
Testing Fundamentals
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import json

class PerformanceTest:
    def setup_method(self):
        options = webdriver.ChromeOptions()
        options.set_capability('goog:loggingPrefs', {'performance': 'ALL'})
        self.driver = webdriver.Chrome(options=options)
        self.wait = WebDriverWait(self.driver, 10)

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

    def get_performance_logs(self):
        logs = self.driver.get_log('performance')
        return [json.loads(entry['message'])['message'] for entry in logs]

    def test_performance_bottlenecks(self):
        start_time = time.time()
        self.driver.get('https://example.com')
        self.wait.until(EC.presence_of_element_located((By.TAG_NAME, 'body')))
        load_time = time.time() - start_time

        assert load_time < 3, f"Page load took too long: {load_time:.2f} seconds"

        # Simulate user navigation
        product_link = self.wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a.product-link')))
        product_link.click()
        self.wait.until(EC.presence_of_element_located((By.ID, 'add-to-cart')))

        add_to_cart_btn = self.driver.find_element(By.ID, 'add-to-cart')
        add_to_cart_btn.click()

        # Check for JS errors
        logs = self.driver.get_log('browser')
        js_errors = [log for log in logs if log['level'] == 'SEVERE']
        assert not js_errors, f"JavaScript errors found: {js_errors}"

        # Analyze performance logs for slow network requests
        perf_logs = self.get_performance_logs()
        slow_requests = []
        for message in perf_logs:
            if message['method'] == 'Network.responseReceived':
                request_id = message['params']['requestId']
                timing = next((m for m in perf_logs if m['method'] == 'Network.loadingFinished' and m['params']['requestId'] == request_id), None)
                if timing:
                    duration = timing['params']['timestamp'] - message['params']['timestamp']
                    if duration > 2:
                        url = message['params']['response']['url']
                        slow_requests.append({'url': url, 'duration': duration})

        assert not slow_requests, f"Slow network requests found: {slow_requests}"

This test uses Selenium with ChromeDriver to open the web application and measure page load time using Python's time module.

Explicit waits ensure elements are ready before interacting, avoiding flaky tests.

JavaScript errors are checked from browser logs to catch client-side issues.

Performance logs from Chrome capture network request timings. The test filters requests taking longer than 2 seconds and fails if any are found.

Setup and teardown methods manage browser lifecycle cleanly.

Common Mistakes - 4 Pitfalls
Using implicit waits instead of explicit waits
Not checking browser logs for JavaScript errors
Hardcoding long sleep times instead of using waits
Ignoring network request timings in performance analysis
Bonus Challenge

Now add data-driven testing with 3 different user actions sequences to measure performance for each

Show Hint