我试图获取html属性的值,并使用下面的代码将其分配给url_extension变量,但我不断获取Promise对象。这是我的代码:(注意:wd = browser.driver;
ReturnNewArticleURL : function() {
var url_extension = this.getArticleExtension();
console.log("url_extension: ", url_extension);
wd.get("http://any_website.com" + url_extension)
return url_extension;
},
getArticleExtension : function() {
return wd.findElement( by.id ("confirm-unpublish-url")).getAttribute("value").then(function (text) {
return text});
},
如果我在getArticleExtension模块中将“return text”更改为console.log(text),那么它将在我的控制台中为url扩展名打印正确的值。我的问题基本上是将文本值放在.then()函数之外并在其他地方使用它。任何人都知道如何做到这一点或我做错了什么?
答案 0 :(得分:1)
.getAttribute()
,因为其他Protractor / WebDriverJS方法返回promise。
我们的想法是让你的函数在需要实际值时返回一个promise并解决:
ReturnNewArticleURL : function() {
this.getArticleExtension().then(function (url_extension) {
console.log("url_extension: ", url_extension);
}
},
getArticleExtension : function() {
return wd.findElement(by.id("confirm-unpublish-url")).getAttribute("value");
},
答案 1 :(得分:1)
在重读并思考@ alecxe的回答后,我能够弄清楚我需要做什么。显然我只能在.then()函数中访问my属性的值。对于其他试图解决这个问题的人来说,以下是我的代码工作方式:
ReturnNewArticleURL : function() {
var url_extension = this.getArticleExtension();
console.log("url_extension: ", url_extension); //prints promise as expected
url_extension.then(function (text) {wd.get("http://any_website.com"+ text );
}), //this is how you use the url extension from the url_extension promise.
getArticleExtension : function() {
return wd.findElement( by.id ("confirm-unpublish-
url")).getAttribute("value")
},
密钥是使用.then()函数中的promise值,而不是将其赋值给外部的变量。
url_extension.then(function (text) {
wd.get("http://any_website.com"+ text );
}),
由于量角器使用node.js,它会异步读取代码,所以当我为一个在开头没有评估过的变量赋值时,会为它们分配一个promise。