【问题标题】:Spectron app.client.close() does not trigger on('close' eventSpectron app.client.close() 不会触发 on('close' 事件
【发布时间】:2019-11-21 06:04:49
【问题描述】:

我有一个多窗口电子应用程序,即它由一个启动器窗口和一个主窗口组成。

主进程负责首先显示启动器,一切初始化后,它通过 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', ) 事件没有被触发,因此它不会跳回启动器。

我有什么遗漏/误解吗?

【问题讨论】:

    标签: typescript electron integration-testing spectron


    【解决方案1】:

    我最终将执行的代码包装在一个额外的方法中,并将该方法绑定到ipcMain.on("should-shutdown",.. 事件。无论如何,这是有道理的,因为其他渲染器也应该能够请求平滑关闭。

    在 Spectron 测试中,我使用了

     const { ipcRenderer } = app.electron;
     ipcRenderer.send('smooth-shutdown');
    

    而不是app.client.close() 来启动关机序列。

    【讨论】:

      【解决方案2】:

      在 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();
          }
      })
      

      【讨论】:

        猜你喜欢
        • 2018-02-20
        • 1970-01-01
        • 1970-01-01
        • 2018-09-20
        • 2013-05-26
        • 2020-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多