使用 spectron 访问多个渲染器

Access multiple renderers with spectron

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

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

这与 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 或其他任何东西一起工作,那将非常有帮助。

谢谢!

与用户一样,Specton 只能与专注的 window 进行交互。 应该可以工作,但我还没有测试过:

// 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');

遵循给出的理念,我们可以使用WebDriverIO来代替使用BrowserWindow来聚焦所需的window:

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
    });
});
  it('Switch window', async () => {
    await app.client.waitUntilWindowLoaded(100000)
      .windowByIndex(0);
      .windowByIndex(1);
})