|
1 | | -(function () { |
| 1 | +(() => { |
2 | 2 | 'use strict'; |
3 | 3 |
|
4 | 4 | const path = require('path'); |
5 | 5 | const ngapimockid = _require('uuid').v4(); |
6 | 6 | const request = _require('then-request'); |
7 | 7 | const baseUrl = _require('url-join')(browser.baseUrl, 'ngapimock'); |
8 | 8 |
|
| 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 | + |
9 | 21 | const ProtractorMock = function () { |
10 | 22 | function NgApimockHeader($http, ngApimockInstance) { |
11 | 23 | $http.defaults.headers.common['ngapimockid'] = ngApimockInstance.ngapimockid; |
|
19 | 31 | }; |
20 | 32 |
|
21 | 33 | /** Make sure that angular uses the ngapimock identifier for the requests. */ |
22 | | - browser.getProcessedConfig().then(function (config) { |
| 34 | + browser.getProcessedConfig().then((config) => { |
23 | 35 | // As of protractor 5.0.0 the flag config.useAllAngular2AppRoots has been deprecated, to let protractor tell |
24 | 36 | // ngApimock that Angular 2 is used a custom object needs to be provided with the angular version in it |
25 | 37 | // See: https://github.com/angular/protractor/blob/master/CHANGELOG.md#features-2 |
|
43 | 55 | } else { |
44 | 56 | browser.addMockModule('ngApimock', ProtractorMock, {'ngapimockid': ngapimockid}) |
45 | 57 | } |
| 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 | + } |
46 | 68 | }); |
47 | 69 |
|
48 | 70 |
|
|
147 | 169 | * @private |
148 | 170 | */ |
149 | 171 | 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 | + }; |
157 | 178 |
|
158 | 179 | if (options !== undefined) { |
159 | 180 | opts.json = options; |
160 | 181 | } |
161 | 182 |
|
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); |
170 | 184 | } |
171 | 185 |
|
172 | 186 |
|
|
0 commit comments