abhinaba-ghosh / playwright-lighthouse

:performing_arts:: Playwright Lighthouse Audit
https://www.npmjs.com/package/playwright-lighthouse
MIT License
250 stars 28 forks source link

An option to pass url instead of page #24

Closed grybykm closed 2 years ago

grybykm commented 2 years ago

Concern

Opening a page one extra time more and keeping it opened consumes 2 times more resources. It's especially noticeable when running tests in parallel.

There is no real need to pass page to playAudit. It's used to perform some validations and get the url.

Solution

An option to pass url string instead of page.

ex: if page is string use it as url and skip all the checks.

I can raise the PR if you are ok with this.

Thank you

abhinaba-ghosh commented 2 years ago

Hi @grybykm this makes a lot of sense.

Kindly raise a PR. This will be a breaking change though

grybykm commented 2 years ago

It should not be a breaking change I believe unless we want to drop page support completely.

Btw, I'm setting up lighthouse like this

import os from 'os'
import path from 'path'
import { chromium, test as base } from '@playwright/test'
import type { BrowserContext } from '@playwright/test'
import getPort from 'get-port' // version ^5.1.1 due to issues with imports in playwright 1.20.1

export const lighthouseTest = base.extend<{ context: BrowserContext }, { port: number }>({
    port: [
        async ({}, use) => {
            // Assign a unique port for each playwright worker to support parallel tests
            const port = await getPort()
            await use(port)
        },
        { scope: 'worker' },
    ],

    context: [
        async ({ port, launchOptions }, use) => {
            const context = await chromium.launchPersistentContext(path.join(os.tmpdir(), 'pw', `${Math.random()}`.replace('.', '')), {
                args: [...(launchOptions.args || []), `--remote-debugging-port=${port}`],
            })

            // apply state previously saved in the the `globalSetup`
            await context.addCookies(require('../../state-chrome.json').cookies)

            await use(context)
            await context.close()
        },
        { scope: 'test' },
    ],
})

ref to globalSetup https://playwright.dev/docs/test-auth

Additionally, I use global teardown to cleanup the tmp folder

import os from 'os'
import path from 'path'
import rimraf from 'rimraf'

function globalTeardown() {
    rimraf(path.join(os.tmpdir(), 'pw'), console.log)
}

export default globalTeardown
abhinaba-ghosh commented 2 years ago

@grybykm looks great. Let's do this.