Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get "Coverage" data out from the Chrome Dev Tools

I am using the Coverage tab at my Chrome Dev Tools and I have a really big file and after playing a lot with Coverage it's clear enough that only 15% enough of my CSS code is being used (I simulated button presses, hover menus...).

The problem is getting hat 15% of code OUT of the Coverage tab. I cant believe the Devs behind this really nice feature didnt think an easy way for the end user copy only the green part of the code. Check image attached.

Do you have any idea how I could do that? I read something about using Puppeteers but it requires lots of preparation. On latest Canary version it looks like I can export a JSON but it would require some time to code a parser to that JSON in order to extract only the needed part.

enter image description here

like image 777
Samul Avatar asked Feb 03 '19 13:02

Samul


People also ask

How do I see DevTools coverage in Chrome?

In Chrome's Dev Tools (Command+Option+C on Mac, Control+Shift+C on Windows/Linux, or right-click on the page and choose “Inspect”), select the “Sources” tab, and if “Coverage” isn't a displayed tab at the bottom, select it using the three vertical dots to the left of those tabs.

Where is coverage in dev tools?

Open the Command Menu. Start typing coverage , select the Show Coverage command, and then press Enter . The Coverage tool opens in the Drawer.

How do I read DevTools performance in Chrome?

To access the Performance tab, navigate to the website you want to profile, then open Chrome DevTools by right-clicking and selecting Inspect. Select the Performance tab inside Chrome DevTools. The easiest way to capture a performance profile is by clicking the Start profiling and reload page icon.


2 Answers

Thanks to an article by Phillip Kriegel (https://www.philkrie.me/2018/07/04/extracting-coverage.html) I managed to setup Puppeteer to extract the coverage CSS from a URL and output that CSS into a file.

Here's how to do it:

Step 1: Install node.js globally

Step 2: Create a folder on your desktop

Step 3: Inside the folder install the Node Package Manager (NPM) and the Puppeteer node module

Step 4: Create a JavaScript file inside the folder, name it coverage.js

Step 5: Put this code inside that js file:

const puppeteer = require('puppeteer');
// Include to be able to export files w/ node
const fs = require('fs');

(async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();

    // Begin collecting CSS coverage data
    await Promise.all([
        page.coverage.startCSSCoverage()
    ]);

    // Visit desired page
    await page.goto('https://www.google.com');
  
    //Stop collection and retrieve the coverage iterator
    const cssCoverage = await Promise.all([
        page.coverage.stopCSSCoverage(),
    ]);

    //Investigate CSS Coverage and Extract Used CSS
    const css_coverage = [...cssCoverage];
    let css_used_bytes = 0;
    let css_total_bytes = 0;
    let covered_css = "";
    
    for (const entry of css_coverage[0]) {
        
        css_total_bytes += entry.text.length;
        console.log(`Total Bytes for ${entry.url}: ${entry.text.length}`);

        for (const range of entry.ranges){
            css_used_bytes += range.end - range.start - 1;
            covered_css += entry.text.slice(range.start, range.end) + "\n";
        }       
    }

    console.log(`Total Bytes of CSS: ${css_total_bytes}`);
    console.log(`Used Bytes of CSS: ${css_used_bytes}`);
    fs.writeFile("./exported_css.css", covered_css, function(err) {
        if(err) {
            return console.log(err);
        }
        console.log("The file was saved!");
    }); 

    await browser.close();
})();

Step 6: BE SURE TO REPLACE the URL at this point in the code await page.goto('https://www.google.com'); with your desired URL

Step 7: In the command line tool (Git Bash) type node coverage.js

A file called exported_css.css will be created, it will contain all your coverage CSS for the URL you set in the code.

CAVEAT: This will extract the coverage CSS from ALL the CSS assets that are loaded from the URL you set. You will then have to further optimize that CSS (not covered in this example).

like image 51
Superfein Avatar answered Oct 19 '22 02:10

Superfein


Open Chrome Tab --> Inspect Element (F12) --> Press Escape button enter image description here

like image 1
Surya R Praveen Avatar answered Oct 19 '22 01:10

Surya R Praveen