Integration test: tape + spectron hello world

This commit is contained in:
DC
2016-09-05 23:52:37 -07:00
parent 993e7d77ad
commit fe8c3b190c
7 changed files with 150 additions and 14 deletions

41
test/index.js Normal file
View File

@@ -0,0 +1,41 @@
const test = require('tape')
const fs = require('fs-extra')
const path = require('path')
const setup = require('./setup')
console.log('Creating test dir: ' + setup.TEST_DATA_DIR)
const DOWNLOAD_DIR = path.join(setup.TEST_DATA_DIR, 'Downloads')
fs.mkdirpSync(DOWNLOAD_DIR)
test.onFinish(function () {
console.log('Removing test dir...')
fs.removeSync(setup.TEST_DATA_DIR)
})
test('app runs', function (t) {
t.timeoutAfter(10e3)
const app = setup.createApp()
setup.waitForLoad(app, t)
.then(() => setup.wait())
.then(() => setup.screenshotCreateOrCompare(app, t, 'torrent-list-basic'))
.then(() => setup.endTest(app, t),
(err) => setup.endTest(app, t, err || 'error'))
})
test('show download path missing', function (t) {
fs.removeSync(DOWNLOAD_DIR)
t.timeoutAfter(10e3)
const app = setup.createApp()
setup.waitForLoad(app, t)
.then(() => app.client.getTitle())
.then((text) => console.log('Title ' + text))
.then(() => app.client.waitUntilTextExists('.torrent-list', 'Download path missing'))
.then((err) => t.notOk(err))
.then(() => app.client.click('a'))
.then(() => setup.wait())
.then(() => app.browserWindow.getTitle())
.then((windowTitle) => t.equal(windowTitle, 'Preferences'))
.then(() => setup.endTest(app, t),
(err) => setup.endTest(app, t, err || 'error'))
})

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 MiB

69
test/setup.js Normal file
View File

@@ -0,0 +1,69 @@
const path = require('path')
const Application = require('spectron').Application
const fs = require('fs-extra')
const TEST_DATA_DIR = path.join(__dirname, 'tempTestData')
module.exports = {
TEST_DATA_DIR,
createApp,
endTest,
screenshotCreateOrCompare,
waitForLoad,
wait
}
// Runs WebTorrent Desktop.
// Returns a promise that resolves to a Spectron Application once the app has loaded.
// Takes a Tape test. Makes some basic assertions to verify that the app loaded correctly.
function createApp (t) {
return new Application({
path: path.join(__dirname, '..', 'node_modules', '.bin',
'electron' + (process.platform === 'win32' ? '.cmd' : '')),
args: [path.join(__dirname, '..')],
env: {NODE_ENV: 'test'}
})
}
// Starts the app, waits for it to load, returns a promise
function waitForLoad (app, t) {
return app.start().then(function () {
return app.client.windowByIndex(1)
}).then(function () {
return app.client.waitUntilWindowLoaded()
}).then(function () {
return app.webContents.getTitle()
}).then(function (title) {
t.equal(title, 'WebTorrent Desktop', 'app title')
})
}
function wait (ms) {
if (ms === undefined) ms = 500 // Default: wait long enough for the UI to update
return new Promise(function (resolve, reject) {
setTimeout(resolve, ms)
})
}
function endTest (app, t, err) {
return app.stop().then(function () {
t.end(err)
})
}
function screenshotCreateOrCompare (app, t, name) {
const ssPath = path.join(__dirname, 'screenshots', process.platform, name + '.png')
console.log('Capturing ' + ssPath)
fs.ensureFileSync(ssPath)
const ssBuf = fs.readFileSync(ssPath)
return app.browserWindow.capturePage().then(function (buffer) {
if (ssBuf.length === 0) {
console.log('Saving screenshot ' + ssPath)
fs.writeFileSync(ssPath, buffer)
} else if (Buffer.compare(buffer, ssBuf) !== 0) {
return Promise.reject('Screenshot didn\'t match: ' + ssPath)
} else {
return Promise.resolve()
}
})
}