From 7e5b8183b58d8626176db32052faf2f65bf8bc51 Mon Sep 17 00:00:00 2001 From: Adam Bradley Date: Wed, 28 May 2014 09:00:13 -0500 Subject: [PATCH] init ionic snapshot Conflicts: README.md config/lib/ionic-snapshot.js --- .travis.yml | 1 + config/lib/ionic-snapshot.js | 192 +++++++++++++++++++++++++++++++++++ package.json | 4 +- 3 files changed, 196 insertions(+), 1 deletion(-) create mode 100755 config/lib/ionic-snapshot.js diff --git a/.travis.yml b/.travis.yml index cea190337c..593787b45d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,6 +13,7 @@ env: - secure: 2fk0b82A5q+FtU8M5MUIjxfbm+nxlDC3e7eHksgqfRY7v8odHWjJxJliBF50sP8qkVsop3HRONtxHcX09RpYfjAg2JVWd0QNtKF3PtpyJQO1Oe3oRBLCQSLM3H2O8vp0lXOrqjP3gKCVcqKZftPOJkR1HwwSmgCuDEkEBja/WlU= - secure: venr/q6rVyF2AbiT34XjWZvtXXOPKSusgbIcpNRoSBhs/fxJu6LKZ1byPJsdohBPLxsxd6J+3RJLU+dQul49uY6HCk7+uFETgQAU/H+DE3585eHaUwOyuOoYv/psoEuMXAopbAekJwZw/rnnpGh73CxychuQQUVDsJnt5QsbOwI= - secure: lLyJha5kpQJ7sONn5WsRc9t105GR+fBqYEVtM3ezaf8xUprPJ0WeKwneWEAUq5OLGXeXsovrqkdQgf/Abv8VueGsO5+l78btCEP1aGygoquavi6slx0HjUaWwXxcipIHf//K5CCtwmdbHncrD79BjpP++st3xLUS4FHMrCc3/f8= + - secure: BABaG57DhbhZJKC0ux1Uw89ZdxfARGH622SP9z8+biJFKK8dZTyHym4piZ8RuQCOgJj/4Frvz+dBulDN7pm0sPlr+q3szw4kditrOh/8o6nocg8GSldQ9Bt63Eoz5wNa4Y5OgOldgwRfchchcewjuo8FIX73CcSlMze0iUFq3Ts= notifications: slack: drifty:Wkm8lcIlUNlRXlglGRFxPNQa hipchat: diff --git a/config/lib/ionic-snapshot.js b/config/lib/ionic-snapshot.js new file mode 100755 index 0000000000..a52a1e391b --- /dev/null +++ b/config/lib/ionic-snapshot.js @@ -0,0 +1,192 @@ + +var IonicSnapshot = function(options) { + + // modules + var request = require('request'); + + var IonicReporter = 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.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 || 600; + self.startTime = Date.now(); + self.totalCompares = 0; + self.totalRMS = 0; + self.highestRMS = 0; + self.accessKey = options.accessKey; + self.ptor = protractor.getInstance(); + + console.log('Test Id:', self.testId); + + self.flow = protractor.promise.controlFlow(); + + // set browser size + self.width = options.width; + self.height = options.height; + if(self.width && self.height) { + self.flow.execute(function(){ + return browser.driver.manage().window().setSize(self.width, self.height); + }); + } + + self.flow.execute(function(){ + var d = protractor.promise.defer(); + + browser.getCapabilities().then(function (capabilities) { + + var data = { + compare: self.compare, + test_id: self.testId, + browser: capabilities.get('browserName'), + platform: capabilities.get('platform'), + version: capabilities.get('version') + }; + + request.post( + 'http://' + self.domain + '/' + self.groupId + '/' + self.appId + '/test', + { form: data }, + function (error, response, body) { + console.log(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) { + console.error('Error creating test'); + console.error(e); + } + } + d.fulfill(); + } + ); + + }); + return d.promise; + }); + + }; + + IonicReporter.prototype.reportSpecResults = function(spec) { + var self = this; + + 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.takeScreenshot().then(function(pngBase64){ + + 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, + description: spec.getFullName(), + png_base64: pngBase64, + url: currentAppUrl, + access_key: self.accessKey + }; + pngBase64 = null; + + request.post( + 'http://' + self.domain + '/screenshot', + { form: data }, + function (error, response, body) { + console.log(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) { + console.error('Error posting screenshot'); + console.error(e); + } + + var next = self.flow.getSchedule().toString(); + if(next.indexOf('.quit()') > 0) { + self.onComplete(d); + } else { + d.fulfill(); + } + } + ); + }); + + }); + + }); + + }); + + return d.promise; + }); + }; + + 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) { + console.log(body); + try { + var jsonData = JSON.parse(body); + if(jsonData.compare_url) { + var spawn = require('child_process').spawn; + spawn('open', [jsonData.compare_url]); + } + } catch(e) { + console.error(e); + } + d.fulfill(); + } + ); + } else { + d.fulfill(); + } + }; + + // get --test_id cmd line argument + for(var x=0; x