[英]Returning a value from a from a protractor promise inside a function
我正试图从页面获取文本,然后在规范中进一步使用该文本来断言另一个元素。
我已经粘贴了一个非常简单的规范,你可以运行,如果函数的return语句在一个量角器promise promise return txt;
你就不能从函数返回一个值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?
});
}
在@alecxe回答和我的评论之后更新了代码片段。
我试图从页面上的各种文本构造一个对象,并在稍后的页面中将其返回到断言...
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;
}
首先,你没有从函数返回任何东西 :
function getTextFromElement() {
return $('a.learn-link').getText();
}
现在,此函数将返回您在使用之前需要解决的承诺 :
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!');
});
});
另外,为了确定是否定义了变量,我将使用来自jasmine-matchers
toBeDefined()
:
expect(tempVariableValue).toBeDefined();
感谢@alecxe让我从右脚开始。
我发现我现在用相当多的解决方案看完这个 。
通过引用传递对象,您可以动态添加属性并在以后的规范中使用它们。
例:
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;
});
}
以上都不适合我。 这对我有用:
var item = element.all(by.xpath("some xpath here"));
this.methodToGetTheText = function () {
return Promise.resolve(item.getText().then(function (text) {
return text;
}));
}
你打电话methodToGetTheText().then(
从你的规范呢?值内.then()
函数应该包含您的实际网页的文本
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.