spectron icon indicating copy to clipboard operation
spectron copied to clipboard

javascript error: Cannot convert undefined or null to object

Open alchaplinsky opened this issue 5 years ago • 23 comments

Hey, after updating electron to v10 and spectron to v12 I've started having issues with running my test suite. So I started debugging by creating a single test with code taken from spectron's readme. And when trying to run this test (I'm using Jest by the way) I get this error:

javascript error: javascript error: Cannot convert undefined or null to object
      (Session info: chrome=85.0.4183.98)

      at getErrorFromResponseBody (node_modules/webdriver/build/utils.js:121:10)
      at WebDriverRequest._request (node_modules/webdriver/build/request.js:149:56)
      at Browser.wrapCommandFn (node_modules/@wdio/utils/build/shim.js:74:23)
      at Browser.wrapCommandFn (node_modules/@wdio/utils/build/shim.js:74:23)

Application window launches however, the test fails

  • Node: v14.10.1
  • spectron: 12.0.0
  • electron: v10.1.2
  • chromedriver: 85.0.4183.98

alchaplinsky avatar Sep 26 '20 08:09 alchaplinsky

correct me if I'm wrong but might this https://github.com/webdriverio/webdriverio/issues/5370 be the issue?

We're using webdriverio "^6.1.20" but the latest version is 6.5.2

Myrmod avatar Sep 28 '20 13:09 Myrmod

Same issue here with [email protected] and spectron@^12.0.0. Using jest to run tests.

I've been struggling to get it integrated into my electron app for the first time. I was hitting errors like

TypeError: Cannot read property 'webContents' of undefined

> 51 |     await app.browserWindow.webContents;
     |                             ^

TypeError: waitUntilWindowLoaded Cannot read property 'isLoading' of undefined

> 19 |     await app.client.waitUntilWindowLoaded();
     |                      ^

Then I addd await app.client.waitUntilWindowLoaded(15_000); into the beforeAll() method, and started getting the error referenced in this issue.

 javascript error: javascript error: Cannot convert undefined or null to object
      (Session info: chrome=85.0.4183.98)

      at getErrorFromResponseBody (node_modules/webdriver/build/utils.js:121:10)
      at WebDriverRequest._request (node_modules/webdriver/build/request.js:149:56)
      at Browser.wrapCommandFn (node_modules/@wdio/utils/build/shim.js:74:23)
      at Browser.wrapCommandFn (node_modules/@wdio/utils/build/shim.js:74:23)

andrew-pyle avatar Sep 29 '20 17:09 andrew-pyle

I have seen both of these. I have tried these two fixes but no luck there.

https://github.com/electron-userland/spectron/issues/174 https://github.com/electron-userland/spectron/issues/254

TheOne20001 avatar Sep 29 '20 17:09 TheOne20001

@pgalle Is there a reason Spectron cannot just upgrade to the latest webdriverio? Is it just a matter of opening a PR with the bumped version?

I don't see any breaking changes between 6.1.20 and 6.5.2.

andrew-pyle avatar Sep 29 '20 18:09 andrew-pyle

Actually, Spectron has webdriverio specified like ^6.1.20, so npm will install the 6.5.2 version on npm install. My Spectron tests are running against [email protected] already, and I'm still seeing the issue.

javascript error: javascript error: Cannot convert undefined or null to object
      (Session info: chrome=85.0.4183.98)

Also, WebdriverIO merged the PR fixing webdriverio/webdriverio#5370 in v6.1.9, so we've got the patch either way.

Any other ideas?

andrew-pyle avatar Sep 29 '20 18:09 andrew-pyle

Same issue here, with "devDependencies": { "electron": "^10.1.2", "mocha": "^8.1.3", "spectron": "^12.0.0" } and javascript error: javascript error: Cannot convert undefined or null to object (Session info: chrome=85.0.4183.98)

VictorFouquet avatar Sep 30 '20 09:09 VictorFouquet

I tired to rip my project to it's basest hello world example. All I have left is React, Typescript, Electron and Spectron. It still fails, but it is the base playground.

https://github.com/TheOne20001/electron-react-typescript-spectron

TheOne20001 avatar Sep 30 '20 15:09 TheOne20001

I have the exact same errors and package versions as @VictorFouquet. Do we know what set of versions currently work?

michlimlim avatar Sep 30 '20 17:09 michlimlim

@michlimlim though it doesn't solve the actual problem, I've downgraded my project to "devDependencies": { "electron": "^9.0.0", "mocha": "^8.1.3", "spectron": "^11.0.0" } without having to change anything else and everything works fine with these versions.

VictorFouquet avatar Oct 02 '20 15:10 VictorFouquet

I tried the above versions from VictorFouquet with no luck - still seeing the above error.

darrickc avatar Oct 02 '20 16:10 darrickc

It does work to downgrade but that is no solution, its just avoiding the problem.

I tried the above versions from VictorFouquet with no luck - still seeing the above error.

as per yours, do you have the two fixes 174, and 254 that i mentioned above?

TheOne20001 avatar Oct 02 '20 16:10 TheOne20001

It does work to downgrade but that is no solution, its just avoiding the problem.

I tried the above versions from VictorFouquet with no luck - still seeing the above error.

as per yours, do you have the two fixes 174, and 254 that i mentioned above?

It's not entirely clear what the "fixes" above are. I have in my BrowserWindow ctor the following options

webPreferences: {
                nodeIntegration: true,
                devTools: false
            }

Are the fixes something different?

darrickc avatar Oct 02 '20 19:10 darrickc

It does work to downgrade but that is no solution, its just avoiding the problem.

I tried the above versions from VictorFouquet with no luck - still seeing the above error.

as per yours, do you have the two fixes 174, and 254 that i mentioned above?

It's not entirely clear what the "fixes" above are. I have in my BrowserWindow ctor the following options

webPreferences: {
                nodeIntegration: true,
                devTools: false
            }

Are the fixes something different?

so one of them is to close your devtools window/panel when you are not in dev mode, because that can throw things off, the other is a delay on loading contents into your window by way of a timeout around your loadUrl call.

https://github.com/electron-userland/spectron/issues/174 https://github.com/electron-userland/spectron/issues/254

However as I have said these seem to only help in electron versions <= 9.

TheOne20001 avatar Oct 02 '20 20:10 TheOne20001

That's funny - I read it as the solution is to load your contents right away without a timeout around your loadUrl call. My code had no wait to load - it loaded immediately.

With the above fixes on the old version I am back in shape.

darrickc avatar Oct 02 '20 21:10 darrickc

From the pattern i'd say those two are work arounds and no the actual fixes, but something in 10 disrupts them and brings the actual errors back to the fore. I hope someone does figure out what the actual error is. I'm surprised Spectron folks aren't seeing these issues, and we are...

TheOne20001 avatar Oct 02 '20 21:10 TheOne20001

First time I try to use Spectron - and using the readme example - I do get the same error as others have mentionned:

javascript error: javascript error: Cannot convert undefined or null to object (Session info: chrome=85.0.4183.121) at getErrorFromResponseBody (node_modules\webdriver\build\utils.js:121:10) at WebDriverRequest._request (node_modules\webdriver\build\request.js:149:56) at processTicksAndRejections (internal/process/task_queues.js:93:5) at async Browser.wrapCommandFn (node_modules@wdio\utils\build\shim.js:74:23) at async Browser.wrapCommandFn (node_modules@wdio\utils\build\shim.js:74:23)

alex-drocks avatar Oct 03 '20 11:10 alex-drocks

Same issue here. Additional info: Disabling node integration (nodeIntegration: false) and loading a blank page (win.loadURL('about:blank')) does not throw said exception. Loading blank page with node integration enabled throws again.

"devDependencies": {
    "electron": "^10.1.3",
    "mocha": "^8.1.3",
    "spectron": "^12.0.0"
  }

EDIT: macOS 10.15.7

dehmer avatar Oct 07 '20 19:10 dehmer

Here is a minimal reproducible example:

package.json

{
  "main": "main.js",
  "dependencies": {
    "electron": "^10.1.5",
    "spectron": "^12.0.0",
    "mocha": "^8.2.0"
  }
}

main.js

const {app, BrowserWindow} = require('electron');

app.on('ready', () => {
	const mainWindow = new BrowserWindow({
		webPreferences: {
			nodeIntegration: true
		}
	});

	mainWindow.loadFile('about:blank');
});

app.on('window-all-closed', function () {
	app.quit();
});

spec.js

const Application = require('spectron').Application
const electronPath = require('electron')

describe('Application launch', function () {
	this.timeout(10000)

	beforeEach(function () {
		this.app = new Application({
			path: electronPath,
			args: ['.']
		})
		return this.app.start()
	})

	afterEach(function () {
		if (this.app && this.app.isRunning()) {
			return this.app.stop()
		}
	})

	it('shows an initial window', async function () {
	})
})

To Run:

npm install
mocha spec.js

Error (shown in terminal)

  1) Application launch
       "before each" hook for "shows an initial window":
     javascript error: javascript error: Cannot convert undefined or null to object
  (Session info: chrome=85.0.4183.121)
      at Object.getErrorFromResponseBody (node_modules\webdriver\build\utils.js:94:12)
      at WebDriverRequest._request (node_modules\webdriver\build\request.js:133:31)
      at processTicksAndRejections (internal/process/task_queues.js:97:5)
      at async Browser.wrapCommandFn (node_modules\@wdio\utils\build\shim.js:58:29)
      at async Browser.wrapCommandFn (node_modules\@wdio\utils\build\shim.js:58:29)

