Solved - Spectron not triggering app


#1

Hi,

Electron : 1.7.6
Spectron : latest
OS : Testing in mac

I am trying to write test cases fro my electron app.
I checked few links to trigger spectron without exe/app file.
Following this : http://checkcoding.com/q/170252/javascript-electron-spectron-spectron-and-electron-without-exe-files

my project structure

testelectronMain.js

const Application = require('spectron').Application;
const path = require('path');

var electronPath = path.join(__dirname, '..', 'node_modules', '.bin', 'electron');

if (process.platform === 'win32') {
    electronPath += '.cmd';
}

var appPath = path.join(__dirname, '..');

console.log(electronPath)
console.log(appPath)

beforeEach(function() {
var app = new Application({
    path: electronPath,
    args: [appPath]
});

app.start().then(function () {
    // Check if the window is visible
    return app.browserWindow.isVisible()
}).then(function (isVisible) {
    // Verify the window is visible
    assert.equal(isVisible, true)
}).then(function () {
    // Get the window's title
    return app.client.getTitle()
}).then(function (title) {
    // Verify the window's title
    assert.equal(title, 'My App')
}).then(function () {
    // Stop the application
    return app.stop()
}).catch(function (error) {
    // Log any failures
    console.error('Test failed', error.message)
});
});

package.json:
"test": "mocha --recursive testprojectfiles/"

Expected Result :

It should trigger electron app

Current behavior :

This is not triggering the app, and giving this info in terminal

0 passing (0ms)

Thanks
Jk


#2

I wrongly configured testelectronMain.js file.
Below code works fine with mocha

const Application = require('spectron').Application;
const path = require('path');
var assert = require('assert')



var electronPath = path.join(__dirname, '..', 'node_modules', '.bin', 'electron');
var appPath = path.join(__dirname, '..');

if (process.platform === 'win32') {
    electronPath += '.cmd';
}

console.log(electronPath)
console.log(appPath)




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

    beforeEach(function () {
        this.app = new Application({
            path: electronPath,
            args: [appPath]
        });
        return this.app.start()
    })

    afterEach(function () {
        if (this.app && this.app.isRunning()) {
            return this.app.stop()
        }
    })

    it('shows an initial window', function () {
        return this.app.client.getWindowCount().then(function (count) {
            assert.equal(count, 1)
        })
    })
})