我正在尝试使用Spectron和摩卡在电子应用程序上进行简单的测试。但是,每当我尝试使用 browserWindow API时,都会出现以下形式的错误:
TypeError:无法读取未定义的属性“ .....”
我在互联网上做了一些研究。我发现一些建议的解决方案是确保将 nodeIntegration 设置为true,并确保未打开 DevTools 。应用程序窗口。我确保这两个条件都得到满足,但是我仍然收到相同的错误。我在这里想念什么?我附上我的代码以供参考。
main.js
const {app, BrowserWindow} = require('electron')
const path = require('path')
let mainWindow
function createWindow () {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration:true
}
})
mainWindow.loadFile('index.html')
}
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
})
test.js
const Application = require('spectron').Application
const assert = require('assert')
const electronPath = require('electron')
const path = require('path')
const { app } = require('electron')
const { expect } = require('chai')
describe('Application launch', function () {
this.timeout(10000)
before(function () {
this.app = new Application({
path: electronPath,
args: [path.join(__dirname, '.')]
})
return this.app.start()
})
after(function () {
if (this.app && this.app.isRunning()) {
return this.app.stop()
}
})
it('should be a focused window', function(){
return this.app.client.waitUntilWindowLoaded().browserWindow.isFocused().then(res => {
expect(res).to.be.true
})
})
})
问题出现在this.app.client.waitUntilWindowLoaded().browserWindow.isFocused()
行