Do I have to use Spectron to test Electron?
Asked Answered
C

2

8

Recently I found some difficulty to plan automation testing for our application using Electron. I've tried to use Spectron, which looks like the official framework to test Electron apps, however, I found the documentation was very hard to understand on their website.

I know there are some famous apps using Electron, e.g Slack, Wordpress and Github Desktop. I wonder if they are really using Spectron or something else as automation to test their apps.

Pretty much I just want to figure out if Spectron is the only way to test Electron.

Christabella answered 17/8, 2017 at 19:52 Comment(1)
"I just want to figure out if Spectron is the only way to test Electron." ... No, there are other testing frameworks for Electron too. For example, I know that the commercial testing framework QF-Test can test electron apps, see <qfs.de/en/qf-test-manual/lc/…>.Predispose
S
8

In terms of end to end testing I would say that Spectron is the way to go. It can be pretty hard to get up and running, but Spectron is built upon WebdriverIO and there you'll find a lot of documentation.

To get up and running I would propose the following.

npm install spectron mocha --save-dev

my-first-test-case.e2e.js

const electron = require('electron');

describe('my first test case', function () {

  beforeEach(() => {
    this.app = new Application({
      path: electron,
      args: ['.'],
    });

    return this.app.start();
  });

  afterEach(() => {
    if (this.app && this.app.isRunning()) {
      return this.app.stop();
    }
  });

  it('creates a new tab when account is added', function () {
    const accountName = 'awesomeMail';

    return this.app.client.waitUntilWindowLoaded()
      .waitForVisible('h1')
      .getText('h1')
      .then(text => expect(text).toEqual('Welcome'));
  });
});

And then you run

mocha my-first-test-case.e2e.js

Or if you dont have mocha installed globally

node_modules/.bin/mocha my-first-test-case.e2e.js

Slavish answered 2/9, 2017 at 17:13 Comment(3)
And I am assuming the documentation for functions like waitUntilWindowLoaded(), waitForVisible() and getText() is in the webdriverIO docs?Rodmun
waitUntilWindowLoaded is a part of the Spectron API, while waitForVisible and getText is WebdriverIOSlavish
How can I run electron app in deb mode if I need webpack for it?Ligroin
K
2

I tried to test electron app with java for a while but I just came back to Spectron again because of my applications structure. If you want to test your electron app with other options(java,phyton and selenium) you can set browser options and capabilities for it as you can see in the below.

Example of Java code:

 ChromeOptions options = new ChromeOptions();
    options.setBinary(binaryPath);
    options.addArguments("--app=" + argPath);
    options.setCapability("chromeOptions", options);
    driver = new ChromeDriver(options);   
Kellum answered 19/9, 2018 at 7:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.