spectron: When calling waitUntilWindowLoaded, webContents is not set in Application

I’m seeing: waitUntilWindowLoaded: Cannot read property ‘isLoading’ of undefined

This is the same issue as reported in issue 157, but that was closed, so I thought I would open a new issue. See: https://github.com/electron/spectron/issues/157

I am using spectron 3.4.0 with electron 1.3.2, but I also tried spectron 3.3.0 and this made no difference for me.

After running a lot of tests and debugging, I am pretty convinced the issue is related to the following:

It appears that things work fine as long as the electron App under test creates AND loads its window immediately upon call to the app ready event handler. However, if loading the window is deferred to perform some other processing first, the above error occurs consistently.

Just to be real clear here:

The following does not seem to have a problem:

let mainWindow

app.on('ready', () => {
    mainWindow = new BrowserWindow({ 
        width: 1400, 
        height: 900,
        backgroundColor: '#EAECEE'
    })
    mainWindow.loadURL(`file://${__dirname}/index.html`)
})

However, the following always seems to fail:

let mainWindow

app.on('ready', () => {
    mainWindow = new BrowserWindow({ 
        width: 1400, 
        height: 900,
        backgroundColor: '#EAECEE'
    })
    setTimeout(() => {
        mainWindow.loadURL(`file://${__dirname}/index.html`)
    }, 2000)
})

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 9
  • Comments: 26 (1 by maintainers)

Commits related to this issue

Most upvoted comments

Oh, I found the problem. It’s because I opened the devtool in the main script. After disabling it, it works well.

Using electron@5 you must set

webPreferences: {
  nodeIntegration: true,
},

This fixed my issue.

Encountering a similar issue when nodeIntegration is set to false

Not sure if this is an expected behaviour

this.browserWindow = new BrowserWindow({
      webPreferences: {
        nodeIntegration: false,
      }
    });

Failing test :

1) Opening editor
       opens a window:
     TypeError: waitUntilWindowLoaded Cannot read property 'isLoading' of undefined
      at waitUntil(<Function>, ) - C:\Users\cdelaunay\Desktop\Carnelian\node_modules\spectron\lib\application.js:229:17

  2) Opening editor
       "after each" hook for "opens a window":
     TypeError: Cannot read property 'exit' of undefined
      at Object.stopApp (test\hooks.js:26:29)
      at Context.afterEach (test\open_editor.js:11:17)

Edit :

It will become very problematic when nodeIntegration will be set to false by default in electron 5.0.0 image

This error can also occur if you set requireName to something and your app doesn’t correspond properly.

E.g. I had set requireName: ‘electronRequire’ but my app forgot to change window.electronRequire = require;

            if(window.process&&process.versions&&process.versions.electron) {
                window.electronRequire = require;
                delete window.require;
            }

I had this issue too - perhaps we could add something to the README?

@TooBug Confirmed that fixed my issue (I was loading devtools in my prod build also.) Thanks!

Having the same issue here with

main.js :

`const { app, BrowserWindow } = require(‘electron’)

function createWindow () {

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

win.loadFile(‘index.html’)

//win.webContents.openDevTools() } app.whenReady().then(createWindow)

app.on(‘window-all-closed’, () => { if (process.platform !== ‘darwin’) { app.quit() } })

app.on(‘activate’, () => { if (win === null) { createWindow() }

})`

DevDependecies in package.json :

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

test script :

`const { Application } = require(‘spectron’) const assert = require(‘assert’) const electronPath = require(‘electron’) const path = require(‘path’)

const myApp = new Application({ path: electronPath, args: [path.join(__dirname, ‘…’, ‘main.js’)] })

const verifyWindowIsVisibleWithTitle = async (app) => { await app.start() try { const isVisible = await app.browserWindow.isVisible() assert.strictEqual(isVisible, true) const title = await app.client.getTitle() assert.strictEqual(title, ‘My App’) } catch (error) { console.error(‘Test failed’, error.message) } await app.stop() } verifyWindowIsVisibleWithTitle(myApp)`

Error is the same wether win.webContents.openDevTools() is commented or not :

Test failed Cannot read property ‘isVisible’ of undefined (node:15762) UnhandledPromiseRejectionWarning: TypeError: Cannot read property ‘remote’ of undefined at /home/victorfouquet/Bureau/test-test/node_modules/spectron/lib/application.js:91:28 at new Promise (<anonymous>) at Application.stop (/home/victorfouquet/Bureau/test-test/node_modules/spectron/lib/application.js:81:10) at verifyWindowIsVisibleWithTitle (/home/victorfouquet/Bureau/test-test/test/spec.js:77:13) at processTicksAndRejections (internal/process/task_queues.js:97:5) (node:15762) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2) (node:15762) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

@TooBug Thank you it save my day