mirror of
https://github.com/ijprest/keyboard-layout-editor.git
synced 2026-04-27 17:40:44 -07:00
Testing: Added code to snap screenshots
This commit is contained in:
parent
4b43791e19
commit
7d8f090064
3 changed files with 70 additions and 0 deletions
|
|
@ -1,3 +1,10 @@
|
|||
capture = require('./screenshot.js');
|
||||
|
||||
function getSpecName() {
|
||||
var spec = jasmine.getEnv().currentSpec;
|
||||
return spec.description.split(' ').join('-');
|
||||
}
|
||||
|
||||
// Tests for keyboard-layout-editor
|
||||
describe('keyboard-layout-editor', function() {
|
||||
var home = 'http://localhost:8080/kb.html';
|
||||
|
|
@ -5,6 +12,8 @@ describe('keyboard-layout-editor', function() {
|
|||
// Simple launch test
|
||||
it('should launch without an error', function() {
|
||||
browser.get(home);
|
||||
browser.driver.manage().window().setSize(1280,720);
|
||||
capture.snap(getSpecName(), $('#keyboard'));
|
||||
});
|
||||
|
||||
// Check for exceptions thrown during each scenario
|
||||
|
|
|
|||
61
tests/screenshot.js
Normal file
61
tests/screenshot.js
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
var fs = require('fs');
|
||||
var lwip = require('lwip');
|
||||
|
||||
function capture(name) {
|
||||
browser.takeScreenshot().then(function(png) {
|
||||
var stream = fs.createWriteStream('./tests/screenshots/'+(name.split(' ').join('_'))+'.png');
|
||||
stream.write(new Buffer(png, 'base64'));
|
||||
stream.end();
|
||||
});
|
||||
}
|
||||
|
||||
exports.takeScreenshot = function(spec) {
|
||||
capture(spec.description);
|
||||
};
|
||||
|
||||
exports.takeScreenshotOnFailure = function(spec) {
|
||||
if(!spec.results().passed()) {
|
||||
capture(spec.description);
|
||||
}
|
||||
};
|
||||
|
||||
// snapshot a single element; inspired by snappit-mocha-protractor
|
||||
function saveImage(name, image, deferred) {
|
||||
return browser.controlFlow().execute(function() {
|
||||
image.writeFile('./tests/screenshots/'+(name.split(' ').join('_'))+'.png', function (err) {
|
||||
if (err) {
|
||||
console.log('error saving screenshot:', err);
|
||||
return deferred.reject();
|
||||
}
|
||||
return deferred.fulfill();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
exports.snap = function(name, elem) {
|
||||
return browser.controlFlow().execute(function() {
|
||||
return browser.takeScreenshot().then(function(png) {
|
||||
var deferred = protractor.promise.defer();
|
||||
lwip.open(new Buffer(png, 'base64'), 'png', function (err, image) {
|
||||
// Handle errors
|
||||
if(err) {
|
||||
console.log("error opening screenshot:", err);
|
||||
return deferred.reject();
|
||||
}
|
||||
// Crop the image to the desired element
|
||||
var parms = [elem.getLocation(), elem.getSize()]; // promises!
|
||||
return protractor.promise.all(parms).then(function(parms) {
|
||||
image.crop(parms[0].x, parms[0].y, parms[0].x+parms[1].width, parms[0].y+parms[1].height, function(err, image) {
|
||||
// Handle errors
|
||||
if(err) {
|
||||
console.log("error cropping screenshot:", err);
|
||||
return deferred.reject();
|
||||
}
|
||||
return saveImage(name, image, deferred);
|
||||
});
|
||||
});
|
||||
});
|
||||
return deferred.promise;
|
||||
});
|
||||
});
|
||||
};
|
||||
BIN
tests/screenshots/should-launch-without-an-error.png
Normal file
BIN
tests/screenshots/should-launch-without-an-error.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 14 KiB |
Loading…
Add table
Add a link
Reference in a new issue