Integration test: screenshots

This commit is contained in:
DC
2016-09-07 00:12:50 -07:00
parent fe8c3b190c
commit 967e161288
5 changed files with 13 additions and 6 deletions

View File

@@ -28,16 +28,19 @@ function createApp (t) {
// Starts the app, waits for it to load, returns a promise
function waitForLoad (app, t) {
return app.start().then(function () {
// Switch to the main window. Index 0 is apparently the hidden webtorrent window...
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')
// Note the window title is WebTorrent (BETA), this is the HTML <title>
t.equal(title, 'WebTorrent Desktop', 'html title')
})
}
// Returns a promise that resolves after 'ms' milliseconds. Default: 500
function wait (ms) {
if (ms === undefined) ms = 500 // Default: wait long enough for the UI to update
return new Promise(function (resolve, reject) {
@@ -45,24 +48,26 @@ function wait (ms) {
})
}
// Quit the app, end the test, either in success (!err) or failure (err)
function endTest (app, t, err) {
return app.stop().then(function () {
t.end(err)
})
}
// Takes a screenshot of the app
// If we already have a reference under test/screenshots, assert that they're the same
// Otherwise, create the reference screenshot: test/screenshots/<platform>/<name>.png
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 {
t.ok(Buffer.compare(buffer, ssBuf) === 0, 'screenshot ' + name)
return Promise.resolve()
}
})