Files
Brandy Carney d93070a7d5 chore(gulp): update e2e and snapshot (#8846)
* chore(e2e): add livereload server and clean task for e2e

* chore(e2e): refactor to use gulp connect and open

* chore(e2e): WIP remove open, add formatting, add reload task

* wip(e2e): use SystemJS for faster dev e2e rebuilds

* chore(e2e): wip removing old gulp file, old e2e task

update template for e2e and port number

* chore(e2e): wip add SystemJS for dev build of e2e, use those tasks instead

* chore(e2e): uncomment out range components

* chore(e2e): wip fix paths for the e2e tempate

* chore(scripts): update README back to the old way

* chore(e2e): code cleanup

* chore(e2e): split tasks into dev and prod, put common tasks in e2e

* chore(e2e): rename e2e templates and add to readme

* chore(e2e): fix dev build so it will work with snapshot

* chore(snapshot): get snapshot working with dev and prod builds
2016-10-21 23:59:20 +02:00

126 lines
4.0 KiB
TypeScript

import { spawn } from 'child_process';
import { createServer } from 'http';
import { join, resolve } from 'path';
import * as connect from 'connect';
import { task } from 'gulp';
import * as serveStatic from 'serve-static';
import { argv } from 'yargs';
import { DIST_E2E_ROOT, DIST_E2E_COMPONENTS_ROOT, PROJECT_ROOT, SCRIPTS_ROOT } from '../constants';
import { mergeObjects } from '../util';
task('snapshot', ['e2e.clean', 'e2e.prod'], (done: Function) => {
snapshot(false, false, done);
});
task('snapshot.skipBuild', ['e2e.sass'], (done: Function) => {
snapshot(false, false, done);
});
task('snapshot.dev', ['e2e.clean', 'e2e'], (done: Function) => {
snapshot(false, true, done);
});
task('snapshot.quick', ['e2e.sass'], (done: Function) => {
snapshot(true, true, done);
});
function snapshot(quickMode: boolean, devMode: boolean, callback: Function) {
const snapshotConfig = require('../../snapshot/snapshot.config').config;
const protractorConfigFile = resolve(SCRIPTS_ROOT, 'snapshot/protractor.config.js');
const snapshotDefaults = snapshotConfig.platformDefaults || {};
const snapshotValues: any = mergeObjects(snapshotDefaults, argv || {});
if (!snapshotConfig.accessKey || !snapshotConfig.accessKey.length) {
console.error('Missing IONIC_SNAPSHOT_KEY environment variable');
return callback(new Error('Missing IONIC_SNAPSHOT_KEY environment variable'));
}
let component = '*';
let e2eSpecs = '*';
const folderArg: string = argv.folder || argv.f;
if (folderArg && folderArg.length) {
const folderArgPaths = folderArg.split('/');
component = folderArgPaths[0];
if (folderArgPaths.length > 1) {
e2eSpecs = folderArgPaths[1];
}
}
var specs = join(DIST_E2E_COMPONENTS_ROOT, component, 'test', e2eSpecs, '*e2e.js');
if (devMode) specs = join(DIST_E2E_ROOT, component, e2eSpecs, '*e2e.js');
console.log('[snapshot] Running with', devMode ? 'Development' : 'Production', 'build');
console.log(`[snapshot] Specs: ${specs}`);
const testId = generateTestId();
console.log(`[snapshot] TestId: ${testId}`);
snapshotValues.params.test_id = testId;
snapshotValues.params.upload = !quickMode;
var protractorArgs = [
'--browser ' + snapshotValues.browser,
'--platform ' + snapshotValues.platform,
'--params.platform_id=' + snapshotValues.params.platform_id,
'--params.platform_index=' + snapshotValues.params.platform_index,
'--params.platform_count=' + snapshotValues.params.platform_count,
'--params.width=' + snapshotValues.params.width,
'--params.height=' + snapshotValues.params.height,
'--params.test_id=' + snapshotValues.params.test_id,
'--params.upload=' + snapshotValues.params.upload,
'--params.dev=' + devMode,
'--specs=' + specs
];
return protractor(callback, [protractorConfigFile].concat(protractorArgs), testId);
}
function protractor(callback, args, testId: string) {
const buildConfig = require('../../build/config');
const app = connect().use(serveStatic(PROJECT_ROOT));
const protractorHttpServer = createServer(app).listen(buildConfig.protractorPort);
console.log(`Serving ${process.cwd()} on http://localhost:${buildConfig.protractorPort}`);
const child = spawn('protractor', args, {
stdio: [process.stdin, process.stdout, 'pipe']
});
let errored = false;
let callbackCalled = false;
child.stderr.on('data', function(data) {
protractorHttpServer.close();
console.error(data.toString());
if (!errored) {
errored = true;
if (!callbackCalled) {
callback('Protractor tests failed.');
callbackCalled = true;
}
}
});
child.on('exit', function() {
protractorHttpServer.close();
if (!callbackCalled) {
console.log(`[snapshot] TestId: ${testId}`);
callback();
callbackCalled = true;
}
});
}
function generateTestId() {
let chars = 'abcdefghjkmnpqrstuvwxyz';
let id = chars.charAt(Math.floor(Math.random() * chars.length));
chars += '0123456789';
while (id.length < 3) {
id += chars.charAt(Math.floor(Math.random() * chars.length));
}
return id;
}