GitHub link for getCurrentUrl function in WebDriver
webdriver.WebDriver.prototype.getCurrentUrl = function() {
return this.schedule(
new webdriver.Command(webdriver.CommandName.GET_CURRENT_URL),
'WebDriver.getCurrentUrl()');
};
This code utilizes the schedule()
-> command()
method wrappers to handle promises from the WebDriver.getCurrentUrl()
function.
GitHub link for Protractor's getLocationAbsUrl function
functions.getLocationAbsUrl = function(selector) {
var el = document.querySelector(selector);
if (angular.getTestability) {
return angular.getTestability(el).
getLocation();
}
return angular.element(el).injector().get('$location').absUrl();
};
This is simply a wrapper around the $location.absUrl()
method with an added wait for the AngularJS library to finish loading.
Difference between Current URL and Absolute URL
Assuming the app URL is:
http://www.example.com/home/index.html#/Home
The Current URL refers to more of a URI:
/home/index.html#/Home
The Absolute URL resolves to the full address:
http://www.example.com/home/index.html#/Home
When to use absolute URLs: If you need the complete domain address instead of just the local navigation path, opt for the Absolute URL.
If your application fetches the Current URL, utilize the getCurrentUrl()
function in tests.
If your code requests the Absolute URL, make use of the getLocationAbsUrl()
function in tests.