chore: Refactor the situation a bit for protractor. Can run locally now

This commit is contained in:
Hans Larsen
2016-04-01 14:39:01 -07:00
parent 1f995c423a
commit 5a6657a4fb
5 changed files with 29 additions and 118 deletions
-98
View File
@@ -1,98 +0,0 @@
"use strict";
const path = require('path');
const child_process = require('child_process');
const mapnik = require('mapnik');
const SHA = process.env.TRAVIS_COMMIT;
const LFS_BASE_URL = 'https://media.githubusercontent.com/media/angular/material2';
/**
* Generates a screenshot from the current state of the Protractor test and compares it to the
* previous stored screenshot. If the screenshots do not match or if no existing screenshot is
* found, an error will be thrown. In both cases, the new screenshot will be stored so that it can
* be added to git.
*/
class Screenshot {
/**
* The filename used to store the screenshot
* @returns {string}
*/
get filename() {
return this.id
.toLowerCase()
.replace(/[ :\/]/g, '_')
.replace(/[^/a-z0-9_]+/g, '')
+ '.screenshot.png';
}
/**
* The full path to the screenshot
* @returns {string}
*/
get path() {
return path.resolve(__dirname, '..', 'screenshots', this.filename);
}
/**
* @param {string} id A unique identifier used for the screenshot
*/
constructor(id) {
this.id = id;
this.url = `${LFS_BASE_URL}/${SHA}/screenshots/${this.filename}`;
browser.takeScreenshot().then(png => this.storeScreenshot(png));
}
/**
* Stores a local copy of the screenshot for future comparison
* @param {string} png The base64-encoded screenshot generated from the current browser state
*/
storeScreenshot(png) {
console.info(`[STATUS] Generated new screenshot for "${this.id}"`);
this.png = mapnik.Image.fromBytes(new Buffer(png, 'base64'));
if (SHA) {
this.downloadFromGithub();
} else {
this.compareScreenshots();
}
}
/**
* Since we are using `git-lfs`, screenshots are not necessarily available within our local
* directory. To get around this, we download the latest screenshot from Github.
*/
downloadFromGithub() {
console.info(`[STATUS] Downloading screenshot from Github: ${this.url} => ${this.path}`);
child_process.execSync(`curl ${this.url} > "${this.path}"`);
this.compareScreenshots();
}
/**
* Compares the generated screenshot to the existing screenshot. If it does not match, an error
* will be thrown.
*/
compareScreenshots() {
console.info(`[STATUS] Comparing screenshots`);
try {
let referenceScreenshot = mapnik.Image.open(this.path);
this.overwriteExistingScreenshot();
if (referenceScreenshot.compare(this.png)) {
throw `screenshot "${this.id}" has changed.`;
} else {
console.info('[STATUS] Screenshot has not changed');
}
} catch (e) {
console.info('[STATUS] Unable to manually load screenshot, skipping comparison');
}
}
/**
* Replaces the existing screenshot with the newly generated one.
*/
overwriteExistingScreenshot() {
console.info(`[STATUS] Saving new screenshot`);
this.png.save(this.path);
}
}
module.exports = (id) => new Screenshot(id);
-2
View File
@@ -1,5 +1,3 @@
const screenshot = require('./_screenshot.js');
describe('hello, protractor', function () {
describe('index', function () {
browser.get('/');
+2 -3
View File
@@ -15,7 +15,7 @@
"tslint": "tslint -c tslint.json 'src/**/*.ts'",
"typings": "typings install --ambient",
"postinstall": "npm run typings",
"e2e": "protractor ./protractor.conf.js",
"e2e": "protractor test/protractor.conf.js",
"inline-resources": "gulp inline-resources"
},
"version": "2.0.0-alpha.1",
@@ -27,8 +27,6 @@
"angular2": "2.0.0-beta.12",
"es6-promise": "^3.0.2",
"es6-shim": "^0.35.0",
"mapnik": "^3.5.2",
"protractor": "^3.1.1",
"reflect-metadata": "0.1.2",
"rxjs": "5.0.0-beta.2",
"systemjs": "0.19.4",
@@ -60,6 +58,7 @@
"karma-firefox-launcher": "^0.1.7",
"karma-jasmine": "^0.3.6",
"karma-sauce-launcher": "^0.2.14",
"protractor": "^3.1.1",
"strip-ansi": "^3.0.0",
"symlink-or-copy": "^1.0.1",
"ts-node": "^0.5.5",
-15
View File
@@ -1,15 +0,0 @@
var key = require('./scripts/sauce/sauce_config.js');
exports.config = {
useAllAngular2AppRoots: true,
specs: [ './e2e/**/*.e2e.js' ],
baseUrl: 'http://localhost:4200',
sauceUser: process.env.SAUCE_USERNAME,
sauceKey: key,
capabilities: {
'browserName': 'chrome',
'tunnel-identifier': process.env.TRAVIS_JOB_NUMBER,
'build': process.env.TRAVIS_JOB_NUMBER,
'name': 'Material 2 E2E Tests'
}
};
+27
View File
@@ -0,0 +1,27 @@
/**
* This file is Node ES6 and will not be translated to Dart.
*/
const path = require('path');
const E2E_BASE_URL = process.env['E2E_BASE_URL'] || 'http://localhost:4200';
const config = {
useAllAngular2AppRoots: true,
specs: [ path.join(__dirname, '../e2e/**/*.e2e.js') ],
baseUrl: E2E_BASE_URL
};
if (process.env['TRAVIS'] !== undefined) {
const key = require('../scripts/sauce/sauce_config');
config.sauceUser = process.env['SAUCE_USERNAME'];
config.sauceKey = key;
config.capabilities = {
'browserName': 'chrome',
'tunnel-identifier': process.env['TRAVIS_JOB_NUMBER'],
'build': process.env['TRAVIS_JOB_NUMBER'],
'name': 'Material 2 E2E Tests'
};
}
exports.config = config;