Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

Commit b7afa87

Browse files
bmenantjuliemr
authored andcommitted
feat(addMockModule): allow additional parameters
Allow Protractor’s 'addMockModule' method to pass context to its mocks, providing an argument to the script which overrides a module. Rely on the WebDriver’s 'executeScript' method. Closes #695
1 parent 95093c3 commit b7afa87

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

lib/protractor.js

+16-6
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,8 @@ var Protractor = function(webdriver, opt_baseUrl, opt_rootElement) {
582582
this.moduleNames_ = [];
583583

584584
this.moduleScripts_ = [];
585+
586+
this.moduleArgs_ = [];
585587
};
586588

587589
/**
@@ -846,10 +848,14 @@ Protractor.prototype.isElementPresent = function(locatorOrElement, varArgs) {
846848
*
847849
* @param {!string} name The name of the module to load or override.
848850
* @param {!string|Function} script The JavaScript to load the module.
851+
* @param {...*} varArgs Any additional arguments will be provided to
852+
* the script and may be referenced using the `arguments` object.
849853
*/
850854
Protractor.prototype.addMockModule = function(name, script) {
851855
this.moduleNames_.push(name);
852856
this.moduleScripts_.push(script);
857+
var moduleArgs = Array.prototype.slice.call(arguments, 2);
858+
this.moduleArgs_.push(moduleArgs);
853859
};
854860

855861
/**
@@ -858,6 +864,7 @@ Protractor.prototype.addMockModule = function(name, script) {
858864
Protractor.prototype.clearMockModules = function() {
859865
this.moduleNames_ = [];
860866
this.moduleScripts_ = [];
867+
this.moduleArgs_ = [];
861868
};
862869

863870
/**
@@ -868,6 +875,7 @@ Protractor.prototype.removeMockModule = function(name) {
868875
var index = this.moduleNames_.indexOf(name);
869876
this.moduleNames_.splice(index, 1);
870877
this.moduleScripts_.splice(index, 1);
878+
this.moduleArgs_.splice(index, 1);
871879
};
872880

873881
/**
@@ -924,7 +932,9 @@ Protractor.prototype.get = function(destination, opt_timeout) {
924932
// is called.
925933
for (var i = 0; i < this.moduleScripts_.length; ++i) {
926934
var name = this.moduleNames_[i];
927-
this.driver.executeScript(this.moduleScripts_[i]).
935+
var executeScriptArgs = [this.moduleScripts_[i]].
936+
concat(this.moduleArgs_[i]);
937+
this.driver.executeScript.apply(this, executeScriptArgs).
928938
then(null, function(err) {
929939
throw 'Error wile running module script ' + name +
930940
': ' + err.message;
@@ -952,16 +962,16 @@ Protractor.prototype.refresh = function(opt_timeout) {
952962

953963
if (self.ignoreSynchronization) {
954964
return self.driver.navigate().refresh();
955-
}
965+
}
956966

957967
return self.driver.executeScript('return window.location.href').then(function(href) {
958968
return self.get(href, timeout);
959969
});
960-
};
970+
};
961971

962-
/**
963-
* Mixin navigation methods back into the navigation object so that
964-
* they are invoked as before, i.e. driver.navigate().refresh()
972+
/**
973+
* Mixin navigation methods back into the navigation object so that
974+
* they are invoked as before, i.e. driver.navigate().refresh()
965975
*/
966976
Protractor.prototype.navigate = function() {
967977
var nav = this.driver.navigate();

spec/basic/mockmodule_spec.js

+24-9
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,20 @@ describe('mock modules', function() {
99
newModule.value('version', '2');
1010
};
1111

12-
// A second module overriding the 'version' service.
12+
// A second module overriding the 'version' service.
1313
// This module shows the use of a string for the load
1414
// function.
1515
// TODO(julie): Consider this syntax. Should we allow loading the
1616
// modules from files? Provide helpers?
1717
var mockModuleB = "angular.module('moduleB', []).value('version', '3');";
1818

19+
// A third module overriding the 'version' service. This function
20+
// references the additional argument provided through addMockModule().
21+
var mockModuleC = function () {
22+
var newModule = angular.module('moduleC', []);
23+
newModule.value('version', arguments[1]);
24+
};
25+
1926
afterEach(function() {
2027
browser.clearMockModules();
2128
});
@@ -48,24 +55,32 @@ describe('mock modules', function() {
4855
expect(element(by.css('[app-version]')).getText()).toEqual('2');
4956
});
5057

58+
it('should have the version provided as fourth parameter through the Module C', function() {
59+
browser.addMockModule('moduleC', mockModuleC, 'unused', '42');
60+
61+
browser.get('index.html');
62+
63+
expect(element(by.css('[app-version]')).getText()).toEqual('42');
64+
});
65+
5166
it('should load mock modules after refresh', function() {
5267
browser.addMockModule('moduleA', mockModuleA);
5368

54-
browser.get('index.html');
69+
browser.get('index.html');
5570
expect(element(by.css('[app-version]')).getText()).toEqual('2');
5671

5772
browser.navigate().refresh();
5873
expect(element(by.css('[app-version]')).getText()).toEqual('2');
5974
});
6075

61-
// Back and forward do NOT work at the moment because of an issue
76+
// Back and forward do NOT work at the moment because of an issue
6277
// bootstrapping with Angular
63-
/*
78+
/*
6479
it('should load mock modules after navigating back and forward', function() {
6580
browser.addMockModule('moduleA', mockModuleA);
6681
67-
browser.get('index.html');
68-
expect(element(by.css('[app-version]')).getText()).toEqual('2');
82+
browser.get('index.html');
83+
expect(element(by.css('[app-version]')).getText()).toEqual('2');
6984
7085
browser.get('index.html#/repeater');
7186
expect(element(by.css('[app-version]')).getText()).toEqual('2');
@@ -81,8 +96,8 @@ describe('mock modules', function() {
8196
it('should load mock modules after navigating back and forward from link', function() {
8297
browser.addMockModule('moduleA', mockModuleA);
8398

84-
browser.get('index.html');
85-
expect(element(by.css('[app-version]')).getText()).toEqual('2');
99+
browser.get('index.html');
100+
expect(element(by.css('[app-version]')).getText()).toEqual('2');
86101

87102
element(by.linkText('repeater')).click();
88103
expect(element(by.css('[app-version]')).getText()).toEqual('2');
@@ -92,6 +107,6 @@ describe('mock modules', function() {
92107

93108
browser.navigate().forward();
94109
expect(element(by.css('[app-version]')).getText()).toEqual('2');
95-
});
110+
});
96111

97112
});

0 commit comments

Comments
 (0)