我试图组合两个字符串“globalVariables.EditButton_”和“i”的值,并将该字符串(例如“globalVariables.EditButton_1”)作为变量传递,该变量应该引用我们存储为全局变量的按钮元素。下面是我的代码,但它不起作用。错误是
"Message: Failed: Button.isDisplayed is not a function Stack: TypeError: Button.isDisplayed is not a function "
代码片段:
it('should show the same data in both Page' , function(){
var RowCount=
globalVariables.tableData_Dashboard.all(by.tagName("tr")).count() -1;
for (var i=1; i < RowCount; ++i){
var Button = 'globalVariables.Edit_Button_'+i;
expect(Button.isDisplayed());
// do something
}
});
在这种情况下,如何添加两个字符串并将其用作变量?
最佳答案
在上面的代码片段中,您的 globalVariables.tableData_Dashboard.all(by.tagName("tr")).count()
返回 promise 。因此,当您将它们加在一起时,您就为一个 int 和一个 int 添加了一个 Promise。接下来看起来很奇怪: var Button = 'globalVariables.Edit_Button_'+i;
这不会被评估为 elementFinder。我同意上面 yong 的评论,您应该使用 globalVariables['Edit_Button_'+i];
使用异步/等待
所以我将用 async/await 并关闭控制流来回答这个问题。这是在配置文件中设置的 SELENIUM_PROMISE_MANAGER: false,
。
it('should show the same data in both Page' , async () => {
const RowCount=
(await globalVariables.tableData_Dashboard.all(by.tagName("tr")).count()) - 1;
for (var i=1; i < RowCount; ++i){
const button = globalVariables['Edit_Button_'+i];
expect(await button.isDisplayed());
// do something
}
});
关于javascript - 组合文本并将其用作 Protractor 中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54226171/