spectron icon indicating copy to clipboard operation
spectron copied to clipboard

Testing electron exe applications

Open csharsha opened this issue 6 years ago • 5 comments

Hi,

We have our electron application built and packed into an executable exe file and when installed it can be seen in C:\Program Files\App Name\resources\app. Can anyone please help me on how to test this kind of applications in spectron.

csharsha avatar Apr 09 '18 15:04 csharsha

I am in a similar situation, the Electron App is compiled and can spawn multiple Tools from loading the main exe. There is not even a main.js file. I would also appreciate some guidance on how we can tie Spectron to these applications for testing purposes.

JimmyMac6996 avatar Apr 25 '18 17:04 JimmyMac6996

@csharsha @JimmyMac6996 Hi, You can use spectron with mocha or jasmine to test the application. In order to launch the application you just need to give your application path (like C:\Program Files\App Name\resources\app.exe) to the Application instance. See the code below:

var app = new Application({ path: '/Applications/MyApp.app/Contents/MacOS/MyApp' //Change the path to your application path (like 'C:\Program Files\App Name\resources\app.exe') })

webriverio provides a descent api that you can use to write your tests.

Thanks Karthik

karthik1407s avatar Jun 13 '18 11:06 karthik1407s

@csharsha @JimmyMac6996

Here is a sample test-spec.js that you refer

var Application = require('spectron').Application;
var assert = require('assert');
var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');

//element xpaths (best practice to move to another .js file)
var element.username = //*[@id="username"];
var element.password = //*[@id="password"];

global.before(function () {
    app = new Application({
        path: 'C:\\Program Files\\App Name\\resources\\app.exe'
    });
    chai.should();
    chai.use(chaiAsPromised);
});

describe('Test Suite', function () {

    this.timeout(20000);

    before('Start Application', function () {
        return app.start()
    });

    beforeEach(function () {
        chaiAsPromised.transferPromiseness = app.transferPromiseness;
    });

    it('Authentication Pop-up', function () {
        return app.client
            .pause(20000) //waiting for login window
            .windowByIndex(1)
            .waitUntilWindowLoaded()
            .setValue(element.userName, 'username').pause(1000)
            .setValue(element.password, 'password').pause(1000)
            .click(element.loginButton);
    });

    it('tests initial window', function () {
        return app.client
            .pause(15000)    //waiting for tool window to show
            .windowByIndex(0)
            .waitUntilWindowLoaded()
            .getWindowCount().should.eventually.equal(1);
    })

    it('tests title and version', function () {
        return   app.client.waitUntilWindowLoaded()
            .getTitle().should.eventually.equal('Electron App vx.x.x');
    });

Before adding the spec above, create your package.json with required dependencies and script command e.g.

{
  "name": "spectron-test-framework",
  "version": "1.0.0",
  "description": "Test Framework for Electron Desktop Application",
  "repository": {
    "type": "git",
    "url": "http://<your-git-repository-path>.git"
  },
  "main": "index.js",
  "scripts": {
    "test:demo": "mocha ./src/test-suites/test-spec.js"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@types/mocha": "^5.2.3",
    "@types/webdriverio": "^4.10.2",
    "chai": "^4.1.2",
    "chai-as-promised": "^7.1.1",
    "electron": "^2.0.2",
    "mocha": "^5.2.0",
    "mochawesome": "^3.0.2",
    "robotjs": "^0.5.1",
    "spectron": "^3.8.0",
  }
}

Install all required dependencies by running "npm install" command

Finally, run the command to execute your tests <path-to-package.json>:\ npm run test:demo

nikhil-zinjurde avatar Aug 27 '18 11:08 nikhil-zinjurde

I have an error when testing an exe instead of importing electron, the error it's the following: Error: A session id is required for this command but wasn't found in the response payload

Ladvace avatar Oct 09 '20 17:10 Ladvace

My code not interage with the application.

test.js:

const Application = require('spectron').Application
const assert = require('assert')
const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');

global.before(() => {
  chai.should();
  chai.use(chaiAsPromised);
});

describe('Application launch', function () {
  this.timeout(10000)

  const app = new Application ({

    path: 'C:\\Program Files\\Folder\\my.exe',
    args: ['app'],

    webPreferences: {
      nodeIntegration: true,
    },
    chromeDriverArgs: ['remote-debugging-port=9222']
  })

  beforeEach(() => app.start().then((app) => {
    chaiAsPromised.transferPromiseness = app.transferPromiseness;
    return app;
    })
  );

  it('shows an initial window', async () => {
    const count = await app.client.getWindowCount();
    assert.equal(count, 1);
  })

  it('decreases the window height and width by 10 pixels', async  () => {
    app.client.timeoutsImplicitWait(1000);
    app.client.click('#bt-config')
    
    var text = app.client.getText('//*[@id="config"]/div[5]/div[1]/span');
    assert(text === 'Preencha corretamente para continuar'); // true
  })
})

package.json:

{
  "name": "Spectron",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "@wdio/cli": "^7.13.2",
    "chai-as-promised": "^7.1.1",
    "spectron": "^9.0.0"
  },
  "devDependencies": {
    "chai": "^4.3.4",
    "electron": "^7.2.1",
    "mocha": "^9.1.2"
  },
  "scripts": {
    "start": "nodemon app.js",
    "test": "mocha"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

LucasSantosSilva avatar Oct 05 '21 13:10 LucasSantosSilva