Full error from Object.getErrorFromResponseBody (node_modules\webdriver\build\utils.js:94:12)

{
  value: {
    error: 'javascript error',
    message: 'javascript error: Cannot convert undefined or null to object\n' +
      '  (Session info: chrome=85.0.4183.121)',
    stacktrace: 'Backtrace:\n' +
      '\tOrdinal0 [0x00007FF6D669EBD2+3664850]\n' +
      '\tOrdinal0 [0x00007FF6D655C4C2+2344130]\n' +
      '\tOrdinal0 [0x00007FF6D63CC5D3+706003]\n' +
      '\tOrdinal0 [0x00007FF6D63CF0CE+717006]\n' +
      '\tOrdinal0 [0x00007FF6D63CEF7A+716666]\n' +
      '\tOrdinal0 [0x00007FF6D63CF903+719107]\n' +
      '\tOrdinal0 [0x00007FF6D6365D5E+286046]\n' +
      '\tOrdinal0 [0x00007FF6D6358F96+233366]\n' +
      '\tOrdinal0 [0x00007FF6D636533A+283450]\n' +
      '\tOrdinal0 [0x00007FF6D6358DDF+232927]\n' +
      '\tOrdinal0 [0x00007FF6D6336E1B+93723]\n' +
      '\tOrdinal0 [0x00007FF6D6337E2E+97838]\n' +
      '\tOrdinal0 [0x00007FF6D657690C+2451724]\n' +
      '\tGetHandleVerifier [0x00007FF6D6848444+1625924]\n' +
      '\tGetHandleVerifier [0x00007FF6D6848128+1625128]\n' +
      '\tGetHandleVerifier [0x00007FF6D685430B+1674763]\n' +
      '\tGetHandleVerifier [0x00007FF6D6848A3E+1627454]\n' +
      '\tOrdinal0 [0x00007FF6D656CBBC+2411452]\n' +
      '\tOrdinal0 [0x00007FF6D65781EB+2458091]\n' +
      '\tOrdinal0 [0x00007FF6D658D34C+2544460]\n' +
      '\tBaseThreadInitThunk [0x00007FFBE6127C24+20]\n' +
      '\tRtlUserThreadStart [0x00007FFBE648CEA1+33]\n'
  }
}

