microsoft/playwright-test

Bug: importing Worker from playwright fails - _playwright is not defined

Meir017 opened this issue · 1 comments

the following code works

import * as playwright from 'playwright';
import { folio as baseFolio } from '@playwright/test';

const builder = baseFolio.extend<{ worker: playwright.Worker }, {}, {}>();

builder.worker.init(async ({ page }, runTest) => {
    await page.goto('https://en.wikipedia.org/wiki/Main_Page');
    await page.route(url => url.href.includes('/sw.js'), route => route.fulfill({
        body: 'var a = 6',
        contentType: 'text/javascript',
        status: 200
    }));
    const [worker] = await Promise.all([
        page.waitForEvent('worker'),
        page.evaluate(() => {
            myWorker = new Worker('https://en.wikipedia.org/sw.js')
        })
    ]);

    await runTest(worker);
});

const folio = builder.build();

const { it, describe } = folio;

describe('worker', function () {
    it('should work', async ({ worker }) => {

    });
});

when importing the specific type from playwright an error is thrown

this solves the issue but I rather import just the used types and not prefix everything with playwrighs's alias

--import { Worker } from 'playwright';
++import * as playwright from 'playwright';
import { folio as baseFolio } from '@playwright/test';

--const builder = baseFolio.extend<{ worker: Worker }, {}, {}>();
++const builder = baseFolio.extend<{ worker: playwright.Worker }, {}, {}>();

the error

npx folio -p browserName=chromium

Running 1 test using 1 worker
  1) sample.spec.ts:32:5 › worker should work ======================================================
     browserName=chromium, headful=undefined, slowMo=0, video=false, screenshotOnFailure=false

    page.evaluate: Evaluation failed: ReferenceError: _playwright is not defined
        at eval (eval at evaluate (:303:29), <anonymous>:2:5)
        at UtilityScript.evaluate (<anonymous>:305:22)
        at UtilityScript.<anonymous> (<anonymous>:1:44)

      17 |     const [worker] = await Promise.all([
      18 |         page.waitForEvent('worker'),
    > 19 |         page.evaluate(() => {
         |              ^
      20 |             myWorker = new Worker('https://en.wikipedia.org/sw.js')
      21 |         })
      22 |     ]);

    rror: 
        at Object.captureStackTrace (D:\Playground\Playground.Js\node_modules\playwright\lib\utils\stackTrace.js:48:19)
        at Connection.sendMessageToServer (D:\Playground\Playground.Js\node_modules\playwright\lib\client\connection.js:69:48)
        at Proxy.<anonymous> (D:\Playground\Playground.Js\node_modules\playwright\lib\client\channelOwner.js:64:61)
        at D:\Playground\Playground.Js\node_modules\playwright\lib\client\frame.js:179:42
        at Frame._wrapApiCall (D:\Playground\Playground.Js\node_modules\playwright\lib\client\channelOwner.js:77:34)
        at Frame.evaluate (D:\Playground\Playground.Js\node_modules\playwright\lib\client\frame.js:178:21)
        at D:\Playground\Playground.Js\node_modules\playwright\lib\client\page.js:385:60
        at Page._attributeToPage (D:\Playground\Playground.Js\node_modules\playwright\lib\client\page.js:231:20)
        at Page.evaluate (D:\Playground\Playground.Js\node_modules\playwright\lib\client\page.js:385:21)
        at Object.fn (D:\Playground\Playground.Js\play\sample.spec.ts:19:14)

  1 failed
    sample.spec.ts:32:5 › worker should work =======================================================
    browserName=chromium, headful=undefined, slowMo=0, video=false, screenshotOnFailure=false

Closing as part of the triage since the new test-runner is quite different. Please create a new issue if its still persistent.