我有一个多窗口电子应用程序,即它由一个发射器窗口和一个主窗口组成。
主要过程负责首先显示启动器,初始化所有内容后,它将通过ipc获取一个事件并显示主窗口,隐藏启动器。
我在主窗口上使用on('close')
来检测用户何时关闭主窗口,再次显示启动器并执行一些拆卸逻辑,然后退出应用程序。
this.mainWindow = new BrowserWindow({width: 1024, height: 768, webPreferences: {nodeIntegration: true}});
this.mainWindow.setMenu(null);
this.mainWindow.on('close', () => {
this.logger.debug('"close" received');
this.mainWindow.hide();
this.launcherWindow.show();
this.sendShutdown();
});
这很好用,现在我想用Spectron集成测试此行为。我在将关闭信号发送到窗口时模拟用户单击关闭时遇到问题。
it('should start shutting down the services gracefully if the user clicks on the close button', async () => {
await app.client.windowByIndex(1);
await app.client.close();
//..... expectations that verify the launcher window is visible now
});
我已经确认索引为1的窗口是主窗口。当我调用app.client.close();
时,主窗口关闭,但是我在日志中看到未触发主窗口的on('close', )
事件,因此它不会跳回到启动器。
我有什么想念/误解吗?
答案 0 :(得分:0)
最后,我包装了在额外方法中执行的代码,并将该方法绑定到ipcMain.on("should-shutdown",..
事件。无论如何,这是有道理的,因为其他渲染器也应该能够请求平稳关闭。
然后在Spectron测试中使用
const { ipcRenderer } = app.electron;
ipcRenderer.send('smooth-shutdown');
代替app.client.close()
来启动关闭序列。
答案 1 :(得分:0)
在 main.js 中添加以下代码
mainWindow.on('close', async function (event) {
if(process.env.NODE_ENV=="test"){
app.isQuiting = true;
app.quit();
}
//other codes
})
在测试用例中添加 NODE_ENV=test
beforeAll(async function (done) {
this.app = new Application({
path: electronPath,
chromeDriverArgs: ['--no-sandbox'],
args: [path.join(__dirname, '../main.js')],
env: {NODE_ENV: 'test'},
waitTimeout: 10e2
});
await this.app.start()
done();
})
afterAll(async function (done) {
if (this.app && this.app.isRunning()) {
await this.app.stop();
done();
} else {
done();
}
})