Exploring Spectron API

Spectron provides us with a simple but powerful API that leverages us to handle the various aspect of an Electron application from your test cases. You can manipulate almost each and every point of the Electron application from your test cases using the Spectron API. The preceding section already gave you a brief idea about a Spectron test case. In this section, let's explore the Spectron API in detail. As I said, it's a very simple API, and you should integrate Spectron with some other testing framework for real-world use cases.

Creating an Electron application instance is the first step in testing, and it should be done when initializing the test case. When testing with Mocha the Electron application instance should be created in the  beforeEach callback. The Application class accepts a configuration option as the parameter that can be used to configure your Electron shell application for your test cases. The path is a required option for the Application that indicates the executable path. You need to pass the absolute path to your application. If you need to invoke the Electron from your current working directory with your main script, you need to use the Electron command with the main script filename as the first item in the args array:

let application = new Application({
path: './node_modules/.bin/electron', // OR just electron
args: ['main.js']
});

You can use the args to pass some command-line arguments to the Electron executable. It can be chrome command-line switches that Electron supports or any other custom command-line arguments that you need to pass to the application. Basically, the args is an array or arguments that you need to pass:

let application = new Application({
path: './node_modules/.bin/electron', // OR just electron
args: ['main.js', 'enable-logging']
});

Here are the some of the options that you can pass to the Application constructor:

let application = new Application({
path: './node_modules/.bin/electron', // OR just electron
args: ['main.js', 'enable-logging'],
cwd: path.join(__dirname, '..'),// current working directory
env: { //Custom environment variables
var1: 'Value'
},
host: 'localhost',
port: 9515,
nodePath: '/User/node/',
requireName: 'require'
});

Not all the options are listed here. You can pass the custom environment variables in the env object so that your code can be accessed those variables in using the process.env of the node. The host and port are the host and port of the Chrome driver process.

The Electron helpers provided by Spectron need to access the Electron API. So, if you have disabled the node integration in your application, you will need to expose the require function to Spectron in your code. You can do this by adding the following code to the application code first:

if(process.env.NODE_ENV == 'test') {
window.electronRequire = require;
}

Then, in your Spectron application constructor, you need to use this variable, as follows:

let application = new Application({
path: './node_modules/.bin/electron', // OR just electron
args: ['main.js', 'enable-logging'],
requireName: 'electronRequire' // add this line
});

You need to set the NODE_ENV variable before running the test:

NODE_ENV=test mocha tests/test.js

The application instance exposes some properties and methods that give access to the underlying Electron application and its window. The Electron property, which we had already discussed before, is equivalent to the following:

// In your Spectron test case
var application = new Application({
path: '....'
});
var electron = application.electron;

// The above line is equivalent to
var electron = require('electron') ;

This property can be used as the gateway to the Electron API. You can access the Electron API through this property.

The current browser window object can be accessed using the browserWindow object. The property is an alias for the following code method call:

require('electron').remote.getCurrentWindow();

This will return a BrowserWindow object. The full access to the BrowserWindow API is available on this property. For example, check whether the window is visible or not using the BrowserWindow method, as follows:

var application = new Application({...})
var isWindowVisible = application.browserWindow.isVisible();
console.log('Visible - ' + isWindowVisible);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.240.230