2

I'm working on an Electron application. The main process opens a first renderer (browserWindow). When the user click on a button, this renderer sends an IPC message to the main process. When this message is received, the main process opens a second, different, renderer. Those two renderers live concurently. The application works fine.

Then, using Spectron to test this app, how to access both renderers ? The problem is app.rendererProcess always returns the first renderer.

This is the same problem with app.client which always contains the WebdriverIO browser object of the first renderer and never the second.

Is there a way to list all the process of a Spectron application in a test? Is it possible to access the browser object of the second renderer ?

With 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');
});

I'm using AVA but I don't think it is the problem. So if anyone know how to make this works with Mocha or anything else, it would be very helpfull.

Thanks !

Anozer
  • 411
  • 1
  • 4
  • 11

3 Answers3

4

Following the philosophy given by Tim answer, instead of using BrowserWindow, we can use WebDriverIO to focus the desired 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
    });
});
Anozer
  • 411
  • 1
  • 4
  • 11
1

Like a user, Specton can only interact with the focused window. This should work but I haven't tested it:

// 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');
Tim
  • 6,485
  • 2
  • 42
  • 81
  • Unfortunately, `app.electron.BrowserWindow` is undefined. I tried `app.electron.remote.BrowserWindow.getAllWindows()` but I get the following error returned by the promise: `unknown error: Maximum call stack size exceeded`. – Anozer Sep 07 '18 at 16:17
0
  it('Switch window', async () => {
    await app.client.waitUntilWindowLoaded(100000)
      .windowByIndex(0);
      .windowByIndex(1);
})
  • 2
    Please don't post only code as an answer, but include an explanation what your code does and how it solves the problem of the question. Answers with an explanation are generally of higher quality, and are more likely to attract upvotes. – Mark Rotteveel Feb 10 '20 at 11:23