Skip to content

Commit af3eec5

Browse files
committed
fix(protractor.mock): Add support for disabled selenium control flow
1 parent 5f8c376 commit af3eec5

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

templates/protractor.mock.js

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1-
(function () {
1+
(() => {
22
'use strict';
33

44
const path = require('path');
55
const ngapimockid = _require('uuid').v4();
66
const request = _require('then-request');
77
const baseUrl = _require('url-join')(browser.baseUrl, 'ngapimock');
88

9+
let _handleRequest = function (httpMethod, urlSuffix, opts, errorMessage) {
10+
const deferred = protractor.promise.defer();
11+
request(httpMethod, baseUrl + urlSuffix, opts).done((res) => {
12+
if (res.statusCode !== 200) {
13+
deferred.reject(errorMessage);
14+
} else {
15+
deferred.fulfill();
16+
}
17+
});
18+
return deferred.promise;
19+
};
20+
921
const ProtractorMock = function () {
1022
function NgApimockHeader($http, ngApimockInstance) {
1123
$http.defaults.headers.common['ngapimockid'] = ngApimockInstance.ngapimockid;
@@ -19,7 +31,7 @@
1931
};
2032

2133
/** Make sure that angular uses the ngapimock identifier for the requests. */
22-
browser.getProcessedConfig().then(function (config) {
34+
browser.getProcessedConfig().then((config) => {
2335
// As of protractor 5.0.0 the flag config.useAllAngular2AppRoots has been deprecated, to let protractor tell
2436
// ngApimock that Angular 2 is used a custom object needs to be provided with the angular version in it
2537
// See: https://github.com/angular/protractor/blob/master/CHANGELOG.md#features-2
@@ -43,6 +55,16 @@
4355
} else {
4456
browser.addMockModule('ngApimock', ProtractorMock, {'ngapimockid': ngapimockid})
4557
}
58+
59+
if (config.SELENIUM_PROMISE_MANAGER === false) {
60+
_handleRequest = function (httpMethod, urlSuffix, opts, errorMessage) {
61+
return new Promise((resolve, reject) => {
62+
request(httpMethod, baseUrl + urlSuffix, opts).done((res) => {
63+
return res.statusCode === 200 ? resolve() : reject(errorMessage);
64+
});
65+
});
66+
}
67+
}
4668
});
4769

4870

@@ -147,26 +169,18 @@
147169
* @private
148170
*/
149171
function _execute(httpMethod, urlSuffix, options, errorMessage) {
150-
const deferred = protractor.promise.defer(),
151-
opts = {
152-
headers: {
153-
'Content-Type': 'application/json',
154-
'ngapimockid': ngapimockid
155-
}
156-
};
172+
const opts = {
173+
headers: {
174+
'Content-Type': 'application/json',
175+
'ngapimockid': ngapimockid
176+
}
177+
};
157178

158179
if (options !== undefined) {
159180
opts.json = options;
160181
}
161182

162-
request(httpMethod, baseUrl + urlSuffix, opts).done(function (res) {
163-
if (res.statusCode !== 200) {
164-
deferred.reject(errorMessage);
165-
} else {
166-
deferred.fulfill();
167-
}
168-
});
169-
return deferred.promise;
183+
return _handleRequest(httpMethod, urlSuffix, opts, errorMessage);
170184
}
171185

172186

0 commit comments

Comments
 (0)