-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also include a test. Electron-mocha sets up an Electron instance and runs Mocha within it. One oddity is that it consumes the ready event before running the Mocha scripts, so that has to be faked (or assumed) within the test.
- Loading branch information
Showing
3 changed files
with
99 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,60 @@ | ||
'use strict' | ||
const {app, BrowserWindow} = require('electron') | ||
const yargs = require('yargs') | ||
const temp = require('temp') | ||
const path = require('path') | ||
const {app, BrowserWindow} = require('electron') | ||
|
||
process.on('uncaughtException', function (err) { | ||
console.error(err) | ||
app.quit() | ||
}) | ||
|
||
var win, filename, url | ||
class Scanner { | ||
run (url, cb) { | ||
this.url = url | ||
this.cb = cb | ||
process.on('uncaughtException', function (err) { | ||
console.error(err) | ||
app.exit(1) | ||
}) | ||
app.once('ready', () => this.onReady()) | ||
} | ||
|
||
function onReady () { | ||
const options = { frame: false, height: 768, width: 1024, x: 0, y: 0, show: false } | ||
win = new BrowserWindow(options) | ||
win.webContents.once('did-stop-loading', grabPage) | ||
win.loadURL(url) | ||
} | ||
onReady () { | ||
const options = { frame: true, height: 768, width: 1024, x: 0, y: 0, show: false } | ||
this.win = new BrowserWindow(options) | ||
this.win.webContents.once('did-stop-loading', () => this.grabPage()) | ||
this.win.loadURL(this.url) | ||
} | ||
|
||
function grabPage () { | ||
temp.track() | ||
const dir = temp.mkdirSync('scanner') | ||
filename = path.join(dir, 'page.html') | ||
console.log(`Saving to ${filename}`) | ||
win.webContents.savePage(filename, 'HTMLOnly', scanPage) | ||
} | ||
grabPage () { | ||
temp.track() | ||
const dir = temp.mkdirSync('scanner') | ||
this.filename = path.join(dir, 'page.html') | ||
console.log(`Saving to ${this.filename}`) | ||
this.win.webContents.savePage(this.filename, 'HTMLOnly', () => this.scanPage()) | ||
} | ||
|
||
function scanPage (error) { | ||
// Do the scan here, HTML source is in filename | ||
app.quit() | ||
if (error) { | ||
console.error(error) | ||
process.exit(-1) | ||
scanPage (error) { | ||
// Do the scan here, HTML source is in this.filename | ||
if (this.cb) { | ||
this.cb(error) // Assume caller will quit the app. | ||
} else { | ||
if (error) { | ||
console.error(error) | ||
app.exit(1) | ||
} else { | ||
app.quit() | ||
} | ||
} | ||
} | ||
} | ||
|
||
const argv = yargs | ||
.usage('Usage: $0 URL') | ||
.demand(1) | ||
.strict() | ||
.version() | ||
.help('h') | ||
.argv | ||
|
||
url = argv._[0] | ||
app.once('ready', onReady) | ||
if (!module.parent || module.parent.id === '.') { | ||
const argv = yargs | ||
.usage('Usage: $0 URL') | ||
.demand(1) | ||
.strict() | ||
.version() | ||
.help('h') | ||
.argv | ||
const s = new Scanner() | ||
s.run(argv._[0]) | ||
} else { | ||
module.exports = Scanner | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* global describe, before, after, it */ | ||
const expect = require('chai').expect | ||
const express = require('express') | ||
const temp = require('temp') | ||
const path = require('path') | ||
const sinon = require('sinon') | ||
const Scanner = require('..') | ||
const fs = require('fs') | ||
const {app} = require('electron') | ||
|
||
describe('Scanner', function () { | ||
before(function (done) { | ||
const app = express() | ||
app.route('/').get((req, res) => { | ||
res.send('Yeah') | ||
}) | ||
this.server = app.listen(() => { | ||
this.url = `http://localhost:${this.server.address().port}` | ||
done() | ||
}) | ||
}) | ||
|
||
after(function () { | ||
this.server.close() | ||
}) | ||
|
||
it('can render and obtain HTML', sinon.test(function (done) { | ||
temp.track() | ||
const test_dir = temp.mkdirSync('scanner-test') | ||
this.stub(temp, 'mkdirSync', () => test_dir) // Sub in our own dir | ||
|
||
const s = new Scanner() | ||
const finish = function (error) { | ||
expect(error).to.be.nil | ||
const page_file = path.join(test_dir, 'page.html') | ||
const file_contents = fs.readFileSync(page_file) | ||
expect(file_contents.toString()).to.equal('Yeah') | ||
done() | ||
} | ||
s.run(this.url, finish) | ||
s.onReady() // Because electron-mocha already consumed the ready event | ||
})) | ||
}) |