Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getLocationAbsUrl vs getCurrentUrl

In protractor, globally available browser object has two methods:

  • getLocationAbsUrl()

Returns the current absolute url from AngularJS.

  • getCurrentUrl()

Schedules a command to retrieve the URL of the current page.

It is not quite clear and obvious what is the difference between the two. To this very moment, I've been using getCurrentUrl() only.

When should we use getLocationAbsUrl()? Which use-cases does it cover?


I cannot recall anything similar to getLocationAbsUrl() in other selenium language bindings. It looks pretty much protractor-specific.

like image 460
alecxe Avatar asked Jun 11 '15 19:06

alecxe


1 Answers

GitHub source for getCurrentUrl

webdriver.WebDriver.prototype.getCurrentUrl = function() {
  return this.schedule(
      new webdriver.Command(webdriver.CommandName.GET_CURRENT_URL),
      'WebDriver.getCurrentUrl()');
};

Uses the schedule() -> command() wrappers to resolve a promise from WebDriver.getCurrentUrl()


GitHub source for Protractor.getLocationAbsUrl

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();
};

Simply a wrapper around $location.absUrl() with a wait for the AngularJS library to load


Current URL vs Absolute URL

given app URL:

http://www.example.com/home/index.html#/Home

Current URL resolves to more of a URI

/home/index.html#/Home

Absolute URL resolves to

http://www.example.com/home/index.html#/Home

When do you want to use absolute URL: You want to use the Full Domain URL, rather than the local navigation (URI), you want the Absolute URL.

  • If your application makes calls for the Current URL, your tests should call getCurrentUrl().

  • If your code makes requests for Absolute URL, your tests should call getLocationAbsUrl().

like image 125
Dave Alperovich Avatar answered Oct 05 '22 05:10

Dave Alperovich