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

javascript error: Cannot convert undefined or null to object #720

Open
alchaplinsky opened this issue Sep 26, 2020 · 23 comments
Open

javascript error: Cannot convert undefined or null to object #720

alchaplinsky opened this issue Sep 26, 2020 · 23 comments

Comments

@alchaplinsky
Copy link

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
@Myrmod
Copy link

Myrmod commented Sep 28, 2020

correct me if I'm wrong but might this webdriverio/webdriverio#5370 be the issue?

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

@andrew-pyle
Copy link

andrew-pyle commented Sep 29, 2020

Same issue here with electron@10.1.2 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)

@TheOne20001
Copy link

TheOne20001 commented Sep 29, 2020

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

#174
#254

@andrew-pyle
Copy link

@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
Copy link

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 webdriver@6.5.2 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?

@VictorFouquet
Copy link

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)

@TheOne20001
Copy link

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

@michlimlim
Copy link

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

@VictorFouquet
Copy link

@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.

@darrickc
Copy link

darrickc commented Oct 2, 2020

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

@TheOne20001
Copy link

TheOne20001 commented Oct 2, 2020

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?

@darrickc
Copy link

darrickc commented Oct 2, 2020

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?

@TheOne20001
Copy link

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.

#174
#254

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

@darrickc
Copy link

darrickc commented Oct 2, 2020

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.

@TheOne20001
Copy link

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...

@alex-drocks
Copy link

alex-drocks commented Oct 3, 2020

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)

@dehmer
Copy link

dehmer commented Oct 7, 2020

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

@ChrisHSandN
Copy link

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'
  }
}

@BartNijland91
Copy link

Any updates on this? Or workarounds for now?

@Ankitr19
Copy link

Ankitr19 commented Dec 8, 2020

Any updates on this?

@amiller-gh
Copy link

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:

api.electron.remote = {};

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 🙂

@darrickc
Copy link

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

@matthiasg
Copy link

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

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

No branches or pull requests