Pyppeteer not extracting javascript coverage correctly

Viewed 697

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

0 Answers
Related