我正在用量角器编写一些pageobject来模块化我的代码。我无法从函数返回正确的值。
以下功能为我提供了正确的输出。
this.functionName = function(){
var1 = '';
return this.locator.getText().then((locatorText)=>{
locatorNumber = locatorText;
return locatorNumber;
})
}
但是此功能不是
this.functionName = function(parameter){
return this.locator1.each((locator1Each, index)=>{
return locator1Each.getText().then((locator1EachText)=>{
if (locator1EachText.toLowerCase().includes(parameter.toLowerCase())) {
return this.locator1Count.get(index).getText().then((locator1CountText)=>{
locator1CountString = "Showing " + locator1CountText + " results"
console.log(locator1CountString);
return locator1CountText;
})
}
})
})
我无法从此函数获取返回值。
var x = common.functionName('mapreduce').then((functionNameTextV)=>{
console.log('functionNameText = ' + functionNameTextV);
})
任何帮助将不胜感激
也
it('description', function() {
x = 0;
y = 0;
z = 0;
z1 = 0;
locator.getText().then(function(var1){
x = var1;
})
locator1.getText().then(function(var2){
y = var2;
})
//this is what the sum of the above two variables will be compared against
locator2.getText().then(function(var3){
z1=var3;
})
z = x + y;
expect(z).toEqual(z1);
});
我无法执行此操作。值始终为0,这是初始值。反正要在一个内完成它吗?
答案 0 :(得分:0)
element.all().each()
返回一个promise,其最终值为null。因此,您的functionNameTextV
应该为空。
this.functionName= function(parameter){
return this.locator1.getText().then(function(txts) {
var index = txts.findIndex(function(txt){
return txt.toLowerCase().includes(parameter.toLowerCase());
});
if(index > -1) {
return locator1Count.get(index).getText().then((locator1CountText)=>{
var locator1CountString = "Showing " +
locator1CountText + " results";
console.log(locator1CountString);
return locator1CountText;
)};
}
return 'Not found such element: ' + parameter;
})
}
common.functionName('mapreduce').then((functionNameTextV)=>{
console.log('functionNameText = ' + functionNameTextV);
});
您需要注意所有Protractor API都是异步的并返回承诺。
it('description', function () {
x = 0;
y = 0;
z = 0;
z1 = 0;
locator.getText().then(function (var1) {
x = var1;
})
locator1.getText().then(function (var2) {
y = var2;
})
//this is what the sum of the above two variables will be compared against
locator2.getText().then(function (var3) {
z1 = var3;
})
z = x + y;
expect(z).toEqual(z1);
// The `getText()` internal implement is Async, `getText()`
// will return a promise as the execute result of `locator.getText()`
// then Nodejs start to execute next code line.
// the '.then(function callback())' is used to register a callback
// to the promise which returned by previous `getText()`.
// And the registered callbac be invoked automatically when
// the Async execution inside 'getText()'completed.
// Nodejs won't wait the callback to be invoked when it execute
// the code line `locator.getText().then(...)`.
// Thus when `z = x + y` be executed, the variable x, y are still
// with init value 0, the new assignment value inside then() have
// not happened yet, because the getText() internal async execution
// have not complete.
});
您可以使用Promise.all()
使代码看起来简洁。
it('description', function () {
x = 0;
y = 0;
z = 0;
z1 = 0;
Promise.all([
locator.getText(),
locator1.getText(),
locator2.getText()
])
.then(function (datas) {
x = datas[0];
y = datas[1];
z1 = datas[2];
z = x + y;
expect(z).toEqual(z1);
});
});
或使用嵌套的then()
it('description', function () {
x = 0;
y = 0;
z = 0;
z1 = 0;
locator.getText().then(function (var1) {
x = var1;
locator1.getText().then(function (var2) {
y = var2;
locator2.getText().then(function (var3) {
z1 = var3;
z = x + y;
expect(z).toEqual(z1);
})
})
})
})