element.all().each()
вернуть обещание, конечное значение которого равно нулю.Таким образом, ваш 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);
});
Вы должны заметить, что все API-интерфейсы Protractor являются асинхронными и возвращать обещание.
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);
})
})
})
})