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

Commit

Permalink
feat(protractor): add the browser.setLocation method to perform in-pa…
Browse files Browse the repository at this point in the history
…ge navigation

Allow a faster way to navigate within the app.
The current browser.get method forces the entire app to load every time you navigate to a new page.
The proposed browser.setLocation method uses the same format as $location.url().

Closes #368
  • Loading branch information
flegall authored and juliemr committed Apr 4, 2014
1 parent 2f0c789 commit 54060b7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/clientsidescripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,3 +528,22 @@ clientSideScripts.getLocationAbsUrl = function(selector) {
var el = document.querySelector(selector);
return angular.element(el).injector().get('$location').absUrl();
};

/**
* Browse to another page using in-page navigation.
*
* @param {string} selector The selector housing an ng-app
* @param {string} url In page URL using the same syntax as $location.url(),
* /path?search=a&b=c#hash
*/
clientSideScripts.setLocation = function(selector, url) {
var el = document.querySelector(selector);
var $injector = angular.element(el).injector();
var $location = $injector.get('$location');
var $rootScope = $injector.get('$rootScope');

if (url !== $location.url()) {
$location.url(url);
$rootScope.$digest();
}
};
18 changes: 18 additions & 0 deletions lib/protractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,24 @@ Protractor.prototype.get = function(destination, opt_timeout) {
}, this.moduleNames_);
};

/**
* Browse to another page using in-page navigation.
*
* @param {string} url In page URL using the same syntax as $location.url()
* @returns {!webdriver.promise.Promise} A promise that will resolve once
* page has been changed.
*/
Protractor.prototype.setLocation = function(url) {
this.waitForAngular();
return this.driver.executeScript(clientSideScripts.setLocation, this.rootEl, url)
.then(function(browserErr) {
if (browserErr) {
throw 'Error while navigating to \'' + url + '\' : ' +
JSON.stringify(browserErr);
}
});
};

/**
* Returns the current absolute url from AngularJS.
*/
Expand Down
9 changes: 9 additions & 0 deletions spec/basic/lib_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,14 @@ describe('protractor library', function() {
expect(browser.getLocationAbsUrl()).
toEqual('http://localhost:'+port+'/index.html#/repeater');
});

it('should navigate to another url with setLocation', function() {
browser.get('index.html');

browser.setLocation('/repeater');

expect(browser.getLocationAbsUrl()).
toEqual('http://localhost:' + port + '/index.html#/repeater');
});
})
});

0 comments on commit 54060b7

Please sign in to comment.