2

When I'm using pyppeteer for extracting js coverage, there is missing some parts of the javascript code.

What I'm doing is the following:

import asyncio
import json
import os
from pyppeteer import launch


def process_coverage(coverage):

    resources = []

    for entry in coverage:
        url = entry['url']
        text = entry['text']

        used_resource = ''

        for interval in entry['ranges']:
            start = interval['start']
            end = interval['end']
            used_resource = used_resource + text[start:end]

        resources.append(used_resource)

    return resources

def save_to_new_file(path, content):
    file = open(path, 'w') 
    file.write(content) 
    file.close()

async def main():

    browser = await launch()
    page = await browser.newPage()

    await page.coverage.startJSCoverage()

    await page.goto('http://127.0.0.1:8000/', option={'waitUntil': 'load'})
    await page.setViewport({ 'width': 500, 'height': 500 })
    await page.setViewport({ 'width': 1920, 'height': 1080 })

    js_coverage = await page.coverage.stopJSCoverage()
    js_resources = process_coverage(js_coverage)

    for idx, resource in enumerate(js_resources):
        save_to_new_file(f'{idx}.txt', resource)

    await page.close()
    await browser.close()

asyncio.get_event_loop().run_until_complete(main())

For example, some screenshots are left here (what it was extracted with the pyppeteer code vs the coverage report that I ran on the chrome browser):

enter image description here

enter image description here

Another example:

enter image description here

enter image description here

As shown on the screenshots, there is code that is missing and on the chrome coverage report shows that was used. Does anyone know what am I missing? Am I missing some configuration?


Edit: I also have found a link in which they have the same problem. The thing is that I don't understand the approach they have taken to solve the issue: https://github.com/puppeteer/puppeteer/issues/1054

Joaco Terniro
  • 115
  • 1
  • 2
  • 13
  • I am not sure what are you trying to achieve? How do you write the coverage from the puppeteer case? I wrote the next article https://dev.to/storenth/puppeteer-mocha-upgrade-your-implementation-code-with-coverage-22 and report some issues https://github.com/istanbuljs/puppeteer-to-istanbul/issues/21 hope that help. – storenth Jun 11 '20 at 07:51
  • What I'm trying to to achieve is to remove the unused css of the files a website has. The problem is that, when removing the css based on the coverage Puppeteer retrieves to me, it extracts some parts that are still useful, leaving the code with sintax errors. – Joaco Terniro Jun 11 '20 at 11:48

0 Answers0