microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
66.64k stars 3.65k forks source link

[BUG] chrome.runtime is undefined when testing chrome extension #13529

Closed josteph closed 2 years ago

josteph commented 2 years ago

Context:

Code Snippet

import { BrowserContext, chromium, Page } from 'playwright';

const setup = async () => {
  const browser = await chromium.launch({
     args: [`--disable-extensions-except=${pathToExtension}`, `--load-extension=${pathToExtension}`],

     // Extensions only work in Chrome / Chromium in non-headless mode.
     headless: false,
  });

  const context = await browser.newContext();
  const page = await context.newPage();

  await page.goTo('https://www.google.com');

  return [browser, page];
};

let browser: BrowserContext
let page: Page

test.beforeAll(async () => {
  [browser, page] = await setup()
})

test.afterAll(async () => {
  if (browser) {
    await browser.close()
  }
})

test('hello world', async () => {
  // this button will trigger a function that calls `chrome.runtime.sendMessage`
  await page.locator('text=Trigger').click()
});

Describe the bug

Screen Shot 2022-04-13 at 17 59 19

It seems the chrome.runtime will not be present unless a new tab is opened manually.

https://user-images.githubusercontent.com/13672022/163166378-f06833c0-98c7-4edc-ad31-8d9e7978db6d.mov

Also, if a new tab opened due to page event called by window.open('https://www.google.com', '_blank'), the chrome.runtime will also be undefined too.

josteph commented 2 years ago

For now I can trigger reload the page for workaround:

await Promise.all([
    page.waitForURL(url, {
      waitUntil: 'networkidle'
    }),

    // Trigger reload as workaround
    page.reload({
      waitUntil: 'networkidle'
    })
])

// `chrome.runtime` will be defined after reload
pavelfeldman commented 2 years ago

Why was this issue closed?

We are prioritizing the bugs based on the upvotes, recency and our ability to act. It looks like this issue only has a handful of upvotes, has not been touched recently and/or we lack sufficient feedback to act on it. We are closing issues like this one to keep our bug database maintainable. Please feel free to open a new issue and link this one to it if you think this is a mistake.

derevnjuk commented 3 months ago

@pavelfeldman I'm still encountering the same issue, even using v1.45.3 and Chrome v127. Do you have any ideas why this might happen? Unfortunately, using Page.reload is not a option in my case.

josteph commented 3 months ago

Closing an issue using vote numbers as excuse does not make the community any better, it's similar to rejecting public's opinion. Considering that the issue was closed without any effort to reproduce while the steps are clearly stated in the issue description.