cuprite icon indicating copy to clipboard operation
cuprite copied to clipboard

ReferenceError: _cuprite is not defined

Open rubendinho opened this issue 3 years ago • 7 comments

Since upgrading Cuprite to 0.14.3 and Ferrum to 0.13.0, we're seeing intermittent errors in our RSpec runs on Github Actions. We are not seeing errors like this locally.

Failure/Error: click_on 'Get started'

     Ferrum::JavaScriptError:
       ReferenceError: _cuprite is not defined

Would appreciate any tips on mitigating this.

rubendinho avatar Nov 28 '22 18:11 rubendinho

Getting this very frequently on CI (never locally) which makes us reconsider using this project. Happy to try to patch but not sure where to start.

I did notice that this error seems to occur right after switching to an iframe.

rubendinho avatar Jan 24 '23 02:01 rubendinho

Turned out that adding a page.driver.wait_for_network_idle right after switching to the iframe fixed this. I think it's happening on CI because Cuprite is executing too quickly there.

rubendinho avatar Jan 24 '23 14:01 rubendinho

That might be the case, but overall I had a few workarounds just because Chrome doesn't work properly and doesn't load scripts before loading the page https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-addScriptToEvaluateOnNewDocument I can try to check if this changed because that was long time ago.

route avatar Jan 24 '23 15:01 route

Seeing this randomly too with iframes.

jclusso avatar Mar 09 '23 21:03 jclusso

Also facing the same issue since I have upgraded from 0.14.2. Adding page.driver.wait_for_network_idle before it fails helps a lot, thank you @rubendinho :+1:

zedtux avatar May 09 '23 19:05 zedtux

Actually, even with page.driver.wait_for_network_idle or a custom begin ... rescue sleep X; retry end I can't get my tests passing, I'm downgrading to 0.14.2 again.

zedtux avatar May 09 '23 19:05 zedtux

I hit the same issue and after some investigation realized that this is because Cuprite's extensions (source) were not properly injected into the new window opened during a test.

This seems to be a bug in Chrome itself because Ferrum actually does inject the extensions and I double-checked this by debugging inject_extensions method (source). Furthermore, when I updated my Chromium version to the bleeding edge, the issue was gone. But I think it's still present in the majority of Docker images that we run in CI.

I tried manually injecting extensions as a workaround, but it didn't work reliably:

current_browser = page.driver.browser
new_window_handle = current_browser.window_handles.last
target = current_browser.targets[new_window_handle]

target.page.send(:inject_extensions)

What did work though, is refreshing the driver after switching to the new window:

page.driver.refresh

baygeldin avatar Jun 22 '23 21:06 baygeldin