getLocationAbsUrl vs getCurrentUrl

getLocationAbsUrl vs getCurrentUrl

本文关键字:getCurrentUrl vs getLocationAbsUrl      更新时间:2023-09-26

在量角器中,全局可用的browser对象有两种方法:

  • getLocationAbsUrl()

返回来自 AngularJS 的当前绝对 url。

  • getCurrentUrl()

计划用于检索当前页的 URL 的命令。

两者之间有什么区别还不是很清楚和明显。直到现在,我一直在使用getCurrentUrl()

我们什么时候应该使用getLocationAbsUrl()?它涵盖哪些用例?


我不记得其他硒语言绑定中getLocationAbsUrl()类似的东西。它看起来几乎是量角器特定的。

getCurrentUrl 的 GitHub source

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

使用 schedule() -> command() 包装器解析来自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();
};

只需$location.absUrl()一个包装器,等待 AngularJS 库加载


当前网址与绝对网址

给定的应用网址:

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

当前 URL 解析为更多 URI

/home/index.html#/Home

绝对网址解析为

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

何时要使用绝对 URL:要使用完整域 URL,而不是本地导航 (URI),需要绝对 URL

  • 如果应用程序调用当前 URL,则测试应调用 getCurrentUrl()

  • 如果代码发出对绝对 URL 的请求,则测试应调用 getLocationAbsUrl()

前所述getLocationAbsUrl - 返回当前位置的完整路径。 browser.get('http://angular.github.io/protractor/#/api'); expect(browser.getLocationAbsUrl()) .toBe('http://angular.github.io/protractor/#/api');

getCurrentUrl 在承诺(计划)中用于检索当前页面的 URL。 通常,您只会在计划时使用 getCurrentUrl。

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

基本上他们应该做同样的事情,返回页面的URL。

从这个错误报告中:

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

似乎 SeleniumIEDriver总是返回第一个加载的页面,而不是当前页面。因此,量角器团队实现了对 Angular $location的 JS 调用,以始终通过 Javascript 而不是 Webdriver 协议正确返回 URL

请注意,getLocationAbsUrl()现已弃用,不应再使用。您应该改用getCurrentUrl()

这是量角器的相关 Github 提交。