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

Commit 54060b7

Browse files
flegalljuliemr
authored andcommitted
feat(protractor): add the browser.setLocation method to perform in-page 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
1 parent 2f0c789 commit 54060b7

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

lib/clientsidescripts.js

+19
Original file line numberDiff line numberDiff line change
@@ -528,3 +528,22 @@ clientSideScripts.getLocationAbsUrl = function(selector) {
528528
var el = document.querySelector(selector);
529529
return angular.element(el).injector().get('$location').absUrl();
530530
};
531+
532+
/**
533+
* Browse to another page using in-page navigation.
534+
*
535+
* @param {string} selector The selector housing an ng-app
536+
* @param {string} url In page URL using the same syntax as $location.url(),
537+
* /path?search=a&b=c#hash
538+
*/
539+
clientSideScripts.setLocation = function(selector, url) {
540+
var el = document.querySelector(selector);
541+
var $injector = angular.element(el).injector();
542+
var $location = $injector.get('$location');
543+
var $rootScope = $injector.get('$rootScope');
544+
545+
if (url !== $location.url()) {
546+
$location.url(url);
547+
$rootScope.$digest();
548+
}
549+
};

lib/protractor.js

+18
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,24 @@ Protractor.prototype.get = function(destination, opt_timeout) {
903903
}, this.moduleNames_);
904904
};
905905

906+
/**
907+
* Browse to another page using in-page navigation.
908+
*
909+
* @param {string} url In page URL using the same syntax as $location.url()
910+
* @returns {!webdriver.promise.Promise} A promise that will resolve once
911+
* page has been changed.
912+
*/
913+
Protractor.prototype.setLocation = function(url) {
914+
this.waitForAngular();
915+
return this.driver.executeScript(clientSideScripts.setLocation, this.rootEl, url)
916+
.then(function(browserErr) {
917+
if (browserErr) {
918+
throw 'Error while navigating to \'' + url + '\' : ' +
919+
JSON.stringify(browserErr);
920+
}
921+
});
922+
};
923+
906924
/**
907925
* Returns the current absolute url from AngularJS.
908926
*/

spec/basic/lib_spec.js

+9
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,14 @@ describe('protractor library', function() {
102102
expect(browser.getLocationAbsUrl()).
103103
toEqual('http://localhost:'+port+'/index.html#/repeater');
104104
});
105+
106+
it('should navigate to another url with setLocation', function() {
107+
browser.get('index.html');
108+
109+
browser.setLocation('/repeater');
110+
111+
expect(browser.getLocationAbsUrl()).
112+
toEqual('http://localhost:' + port + '/index.html#/repeater');
113+
});
105114
})
106115
});

0 commit comments

Comments
 (0)