如何使用量角器获取当前网址?

时间:2019-01-31 10:13:25

标签: protractor

我已经看到了this的问题,并阅读了this文档,但仍然无法获得作为string值的网址。

我知道在protractor中,您应该使用以下代码获取url

import {browser} from 'protractor'
browser.waitForAngular();    
var url = browser.getCurrentUrl()
console.log("URL is: "+url);

但是,当我在控制台中运行代码时,我有:

URL is: ManagedPromise::253 {[[PromiseStatus]]: "pending"}

此外,文档还指定函数的结果为 将用当前URL解析的承诺。

但是,我想知道为什么我不能以字符串的形式获取它?

3 个答案:

答案 0 :(得分:2)

browser.getCurrentUrl()返回一个承诺。

您必须解决获得当前URL的承诺。

var currentUrl = browser.getCurrentUrl().then(function(url){
console.log(url);
return url;
});

答案 1 :(得分:1)

正如您从documentation中看到的,getCurrentUrl()方法返回!webdriver.promise.Promise.<string>,将包含Promise的方法返回string转换为语言。因此,您首先需要解决Promise。我强烈建议您阅读此article以获取理解。 async ... await的示例:

async function pageUrl() {
   const url = await browser.getCurrentUrl();
   return url;
}

答案 2 :(得分:0)

import {browser} from 'protractor'
browser.getCurrentUrl().then((url) => {
    console.log("URL is: "+url);
})
相关问题