【问题标题】:Access multiple renderers with spectron使用 spectron 访问多个渲染器
【发布时间】:2018-09-01 18:15:26
【问题描述】:

我正在开发一个 Electron 应用程序。主进程打开第一个渲染器(browserWindow)。当用户点击一个按钮时,这个渲染器会向主进程发送一条 IPC 消息。收到此消息后,主进程打开第二个不同的渲染器。这两个渲染器同时存在。该应用程序运行良好。

那么,使用 Spectron 测试这个应用程序,如何访问这两个渲染器?问题是app.rendererProcess 总是返回第一个渲染器。

app.client 也存在同样的问题,它始终包含第一个渲染器的 WebdriverIO browser 对象,而不是第二个。

有没有办法在测试中列出 Spectron 应用程序的所有进程?是否可以访问第二个渲染器的browser 对象?

使用 AVA:

test.(async t => {
    // the application is open before the test
    // at this point, the first renderer is open

    // click on the button to open the second renderer
    await t.context.app.client.click('#bt_openSecondRenderer');

    // wait for the second renderer to open

    // error: this element doesn't exist
    await t.context.app.client.click('elt_of_the_scnd_renderer');
});

我正在使用 AVA,但我认为这不是问题所在。因此,如果有人知道如何使其与 Mocha 或其他任何东西一起使用,那将非常有帮助。

谢谢!

【问题讨论】:

    标签: mocha.js electron webdriver-io ava spectron


    【解决方案1】:

    遵循Tim answer 给出的理念,我们可以使用WebDriverIO 来聚焦所需的窗口,而不是使用BrowserWindow:

    test.(async t => {
        // here, t.context.app.client handles the first window
        await t.context.app.client.windowByIndex(1).then(() => {
            // here, t.context.app.client handles the second window
        });
    });
    

    【讨论】:

      【解决方案2】:

      像用户一样,Specton 只能与焦点窗口进行交互。这应该工作,但我还没有测试过:

      // The docs say that app.electron gives you access to all the Electron APIs
      // This should get you a list of BrowserWindows
      const windows = await t.context.app.electron.BrowserWindow.getAllWindows();
      
      // Focus the window you want to interact with
      windows[1].focus();
      
      // Do your clicking
      await t.context.app.client.click('elt_of_the_scnd_renderer');
      

      【讨论】:

      • 很遗憾,app.electron.BrowserWindow 未定义。我尝试了app.electron.remote.BrowserWindow.getAllWindows(),但我得到了 promise 返回的以下错误:unknown error: Maximum call stack size exceeded。
      【解决方案3】:
        it('Switch window', async () => {
          await app.client.waitUntilWindowLoaded(100000)
            .windowByIndex(0);
            .windowByIndex(1);
      })
      

      【讨论】:

      • 请不要只发布代码作为答案,而是说明您的代码的作用以及它如何解决问题的问题。带有解释的答案通常质量更高,更有可能吸引投票。
      猜你喜欢
      • 1970-01-01
      • 2015-09-26
      • 1970-01-01
      • 1970-01-01
      • 2014-11-01
      • 2017-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多