QA Graphic

Measuring Page Load Time with Playwright

Learn how to use Playwright to measure how long it takes for a web page to load.

One of its many capabilities PlayWright is measuring page performance metrics, such as load times. In this tutorial, we'll walk through how to use Playwright to record the time it takes for a page to fully load.

Writing the Script

Create a file named measure-load-time.js and add the following code to measure the page load time for a website (e.g., example.com):


const { chromium } = require('playwright');
(async () => {
    // Launch browser
    const browser = await chromium.launch();
    const page = await browser.newPage();
    // Record start time
    const startTime = performance.now();
    // Navigate to the page
    await page.goto('https://example.com');
    // Wait for the page to fully load
    await page.waitForLoadState('load');
    // Record end time
    const endTime = performance.now();
    // Calculate load time
    const loadTime = endTime - startTime;
    console.log(`Page load time: ${loadTime.toFixed(2)} ms`);
    // Close browser
    await browser.close();
})();
            

This script:

  • Launches a Chromium browser using Playwright.
  • Records the start time using performance.now().
  • Navigates to the specified URL.
  • Waits for the load event, indicating the page is fully loaded.
  • Calculates the load time by subtracting the start time from the end time.
  • Outputs the result in milliseconds.

Running the Script

Run the script using Node.js:


node measure-load-time.js
            

You should see output similar to:


Page load time: 1234.56 ms
            

The actual time will vary depending on the website, network conditions, and system performance.

 

Comments

Add Your Comments

Name:
Comment:

 

About

Welcome to Playwright Tips and Tricks, your go-to resource for mastering the art of web automation and testing with Playwright! Whether you're a seasoned developer looking to streamline your workflows or a curious beginner eager to dive into the world of browser automation, this blog is designed with you in mind. Here, I'll share a treasure trove of practical insights, clever hacks, and step-by-step guides to help you harness the full power of Playwright - a modern, open-source tool that's revolutionizing how we interact with web applications.

Schedule

Monday 12 Media
Tuesday 13 QA
Wednesday 14 Pytest
Thursday 15 PlayWright
Friday 16 Macintosh
Saturday 17 Internet Tools
Sunday 18 Misc