question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Cannot waitForResponse in a background page

See original GitHub issue

Context:

  • Playwright Version: [what Playwright version do you use?] 1.16
  • Operating System: [e.g. Windows, Linux or Mac] Mac
  • Node.js version: [e.g. 12.22, 14.6] 14.18
  • Browser: [e.g. All, Chromium, Firefox, WebKit] Chromium

Code Snippet

background.js

chrome.runtime.onMessage.addListener(() => {
  fetch(`https://google.com`).then(console.log).catch(console.error)
})

inject.js

const button = document.createElement('button')
button.innerText = 'Click me'
button.id = `injectedButton`
button.onclick = () => chrome.runtime.sendMessage({})
document.documentElement.appendChild(button)
const { chromium } = require('playwright-chromium')
const path = require('path')
;(async () => {
  const EXTENSION_PATH = path.resolve('./ext')
  const context = await chromium.launchPersistentContext(`userDataDir`, {
    defaultViewport: null,
    headless: false,
    timeout: 0,
    devtools: true,
    args: [
      `--load-extension=${EXTENSION_PATH}`,
      `--disable-extensions-except=${EXTENSION_PATH}`,
    ],
  })

  let [backgroundPage] = context.backgroundPages()
  if (!backgroundPage) {
    backgroundPage = await context.waitForEvent('backgroundpage')
  }

  const page = await context.newPage()
  await page.goto(`chrome://inspect/#extensions`)
  await page.click(`#extensions-list > div > div > div > div.actions > span`)

  await page.goto('https://google.com/', { waitUntil: `networkidle` })

  const [response] = await Promise.all([
    backgroundPage.waitForResponse(() => true, { timeout: 15 * 1000 }),
    page.click('#injectedButton', { timeout: 15 * 1000 }),
  ])
  console.log('response', response)
})()

process.on(`unhandledRejection`, console.log)

Describe the bug

I am trying to test an extension. I want to be able to check that a request is being sent once I perform an action. I used to be able to listen for backgroundPage request and response. But it now times out 😦

The snippets show a playwright script starting chromium with an extension. The extension adds a button. This button triggers a request on click.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
vsravuricommented, Nov 19, 2021

Typo, should be

1.17.0-rc1

0reactions
laurentpayotcommented, Sep 29, 2022

I have the same issue: page.waitForResponse never resolves for urls different from the current page url. I had to to use the following workaround to get a response:

const newPage = await context.newPage()
const response = await newPage.goto(myNonCurrentPageUrl)

Any news about this bug?

Context:

  • Playwright Version: 1.26.1
  • Operating System: Linux (Ubuntu 22.04.1 LTS)
  • Node.js version: 18.4.0
  • Browser: Chromium
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to wait for JavaScript to finish in playwright - Stack Overflow
First I fill the text and then click on tab key to invoke the JavaScript that formats the value in the element. await...
Read more >
Puppeteer documentation - DevDocs
Puppeteer methods might throw errors if they are unable to fulfill a request. For example, page.waitForSelector(selector[, options]) might fail if the selector ...
Read more >
Reliably Send an HTTP Request as a User Leaves a Page
Perhaps the most obvious approach to avoid this problem is, as much as possible, to delay the user action until the request returns...
Read more >
88373 - Chrome extension background page XHR forbidden ...
Does this problem only occur with the Matlab extension, or are the other developer omnibox extensions affected? Do you have any other extensions ......
Read more >
How to increase the timeout for a REST response? - ServiceNow
Here is the code I'm using (testing through Scripts – Background): ... system property to false and use the waitForResponse() method to set...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found