spectron
spectron copied to clipboard
Chrome extension throwing a “Cannot convert undefined or null to object” error
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
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
When you check into this request, you will find the URL use for this request 👍
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
And next we found these 2 parameters were sent
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
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à
!
@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)
@movitto I was about to go crazy thank you for your comment!