diff --git a/lib/clientsidescripts.js b/lib/clientsidescripts.js index 52196ce62..5c3399fce 100644 --- a/lib/clientsidescripts.js +++ b/lib/clientsidescripts.js @@ -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(); + } +}; diff --git a/lib/protractor.js b/lib/protractor.js index 9f228136a..562fa2ec1 100644 --- a/lib/protractor.js +++ b/lib/protractor.js @@ -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. */ diff --git a/spec/basic/lib_spec.js b/spec/basic/lib_spec.js index 3fc178720..8ae5883a4 100644 --- a/spec/basic/lib_spec.js +++ b/spec/basic/lib_spec.js @@ -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'); + }); }) });