update snapshot protractor test

Less http requests and now using the golang service
This commit is contained in:
Adam Bradley
2014-06-02 14:39:58 -05:00
parent 2bd1a2576c
commit dc0b4a46b8

View File

@@ -4,7 +4,6 @@ var IonicSnapshot = function(options) {
// modules
var _ = require('lodash');
var request = require('request');
var colors = require('gulp-util').colors;
var log = console.log.bind(console, '[' + colors.cyan('IonicReporter') + ']');
@@ -12,135 +11,88 @@ var IonicSnapshot = function(options) {
var self = this;
// set options and defaults
self.compare = options.compare || 'master';
self.capabilityTestId = options.capabilityTestId || null;
self.compareCapabilityTestId = options.compareCapabilityTestId || null;
self.domain = options.domain || 'ionic-snapshot-go.appspot.com';
self.groupId = options.groupId || 'test_group';
self.appId = options.appId || 'test_app';
self.testId = options.testId || 'test_id';
self.domain = options.domain || 'ionic-snapshot.appspot.com';
self.sleepBetweenSpecs = options.sleepBetweenSpecs || 400;
self.startTime = Date.now();
self.totalCompares = 0;
self.totalRMS = 0;
self.highestRMS = 0;
self.accessKey = options.accessKey;
self.ptor = protractor.getInstance();
self.testId = browser.params.test_id || 'test_id';
self.platformId = browser.params.platform_id;
self.sleepBetweenSpecs = options.sleepBetweenSpecs || 450;
self.width = browser.params.width || -1;
self.height = browser.params.height || -1;
self.highestMismatch = 0;
self.flow = protractor.promise.controlFlow();
// set browser size
self.width = browser.params.width || -1;
self.height = browser.params.height || -1;
if(self.width > 0 && self.height > 0) {
self.flow.execute(function(){
return browser.driver.manage().window().setSize(self.width, self.height);
});
}
self.platformId = browser.params.platform_id;
log('init:', _.pick(self, ['testId', 'appId', 'width', 'height', 'platformId']));
self.flow.execute(function(){
var d = protractor.promise.defer();
browser.getCapabilities().then(function (capabilities) {
var data = {
compare: self.compare,
return browser.getCapabilities().then(function (capabilities) {
self.testData = {
group_id: self.groupId,
app_id: self.appId,
test_id: self.testId,
platform_id: self.platformId,
app_id: self.appId,
width: self.width,
height: self.height,
browser: capabilities.get('browserName'),
platform: capabilities.get('platform'),
version: capabilities.get('version')
version: capabilities.get('version'),
access_key: options.accessKey
};
log('init with data:', data);
request.post(
'http://' + self.domain + '/' + self.groupId + '/' + self.appId + '/test',
{ form: data },
function (error, response, body) {
log('init response:', body);
if(!error && response.statusCode == 200) {
try {
var jsonData = JSON.parse(body);
self.capabilityTestId = jsonData.capability_test_id;
self.compareResultId = jsonData.compare_result_id;
} catch(e) {
log(colors.red('init error creating test:'), e);
}
}
d.fulfill();
}
);
});
return d.promise;
});
process.on('exit', function() {
log(colors.green('Highest Mismatch:'), self.highestMismatch, '%');
});
log('init:', _.pick(self, ['testId', 'appId', 'width', 'height', 'platformId']));
};
IonicReporter.prototype.reportSpecResults = function(spec) {
var self = this;
if(!self.testData.total_specs) {
self.testData.total_specs = 0;
var allSpecs = jasmine.getEnv().currentRunner().specs()
for(var sId in allSpecs) {
self.testData.total_specs++;
}
}
self.flow.execute(function(){
var d = protractor.promise.defer();
browser.waitForAngular().then(function(){
self.ptor.getCurrentUrl().then(function(currentAppUrl){
browser.sleep(self.sleepBetweenSpecs).then(function(){
browser.sleep(self.sleepBetweenSpecs).then(function(){
browser.takeScreenshot().then(function(pngBase64){
log('spec:', spec.id + 1, 'of', self.testData.total_specs);
browser.takeScreenshot().then(function(pngBase64){
self.testData.spec_id = spec.id;
self.testData.description = spec.getFullName();
self.testData.highest_mismatch = self.highestMismatch;
self.testData.png_base64 = pngBase64;
pngBase64 = null;
var data = {
compare_result_id: self.compareResultId,
group_id: self.groupId,
app_id: self.appId,
test_id: self.testId,
capability_test_id: self.capabilityTestId,
spec_id: spec.id,
suite_id: spec.suite.id,
platform_id: self.platformId,
description: spec.getFullName(),
png_base64: pngBase64,
url: currentAppUrl,
access_key: self.accessKey,
width: self.width,
height: self.height
};
pngBase64 = null;
request.post(
'http://' + self.domain + '/screenshot',
{ form: data },
function (error, response, body) {
log('reportSpecResults:', body);
try {
var jsonData = JSON.parse(body);
self.totalCompares++;
self.totalRMS = self.totalRMS + jsonData.rms;
self.highestRMS = Math.max(self.highestRMS, jsonData.rms);
} catch(e) {
log(colors.red('reportSpecResults error posting screenshot:'), e);
}
var next = self.flow.getSchedule().toString();
if(next.indexOf('.quit()') > 0) {
self.onComplete(d);
} else {
d.fulfill();
}
request.post(
'http://' + self.domain + '/screenshot',
{ form: self.testData },
function (error, response, body) {
log('reportSpecResults:', body);
try {
var rspData = JSON.parse(body);
self.highestMismatch = Math.max(self.highestMismatch, rspData.Mismatch);
} catch(e) {
log(colors.red('reportSpecResults error posting screenshot:'), e);
}
);
});
d.fulfill();
}
);
});
});
@@ -151,48 +103,6 @@ var IonicSnapshot = function(options) {
});
};
IonicReporter.prototype.onComplete = function(d) {
var self = this;
var data = {
duration_ms: Date.now() - self.startTime,
total_compares: self.totalCompares,
highest_rms: self.highestRMS,
rms_average: (self.totalCompares > 0 ? (self.totalRMS / self.totalCompares) : 0),
access_key: self.accessKey
};
if(self.compareResultId) {
request.post(
'http://' + self.domain + '/' + self.groupId + '/' + self.appId + '/' + self.testId + '/' + self.compareResultId + '/complete',
{ form: data },
function (error, response, body) {
log('onComplete:', body);
try {
var jsonData = JSON.parse(body);
if(jsonData.compare_url) {
var spawn = require('child_process').spawn;
spawn('open', [jsonData.compare_url]);
}
} catch(e) {
log(colors.red('onComplete error:'), e);
}
d.fulfill();
}
);
} else {
d.fulfill();
}
};
options.testId = browser.params.test_id;
if(!options.testId) {
log(colors.red('--params.test_id error:'), 'unique ID required');
browser.driver.quit();
return;
}
this.jasmine.getEnv().addReporter( new IonicReporter(options) );
};