Skip to content

Commit 2be7446

Browse files
committed
feat(addMockModule): support any additional arguments
Allow Protractor’s 'addMockModule' method to pass context to its mocks, providing arguments to the script which overrides a module. Support any arguments provided in addition to the module’s name and script. Those arguments may be referenced from the script via the `arguments` object. Rely on the WebDriver’s 'executeScript' method. Closes angular#695
1 parent e110e63 commit 2be7446

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

lib/protractor.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -839,13 +839,14 @@ Protractor.prototype.isElementPresent = function(locatorOrElement, varArgs) {
839839
*
840840
* @param {!string} name The name of the module to load or override.
841841
* @param {!string|Function} script The JavaScript to load the module.
842-
* @param {*=} argument The argument provided to the script as
843-
* `arguments[0]`.
842+
* @param {*=} var_args Any additional arguments will be provided to
843+
* the script and may be referenced using the `arguments` object.
844844
*/
845-
Protractor.prototype.addMockModule = function(name, script, argument) {
845+
Protractor.prototype.addMockModule = function(name, script) {
846846
this.moduleNames_.push(name);
847847
this.moduleScripts_.push(script);
848-
this.moduleArgs_.push(argument);
848+
var moduleArgs = Array.prototype.slice.call(arguments, 2);
849+
this.moduleArgs_.push(moduleArgs);
849850
};
850851

851852
/**
@@ -922,7 +923,9 @@ Protractor.prototype.get = function(destination, opt_timeout) {
922923
// is called.
923924
for (var i = 0; i < this.moduleScripts_.length; ++i) {
924925
var name = this.moduleNames_[i];
925-
this.driver.executeScript(this.moduleScripts_[i], this.moduleArgs_[i]).
926+
var executeScriptArgs = [this.moduleScripts_[i]].
927+
concat(this.moduleArgs_[i]);
928+
this.driver.executeScript.apply(this, executeScriptArgs).
926929
then(null, function(err) {
927930
throw 'Error wile running module script ' + name +
928931
': ' + err.message;

spec/basic/mockmodule_spec.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ describe('mock modules', function() {
1717
var mockModuleB = "angular.module('moduleB', []).value('version', '3');";
1818

1919
// A third module overriding the 'version' service. This function
20-
// references the third argument provided through addMockModule().
20+
// references the fourth argument provided through addMockModule().
2121
var mockModuleC = function () {
2222
var newModule = angular.module('moduleC', []);
23-
newModule.value('version', arguments[0]);
23+
newModule.value('version', arguments[1]);
2424
};
2525

2626
afterEach(function() {
@@ -55,12 +55,12 @@ describe('mock modules', function() {
5555
expect(element(by.css('[app-version]')).getText()).toEqual('2');
5656
});
5757

58-
it('should have the version provided as third parameter through the Module C', function() {
59-
browser.addMockModule('moduleC', mockModuleC, '4');
58+
it('should have the version provided as fourth parameter through the Module C', function() {
59+
browser.addMockModule('moduleC', mockModuleC, 'foobar', '42');
6060

6161
browser.get('index.html');
6262

63-
expect(element(by.css('[app-version]')).getText()).toEqual('4');
63+
expect(element(by.css('[app-version]')).getText()).toEqual('42');
6464
});
6565

6666
it('should load mock modules after refresh', function() {

0 commit comments

Comments
 (0)