javascript 从函数内的量角器承诺中返回一个值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31925005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Returning a value from a from a protractor promise inside a function
提问by luker02
I'm trying to get text from a page and then use that text further down in the spec to assert on another element.
我试图从页面中获取文本,然后在规范中进一步使用该文本来断言另一个元素。
I've pasted a very simple spec you can run that shows you can't return a value from a function if the function's return statement is inside a protractor promise return txt;
(line 24)…
我已经粘贴了一个可以运行的非常简单的规范,它表明如果函数的 return 语句在量角器承诺内return txt;
(第 24 行),则不能从函数返回值……
describe('My Test', function () {
var tempVariable;
it('should go get some text from the page', function () {
browser.get('https://angularjs.org/');
tempVariable = getTextFromElement(); //it appears javascript immediately sets this variable before waiting for protractor to return the value
});
it('should do some random other stuff', function () {
element.all(by.cssContainingText('a', 'Learn')).get(0).click();
element.all(by.cssContainingText('a', 'Case Studies')).get(0).click();
element.all(by.cssContainingText('a', ' Home')).get(0).click();
});
it('should be able to use the text from the first page in this test', function () {
console.log('\ntempVariable: ' + tempVariable); //this is undefined!
expect(typeof tempVariable).not.toBe('undefined', 'failed: tempVariable was undefined!');
});
});
function getTextFromElement() {
$('a.learn-link').getText().then(function (txt) {
console.log('\nInitial text: ' + txt);
return txt; //how do we return this so it's available to other 'it' blocks?
});
}
Updated snippet of code following @alecxe answer and my comment.
在@alecxe 回答和我的评论之后更新了代码片段。
I am attempting to contruct an object from various text on a page and return it to assert on in a later page...
我正在尝试从页面上的各种文本构建一个对象,并将其返回以在稍后的页面中进行断言...
function getRandomProductFromList() {
var Product = function (line, name, subname, units) {
this.line = line;
this.name = name;
this.subname = subname;
this.units = units;
};
var myProduct = new Product();
myProduct.line = 'Ford';
myProduct.units = 235;
//select a random product on the page and add it to 'myProduct'
var allProducts = element.all('div.product');
allProducts.count().then(function (count) {
var randomIndex = Math.floor(Math.random() * count);
var productName = allProducts.get(randomIndex);
productName.getText().then(function (prodName) {
myProduct.name = prodName;
productName.click();
});
});
//If a sub-product can be chosen, select it and add it to 'myProduct'
var subproduct = $('div.subproduct');
subproduct.isDisplayed().then(function (subProductExists) {
if (subProductExists) {
subproduct.getText().then(function (subProductName) {
myProduct.subname = subProductName;
});
subproduct.click();
}
}, function (err) {});
return myProduct;
}
采纳答案by luker02
Thanks to @alecxe for starting me off on the right foot.
感谢@alecxe 让我从右脚开始。
I found a solution I am now using quite a bit after reading this.
By passing an object by reference, you can add properties on the fly and use them later in your spec.
通过引用传递对象,您可以动态添加属性并稍后在规范中使用它们。
Example:
例子:
describe('My Test', function () {
var tempObject = {};
it('should go get some text from the page', function () {
browser.get('https://angularjs.org/');
getTextFromElement(tempObject); //pass an object by reference to populate with page text
});
it('should do some random other stuff', function () {
$('div.someDiv').click();
});
it('should be able to use the text from the first page in this test', function () {
console.log(tempObject.textFromFirstPage); //works!
});
});
function getTextFromElement(tempObject) {
$('a.some-link').getText().then(function (txt) {
tempObject.textFromFirstPage = txt;
});
}
回答by alecxe
First of all, you are not returning anything from the function:
首先,您没有从函数返回任何内容:
function getTextFromElement() {
return $('a.learn-link').getText();
}
Now, this function would return you a promisewhich you need to resolve before using:
现在,这个函数会返回一个你需要在使用前解决的承诺:
it('should be able to use the text from the first page in this test', function () {
tempVariable.then(function (tempVariableValue) {
console.log('\ntempVariable: ' + tempVariableValue);
expect(typeof tempVariableValue).not.toBe('undefined', 'failed: tempVariable was undefined!');
});
});
Plus, to determine whether a variable is defined or not, I would use toBeDefined()
from jasmine-matchers
:
另外,要确定变量是否已定义,我将使用toBeDefined()
from jasmine-matchers
:
expect(tempVariableValue).toBeDefined();
回答by Carlos
None of the above worked for me. This is working for me:
以上都不适合我。这对我有用:
var item = element.all(by.xpath("some xpath here"));
this.methodToGetTheText = function () {
return Promise.resolve(item.getText().then(function (text) {
return text;
}));
}
回答by luker02
Are you calling methodToGetTheText().then(
from your spec? The value inside the .then()
function should contain your actual page text
你是methodToGetTheText().then(
从你的规格打电话吗?.then()
函数内的值应包含您的实际页面文本