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

Commit

Permalink
feat(addMockModule): allow additional parameters
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bmenant authored and juliemr committed May 20, 2014
1 parent 95093c3 commit b7afa87
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
22 changes: 16 additions & 6 deletions lib/protractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,8 @@ var Protractor = function(webdriver, opt_baseUrl, opt_rootElement) {
this.moduleNames_ = [];

this.moduleScripts_ = [];

this.moduleArgs_ = [];
};

/**
Expand Down Expand Up @@ -846,10 +848,14 @@ Protractor.prototype.isElementPresent = function(locatorOrElement, varArgs) {
*
* @param {!string} name The name of the module to load or override.
* @param {!string|Function} script The JavaScript to load the module.
* @param {...*} varArgs Any additional arguments will be provided to
* the script and may be referenced using the `arguments` object.
*/
Protractor.prototype.addMockModule = function(name, script) {
this.moduleNames_.push(name);
this.moduleScripts_.push(script);
var moduleArgs = Array.prototype.slice.call(arguments, 2);
this.moduleArgs_.push(moduleArgs);
};

/**
Expand All @@ -858,6 +864,7 @@ Protractor.prototype.addMockModule = function(name, script) {
Protractor.prototype.clearMockModules = function() {
this.moduleNames_ = [];
this.moduleScripts_ = [];
this.moduleArgs_ = [];
};

/**
Expand All @@ -868,6 +875,7 @@ Protractor.prototype.removeMockModule = function(name) {
var index = this.moduleNames_.indexOf(name);
this.moduleNames_.splice(index, 1);
this.moduleScripts_.splice(index, 1);
this.moduleArgs_.splice(index, 1);
};

/**
Expand Down Expand Up @@ -924,7 +932,9 @@ Protractor.prototype.get = function(destination, opt_timeout) {
// is called.
for (var i = 0; i < this.moduleScripts_.length; ++i) {
var name = this.moduleNames_[i];
this.driver.executeScript(this.moduleScripts_[i]).
var executeScriptArgs = [this.moduleScripts_[i]].
concat(this.moduleArgs_[i]);
this.driver.executeScript.apply(this, executeScriptArgs).
then(null, function(err) {
throw 'Error wile running module script ' + name +
': ' + err.message;
Expand Down Expand Up @@ -952,16 +962,16 @@ Protractor.prototype.refresh = function(opt_timeout) {

if (self.ignoreSynchronization) {
return self.driver.navigate().refresh();
}
}

return self.driver.executeScript('return window.location.href').then(function(href) {
return self.get(href, timeout);
});
};
};

/**
* Mixin navigation methods back into the navigation object so that
* they are invoked as before, i.e. driver.navigate().refresh()
/**
* Mixin navigation methods back into the navigation object so that
* they are invoked as before, i.e. driver.navigate().refresh()
*/
Protractor.prototype.navigate = function() {
var nav = this.driver.navigate();
Expand Down
33 changes: 24 additions & 9 deletions spec/basic/mockmodule_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@ describe('mock modules', function() {
newModule.value('version', '2');
};

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

// A third module overriding the 'version' service. This function
// references the additional argument provided through addMockModule().
var mockModuleC = function () {
var newModule = angular.module('moduleC', []);
newModule.value('version', arguments[1]);
};

afterEach(function() {
browser.clearMockModules();
});
Expand Down Expand Up @@ -48,24 +55,32 @@ describe('mock modules', function() {
expect(element(by.css('[app-version]')).getText()).toEqual('2');
});

it('should have the version provided as fourth parameter through the Module C', function() {
browser.addMockModule('moduleC', mockModuleC, 'unused', '42');

browser.get('index.html');

expect(element(by.css('[app-version]')).getText()).toEqual('42');
});

it('should load mock modules after refresh', function() {
browser.addMockModule('moduleA', mockModuleA);

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

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

// Back and forward do NOT work at the moment because of an issue
// Back and forward do NOT work at the moment because of an issue
// bootstrapping with Angular
/*
/*
it('should load mock modules after navigating back and forward', function() {
browser.addMockModule('moduleA', mockModuleA);
browser.get('index.html');
expect(element(by.css('[app-version]')).getText()).toEqual('2');
browser.get('index.html');
expect(element(by.css('[app-version]')).getText()).toEqual('2');
browser.get('index.html#/repeater');
expect(element(by.css('[app-version]')).getText()).toEqual('2');
Expand All @@ -81,8 +96,8 @@ describe('mock modules', function() {
it('should load mock modules after navigating back and forward from link', function() {
browser.addMockModule('moduleA', mockModuleA);

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

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

browser.navigate().forward();
expect(element(by.css('[app-version]')).getText()).toEqual('2');
});
});

});

0 comments on commit b7afa87

Please sign in to comment.