Files
angular__angular/e2e/screenshot.ts
T
Paul Gschwendtner d9f57001cd build: run tslint against e2e directory (#2945)
* Now runs TSLint also against the e2e/ directory.
* Add comments for the different tasks in the lint file.
* Refactors the screenshot e2e file to pass TSLint and also to be less nested.
2017-02-06 15:00:26 -08:00

59 lines
1.4 KiB
TypeScript

import * as fs from 'fs';
import * as path from 'path';
import {browser} from 'protractor';
const OUTPUT_DIR = './screenshots/';
let currentJasmineSpecName = '';
/** Adds a custom jasmine reporter that simply keeps track of the current test name. */
function initializeEnvironment(jasmine: any) {
let reporter = new jasmine.JsApiReporter({});
reporter.specStarted = function(result: any) {
currentJasmineSpecName = result.fullName;
};
jasmine.getEnv().addReporter(reporter);
}
initializeEnvironment(jasmine);
export class Screenshot {
id: string;
/** The filename used to store the screenshot. */
get filename(): string {
return this.id
.toLowerCase()
.replace(/\s/g, '_')
.replace(/[^/a-z0-9_]+/g, '')
+ '.screenshot.png';
}
/** The full path to the screenshot */
get fullPath(): string {
return path.resolve(OUTPUT_DIR, this.filename);
}
constructor(id: string) {
this.id = `${currentJasmineSpecName} ${id}`;
browser.takeScreenshot().then(png => this.storeScreenshot(png));
}
/** Replaces the existing screenshot with the newly generated one. */
storeScreenshot(png: any) {
if (!fs.existsSync(OUTPUT_DIR)) {
fs.mkdirSync(OUTPUT_DIR, '744');
}
if (fs.existsSync(OUTPUT_DIR)) {
fs.writeFileSync(this.fullPath, png, {encoding: 'base64' });
}
}
}
export function screenshot(id: string) {
if (process.env['TRAVIS']) {
return new Screenshot(id);
}
}