getLocationAbsUrl vs getCurrentUrl

时间:2015-06-11 19:44:23

标签: javascript angularjs selenium selenium-webdriver protractor

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

  

从AngularJS返回当前绝对URL。

  

安排命令检索当前页面的URL。

目前尚不清楚和明显两者之间有什么区别。到目前为止,我一直在使用getCurrentUrl()

我们应该何时使用getLocationAbsUrl()?它包含哪些用例?

在其他selenium语言绑定中,我无法回想起与getLocationAbsUrl()类似的任何内容。它看起来非常类似于量角器。

4 个答案:

答案 0 :(得分:16)

getCurrentUrl

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

使用schedule() - > command()封包来解决来自WebDriver.getCurrentUrl()

的承诺

Protractor.getLocationAbsUrl

的GitHub来源
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

当前网址解析为更多URI

/home/index.html#/Home

绝对网址解析为

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

您何时要使用绝对网址:您希望使用完整域网址而不是本地导航(URI),您需要绝对网址

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

  • 如果您的代码请求绝对网址,您的测试应该调用getLocationAbsUrl()

答案 1 :(得分:2)

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

从这个错误报告:

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

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

答案 2 :(得分:2)

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

getCurrentUrl在promise(schedule)中用于检索当前页面的URL。通常,您只在调度时使用getCurrentUrl。

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

答案 3 :(得分:1)

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

以下是Protractor的相关Github commit

相关问题