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.

puppeteer error "Cannot find context with specified id undefined"

See original GitHub issue

What are you trying to achieve?

  1. open a page
  2. click a link by click
  3. see text from new page

What do you get instead?

Got this error and test failed.

Account - basic flow: login, navigation to pages --
 test
 • I am on page "https://github.com/"
 • I click "About"
 • I see "is how people build software"
 ✖ FAILED in 3314ms


-- FAILURES:

  1) Account - basic flow: login, navigation to pages
       test :
     Protocol error (Runtime.callFunctionOn): Cannot find context with specified id undefined

  Scenario Steps:

  - I.see("is how people build software") at Test.Scenario (account-basic_test.js:10:5)
  - I.click("About") at Test.Scenario (account-basic_test.js:9:5)
  - I.amOnPage("https://github.com/") at Test.Scenario (account-basic_test.js:8:5)



  Run with --verbose flag to see NodeJS stacktrace```

But the captured failed screenshot is correct.

> Provide test source code if related

```js
  Scenario('test ', (I) => {
    I.amOnPage('https://github.com/');
    I.click('About');
    I.see('is how people build software');
  });

Details

  • CodeceptJS version: 1.14
  • NodeJS Version: 9.4
  • Operating System: macOS
  • puppeteer
  • Configuration file:
{
  "tests": "./*_test.js",
  "timeout": 10000,
  "output": "./output",
  "helpers": {
    "puppeteer": {
      "disableScreenshots": true,
      "waitForAction": 200,
      "show": true
    }
  },
  "include": {
    "I": "./steps_file.js"
  },
  "bootstrap": false,
  "mocha": {},
  "name": "web-test-runner"
}

PS. It related to the value of waitForAction. If change it to larger. error will not happen.

More Information

This issue is related to GoogleChrome/puppeteer#1325.
And I think latest comment is correct. The problem is because the click promise resolved when click triggered. But at that moment, navigation is not complete. No context is ready for new action.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
reubenmillercommented, Feb 7, 2018

I’ve just spent a bit of time looking into this and it is slightly more complex than I thought. Though I found some useful information on the puppeter repo.

It looks like that you have to call the .click and page.waitForNavigation at the same time like so:

await Promise.all([
  page.click('a'),
  page.waitForNavigation()
]);

The reason that it needs to be called at the same time is that apparently .waitForFunction needs to see the new page trigger and the load events occur. So just seeing the page load event is not enough.

So our problem is that we don’t know which links will cause a new page to load. And if the above code is used and the link does not change the url, then it will throw a timeout error.

Now we could check the button if it is link like, however that does not cater for the scenario if there are event handlers on it which cause a redirect somewhere. So here are some options.

Option 1: Auto detect url changes and wait if one is detected (a little bit hacky)

However I have been playing around with the idea with using the targetchanged and load events, and recording the url has changed and loaded state information. So the scenario would play out like this:

  1. Click a link using I.click (wait for ~500 ms or enough time for the targetchanged event to trigger).
  2. targetchanged event triggers, and sets a urlChanged flag
  3. (in click function): Wait for the urlChanged flag to be reset (also with timeout breakout)
  4. load event triggers, reset the urlChanged flag.
  5. (in click function): See that the urlChanged flag has been reset and break out of the wait loop.

Option 2: Split the I.click function into navigation and non-navigation functions

  • I.click - Don’t wait for any navigation before returning
  • I.clickLink - Click buttons/links/a which will cause a navigation,

Option 3: The user must wait for the page to load themselves

Maybe using I.waitInUrl or something similar to that…

Thoughts?

1reaction
othreecommented, Feb 6, 2018

There is a page.waitForNavigation method mentioned in GoogleChrome/puppeteer#1325 , I didn’t tested yet. And it will have another problem, some click didn’t trigger navigation. Or maybe codecept can expose it to outside.

Read more comments on GitHub >

github_iconTop Results From Across the Web

When would an error "Cannot find context with specified id ...
I am getting the following error: Error: Protocol error (Runtime.callFunctionOn): Cannot find context with specified id undefined at Session ...
Read more >
cannot find context with specified id undefined - Stack Overflow
My test was completing but I was getting this error when attempting to await browser.close(); Adding the wait after searching for my final ......
Read more >
When would an error "Cannot find context with specified id ...
I am getting the following error: Error: Protocol error (Runtime.callFunctionOn): Cannot find context with specified id undefined at Session ...
Read more >
Aaron Lee on Twitter: "Error: Protocol error (Runtime.callFunctionOn ...
Error: Protocol error (Runtime.callFunctionOn): Cannot find context with specified id undefined. 6:23 AM · May 23, 2018 ·Twitter Web Client.
Read more >
Puppeteer documentation - DevDocs
If Puppeteer doesn't find them in the environment during the installation step, ... For certain types of errors Puppeteer uses specific error classes....
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