Skip to content

Commit

Permalink
fix(protractor.mock): Add support for disabled selenium control flow
Browse files Browse the repository at this point in the history
  • Loading branch information
mdasberg committed Oct 25, 2017
1 parent 5f8c376 commit af3eec5
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions templates/protractor.mock.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
(function () {
(() => {
'use strict';

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

let _handleRequest = function (httpMethod, urlSuffix, opts, errorMessage) {
const deferred = protractor.promise.defer();
request(httpMethod, baseUrl + urlSuffix, opts).done((res) => {
if (res.statusCode !== 200) {
deferred.reject(errorMessage);
} else {
deferred.fulfill();
}
});
return deferred.promise;
};

const ProtractorMock = function () {
function NgApimockHeader($http, ngApimockInstance) {
$http.defaults.headers.common['ngapimockid'] = ngApimockInstance.ngapimockid;
Expand All @@ -19,7 +31,7 @@
};

/** Make sure that angular uses the ngapimock identifier for the requests. */
browser.getProcessedConfig().then(function (config) {
browser.getProcessedConfig().then((config) => {
// As of protractor 5.0.0 the flag config.useAllAngular2AppRoots has been deprecated, to let protractor tell
// ngApimock that Angular 2 is used a custom object needs to be provided with the angular version in it
// See: https://github.com/angular/protractor/blob/master/CHANGELOG.md#features-2
Expand All @@ -43,6 +55,16 @@
} else {
browser.addMockModule('ngApimock', ProtractorMock, {'ngapimockid': ngapimockid})
}

if (config.SELENIUM_PROMISE_MANAGER === false) {
_handleRequest = function (httpMethod, urlSuffix, opts, errorMessage) {
return new Promise((resolve, reject) => {
request(httpMethod, baseUrl + urlSuffix, opts).done((res) => {
return res.statusCode === 200 ? resolve() : reject(errorMessage);
});
});
}
}
});


Expand Down Expand Up @@ -147,26 +169,18 @@
* @private
*/
function _execute(httpMethod, urlSuffix, options, errorMessage) {
const deferred = protractor.promise.defer(),
opts = {
headers: {
'Content-Type': 'application/json',
'ngapimockid': ngapimockid
}
};
const opts = {
headers: {
'Content-Type': 'application/json',
'ngapimockid': ngapimockid
}
};

if (options !== undefined) {
opts.json = options;
}

request(httpMethod, baseUrl + urlSuffix, opts).done(function (res) {
if (res.statusCode !== 200) {
deferred.reject(errorMessage);
} else {
deferred.fulfill();
}
});
return deferred.promise;
return _handleRequest(httpMethod, urlSuffix, opts, errorMessage);
}


Expand Down

0 comments on commit af3eec5

Please sign in to comment.