getLocationAbsUrl vs getCurrentUrl

后端 未结 4 1651
既然无缘
既然无缘 2021-02-18 22:36

In protractor, globally available browser object has two methods:

  • getLocationAbsUrl()

Returns the current absolute url f

相关标签:
4条回答
  • 2021-02-18 22:58

    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().

    0 讨论(0)
  • 2021-02-18 22:59

    As stated previously the getLocationAbsUrl - returns a full path to the current location. browser.get('http://angular.github.io/protractor/#/api'); expect(browser.getLocationAbsUrl()) .toBe('http://angular.github.io/protractor/#/api');

    The getCurrentUrl is utilized in a promise (schedule) to retrieve the URL of the current page. Typically, you would only use the getCurrentUrl when scheduling.

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

    0 讨论(0)
  • 2021-02-18 23:11

    Basically they should do the same thing, return the URL of the page.

    From this bug report:

    https://github.com/angular/protractor/issues/132

    it seems that IEDriver for Selenium was always returning the first loaded page, not the current one. So the protractor team implemented a JS call to Angular's $location to always return the URL correctly via Javascript, not Webdriver protocol.

    0 讨论(0)
  • 2021-02-18 23:11

    Note that getLocationAbsUrl() is now deprecated, and should not be use anymore. You should use getCurrentUrl() instead.

    Here is the related Github commit by Protractor.

    0 讨论(0)
提交回复
热议问题