Search code examples
testingautomated-testselectrontestcaferobotjs

TestCafe with Electron: show and hide main window


Our Electron application starts minimized to the Windows notification area i.e. not visible on the desktop.

The user forces the app to display on the desktop by clicking on the app notification tray icon.

Automating this is possible with RobotJs (i.e. clicking on hardcoded XY coordinates) but this is a bit flaky across multiple resolutions even with normalized coordinates.

Through TestCafe, I wanted to show and hide the main window programmatically as needed for our tests.


Solution

  • Following Alex's example and with help from a colleague, this is possible using the TestCafe ClientFunction and Electron native functions:

    import { ClientFunction } from 'testcafe';
    
    fixture `Electron page`
            .page ``;
    
    const isWindowVisible = ClientFunction(() => {
      const remote = require('electron').remote;
      const win = remote.getCurrentWindow();
      return win.isVisible();
    });
    
    const hideWindow = ClientFunction(() => {
      const remote = require('electron').remote;
      const win = remote.getCurrentWindow();
      win.hide();
    });
    
    const showWindow = ClientFunction(() => {
      const remote = require('electron').remote;
      const win = remote.getCurrentWindow();
      win.show();
    });
    
    test('My test', async (t) => {
    
      await showWindow();
      console.log(await isWindowVisible());
      await t.wait(2000);
      await hideWindow();
      console.log(await isWindowVisible());
    });