cjhewett avatar Oct 30 '20 11:10 cjhewett

Any updates on this? Or workarounds for now?

BartNijland91 avatar Dec 02 '20 14:12 BartNijland91

Any updates on this?

Ankitr19 avatar Dec 08 '20 12:12 Ankitr19

Tracked this one down I think.

Its an error from https://github.com/electron-userland/spectron/blob/1510a9beddb64ca3630c143ccdc15466e17b40f5/lib/api.js whose stack trace gets eaton by the bootstrap process.

Running it manually, we find the error is thrown here because electron.remote is undefined: https://github.com/electron-userland/spectron/blob/1510a9beddb64ca3630c143ccdc15466e17b40f5/lib/api.js#L141

As of Electron 10 enableRemoteModule defaults to false. It will be removed in v14. https://github.com/electron/electron/blob/master/docs/breaking-changes.md#default-changed-enableremotemodule-defaults-to-false

The fix for me was to explicitly enable it when creating my window in webPreferences:

const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true,
      enableRemoteModule: true,
    },
  });

This should be updated in the getting started docs, and Spectron should migrate away from electron.remote if that's not already planned 🙂

amiller-gh avatar Dec 13 '20 04:12 amiller-gh

This didn't solve it for me - I actually already tried this a few months ago.

darrickc avatar Dec 14 '20 16:12 darrickc

@amiller-gh This is correct. Remote module was missing. I enable it during tests now.

matthiasg avatar Feb 01 '21 12:02 matthiasg