electron-userland / spectron

DEPRECATED: 🔎 Test Electron apps using ChromeDriver
http://electronjs.org/spectron
MIT License
1.68k stars 228 forks source link

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

Open alchaplinsky opened 4 years ago

alchaplinsky commented 4 years ago

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

Myrmod commented 4 years ago

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

andrew-pyle commented 4 years ago

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 commented 4 years ago

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

andrew-pyle commented 4 years ago

@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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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

VictorFouquet commented 4 years ago

@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 commented 4 years ago

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

TheOne20001 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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.

darrickc commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 3 years ago

Any updates on this? Or workarounds for now?

Ankitr19 commented 3 years ago

Any updates on this?

amiller-gh commented 3 years ago

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 🙂

darrickc commented 3 years ago

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

matthiasg commented 3 years ago

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