This repository has been archived by the owner on Aug 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
visualreview-protractor.js
182 lines (154 loc) · 5.42 KB
/
visualreview-protractor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
Copyright 2015 Xebia
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
const fs = require('fs');
const q = require('q');
const request = require('request');
const VrClient = require('./lib/vr-client.js');
const RUN_PID_FILE = '.visualreview-runid.pid';
const LOG_PREFIX = 'VisualReview-protractor: ';
var _hostname, _port, _client, _metaDataFn, _propertiesFn, _compareSettingsFn, _scheme, _strictSSL;
module.exports = function (options) {
_hostname = options.hostname || 'localhost';
_port = options.port || 7000;
_scheme = options.scheme || 'http';
_strictSSL = options.strictSSL === false ? false : true;
_disabled = options.disabled || false;
_client = new VrClient(_hostname, _port, _scheme, _strictSSL);
_metaDataFn = options.metaDataFn || function () { return {}; };
_propertiesFn = options.propertiesFn || function (capabilities) {
return {
'os': capabilities.get('platform'),
'browser': capabilities.get('browserName'),
'version': capabilities.get('version')
};
};
_compareSettingsFn = options.compareSettings || null;
return {
initRun: initRun,
takeScreenshot: takeScreenshot,
cleanup: cleanup
};
};
/**
* Initializes a run on the given project's suite name.
* @param projectName
* @param suiteName
* @param branchName
* @returns {Promise} a promise which resolves a new Run object with the fields run_id, project_id, suite_id and branch_name.
* If an error has occurred, the promise will reject with a string containing an error message.
*/
function initRun (projectName, suiteName, branchName) {
if(_disabled) {
return q.resolve();
}
return _client.createRun(projectName, suiteName, branchName).then( function (createdRun) {
if (createdRun) {
_logMessage('created run with ID ' + createdRun.run_id);
return _writeRunIdFile(JSON.stringify(createdRun));
}
}.bind(this),
function (err) {
_throwError(err);
});
}
/**
* Instructs Protractor to create a screenshot of the current browser and sends it to the VisualReview server.
* @param name the screenshot's name.
* @returns {Promise}
*/
function takeScreenshot (name) {
if(_disabled) {
return q.resolve();
}
return browser.driver.controlFlow().execute(function () {
return q.all([_getProperties(browser), _getMetaData(browser), browser.takeScreenshot(), _readRunIdFile()]).then(function (results) {
var properties = results[0],
metaData = results[1],
compareSettings = _compareSettingsFn,
png = results[2],
run = results[3];
if (!run || !run.run_id) {
_throwError('VisualReview-protractor: Could not send screenshot to VisualReview server, could not find any run ID. Was initRun called before starting this test? See VisualReview-protractor\'s documentation for more details on how to set this up.');
}
return _client.sendScreenshot(name, run.run_id, metaData, properties, compareSettings, png)
.catch(function (err) {
_throwError('Something went wrong while sending a screenshot to the VisualReview server. ' + err);
});
});
}.bind(this));
}
/**
* Cleans up any created temporary files.
* Call this in Protractor's afterLaunch configuration function.
* @param exitCode Protractor's exit code, used to indicate if the test run generated errors.
* @returns {Promise}
*/
function cleanup (exitCode) {
if(_disabled) {
return q.resolve();
}
var defer = q.defer();
_readRunIdFile().then(function (run) {
_logMessage('test finished. Your results can be viewed at: ' +
_scheme+'://' + _hostname + ':' + _port + '/#/' + run.project_id + '/' + run.suite_id + '/' + run.run_id + '/rp');
fs.unlink(RUN_PID_FILE, function (err) {
if (err) {
defer.reject(err);
} else {
defer.resolve();
}
});
});
return defer.promise;
}
function _writeRunIdFile (run) {
var defer = q.defer();
fs.writeFile(RUN_PID_FILE, run, function (err) {
if (err) {
defer.reject("VisualReview-protractor: could not write temporary runId file. " + err)
} else {
defer.resolve(run);
}
});
return defer.promise;
}
function _readRunIdFile () {
var defer = q.defer();
fs.readFile(RUN_PID_FILE, function (err, data) {
if (err) {
defer.reject("VisualReview-protractor: could not read temporary run pid file + " + err);
} else {
defer.resolve(JSON.parse(data));
}
});
return defer.promise;
}
function _getProperties (browser) {
return browser.getCapabilities()
.then(_propertiesFn)
.then(function (properties) {
return browser.manage().window().getSize().then(function (size) {
properties.resolution = size.width + 'x' + size.height;
return properties;
});
});
}
function _getMetaData (browser) {
return browser.getCapabilities().then(_metaDataFn);
}
function _logMessage (message) {
console.log(LOG_PREFIX + message);
}
function _throwError (message) {
throw new Error(LOG_PREFIX + message);
}