Skip to content

Commit

Permalink
Get electron and Spectron working
Browse files Browse the repository at this point in the history
  • Loading branch information
jwood803 committed Sep 23, 2016
0 parents commit 71a1304
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
19 changes: 19 additions & 0 deletions index.html
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
<!-- All of the Node.js APIs are available in this renderer process. -->
We are using node <script>document.write(process.versions.node)</script>,
Chromium <script>document.write(process.versions.chrome)</script>,
and Electron <script>document.write(process.versions.electron)</script>.
</body>

<script>
// You can also require other files to run in this process
require('./renderer.js')
</script>
</html>
57 changes: 57 additions & 0 deletions main.js
@@ -0,0 +1,57 @@
const electron = require('electron')
// Module to control application life.
const app = electron.app
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow

function createWindow () {
// Create the browser window.
mainWindow = new BrowserWindow({width: 800, height: 600})

// and load the index.html of the app.
mainWindow.loadURL(`file://${__dirname}/index.html`)

// Open the DevTools.
//mainWindow.webContents.openDevTools()

// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
});
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', function () {
createWindow();

//console.log(app.getAppPath());
});

// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})

app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow()
}
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
30 changes: 30 additions & 0 deletions package.json
@@ -0,0 +1,30 @@
{
"name": "electron-demo",
"version": "1.0.0",
"description": "A demo Electron application with Spectron",
"main": "main.js",
"scripts": {
"start": "electron .",
"test:e2e": "./node_modules/mocha/bin/mocha tests/test.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/"
},
"keywords": [
"Electron",
"demo"
],
"author": "jwood",
"bugs": {
"url": "https://github.com/"
},
"homepage": "https://github.com/",
"devDependencies": {
"chai": "^3.5.0",
"chai-as-promised": "^5.3.0",
"electron": "^1.3.4",
"mocha": "^3.0.2",
"spectron": "^3.4.0"
}
}
3 changes: 3 additions & 0 deletions renderer.js
@@ -0,0 +1,3 @@
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// All of the Node.js APIs are available in this process.
45 changes: 45 additions & 0 deletions tests/test.js
@@ -0,0 +1,45 @@
const Application = require("spectron").Application;
const assert = require("assert");
const path = require('path');
const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');

var electronPath = path.join(__dirname, '..', 'node_modules', '.bin', 'electron')
if (process.platform === 'win32') electronPath += '.cmd'

// Path to your application
var appPath = path.join(__dirname, '..')

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

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

describe('Test Example', function () {
beforeEach(function () {
return app.start();
});

afterEach(function () {
return app.stop();
});

it('opens a window', function () {
return app.client.waitUntilWindowLoaded()
.getWindowCount().should.eventually.equal(1);
});

// it('should test', function () {
// app.start().then(function () {
// return app.client.getTitle();
// })
// .then(function (title) {
// title.should.equal('2');
// });
// });
});

0 comments on commit 71a1304

Please sign in to comment.