Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chrome extension throwing a “Cannot convert undefined or null to object” error #738

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

alfreddagenais
Copy link

When I try to start Spectron, i got a javascript error: javascript error: Cannot convert undefined or null to object in the console

To debug this i search lot where can be. I finally found the way to reproduce that error.

Install Debugging tools in VS Code

I install Debugging tests in VS Code tools to help me with this debugging.

Start Debugging

I try to add many break points to find the possible error.

The breakpoint they was the more useful :

  • node_modules/webdriver/build/request.js at line 132 with the variables fullRequestOptions

Capture d’écran, le 2020-10-12 à 16 50 05

I finally found Spectron send Chromedrive debug some functions and the Chromedrive callback. And for this last call back Chromedrive send the javascript error: Cannot convert undefined or null to object

Capture d’écran, le 2020-10-12 à 16 54 34

When you check into this request, you will find the URL use for this request 👍

Capture d’écran, le 2020-10-12 à 16 54 45

The URL we got is http://127.0.0.1:9515/session/{{session_name}}/execute/sync. The session_name is a random session UID

So go back to the fullRequestOptions and you will see the exact request send to the debug tool

Capture d’écran, le 2020-10-12 à 16 58 34

And next we found these 2 parameters were sent

Capture d’écran, le 2020-10-12 à 16 59 59

Try to investigate

I open my Postman application and I try to send POST request with the same parameters.

curl --location --request POST 'http://127.0.0.1:9515/session/55ac1420d478c0b597644644fc2009bf/execute/sync' \
--header 'Content-Type: application/json' \
--data-raw '{
    "args": [
        "require"
    ],
    "script": "return (function (requireName) {.....}).apply(null, arguments)""
}'

Bam!!! We have the error
Capture d’écran, le 2020-10-12 à 17 08 04

Correct the error

I finally found electron.remote not initiated at the point of the code. So I simply add the code to protect this and et voilà!

@movitto
Copy link

movitto commented Jan 5, 2021

@alfreddagenais thank you for this patch, it helps resolve the issue locally. Expanding upon this, according to this stackoverflow issue, the electron.remote is only available if call(s) to new BrowserWindow are extended with the following parmeter:

const myWindow = new BrowserWindow({ webPreferences: { enableRemoteModule: true } });

I confirmed this locally, if I modify my application such that the main window is created w/ this parameter the error disappears, no hotfix is needed.

The issue is that many will not be able to enable the remote module (it entails security implications and is against electron security guidelines.

Your hotfix works as an interim solution for the time being but does entail various side-effects, namely various BrowserWindow and WebContents methods will not be available in the spectron API and perhaps more importantly the spectron Application 'stop' method will not actually stop the application. In the original (premodified) implementation, the stop method delegates to electron.remote.app.quit which actually quits the application but in your patch, this is stubbed out and the resolve/reject methods are manually called.

All in all great addition, though others should be aware of the tradeoffs until we figure out a solution that works in all cases (ideally spectron should not depend on the remote module, but not sure if this is possible)

@snake-py
Copy link

@movitto I was about to go crazy thank you for your comment!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants