使用 gettext() 函数并在其他文件中使用量角器时出现未定义错误
undefined error in protractor on using a gettext() function and using it in other file
我在 InputBoxActions.js 文件中编写了一个实用函数,用于从量角器的输入字段中获取文本,如下所示:
this.getTextFromInputBox = function (element) {
if (typeof element !== 'undefined') {
element.isDisplayed().then(function () {
element.isEnabled().then(function () {
element.getText().then(function(text){
return text;
})
});
});
}
};
但是,在测试规范文件中使用相同的函数时,我得到的文本值为未定义。在这里,我将函数调用为:
browserActions.goto(url);
var searchElement = findElements.byXpath("//input[@type='search']");
inputBoxActions.type(searchElement, 'angular');
var text = inputBoxActions.getTextFromInputBox(searchElement);
console.log(text);
我得到的结果是:
Started
undefined
(node:1412) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
.
1 spec, 0 failures
Finished in 6.082 seconds
您最里面的 return 语句只是 return 将值传递给调用它的函数,然后未使用。
-> element.getText().then(function(text){
return text;
});
如果你想以这种方式实现这一点,你需要return所有嵌套的 then 都是这样。
this.getTextFromInputBox = function (element) {
if (typeof element !== 'undefined') {
return element.isDisplayed().then(function () {
return element.isEnabled().then(function () {
return element.getText().then(function (text) {
return text;
})
});
});
}else{
throw new Error(`${element} is undefined`);
}
};
想象一下,一群人将一些工作交给彼此来完成。你最后的人正在做这项工作,但没有把它传回给需要它的老板。
我在 InputBoxActions.js 文件中编写了一个实用函数,用于从量角器的输入字段中获取文本,如下所示:
this.getTextFromInputBox = function (element) {
if (typeof element !== 'undefined') {
element.isDisplayed().then(function () {
element.isEnabled().then(function () {
element.getText().then(function(text){
return text;
})
});
});
}
};
但是,在测试规范文件中使用相同的函数时,我得到的文本值为未定义。在这里,我将函数调用为:
browserActions.goto(url);
var searchElement = findElements.byXpath("//input[@type='search']");
inputBoxActions.type(searchElement, 'angular');
var text = inputBoxActions.getTextFromInputBox(searchElement);
console.log(text);
我得到的结果是:
Started
undefined
(node:1412) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.
.
1 spec, 0 failures
Finished in 6.082 seconds
您最里面的 return 语句只是 return 将值传递给调用它的函数,然后未使用。
-> element.getText().then(function(text){
return text;
});
如果你想以这种方式实现这一点,你需要return所有嵌套的 then 都是这样。
this.getTextFromInputBox = function (element) {
if (typeof element !== 'undefined') {
return element.isDisplayed().then(function () {
return element.isEnabled().then(function () {
return element.getText().then(function (text) {
return text;
})
});
});
}else{
throw new Error(`${element} is undefined`);
}
};
想象一下,一群人将一些工作交给彼此来完成。你最后的人正在做这项工作,但没有把它传回给需要它的老板。