Protractor 量角器:如果列索引未知,如何在表中搜索?
我的目标是确定与确定的标题相对应的列中是否有一行包含文本 以下是视图:Protractor 量角器:如果列索引未知,如何在表中搜索?,protractor,Protractor,我的目标是确定与确定的标题相对应的列中是否有一行包含文本 以下是视图: <table> <headers> <header>Age</header> <header hidden="isSmallScreen()">Gender</header> <header>Size</header> <headers> <body> <r
<table>
<headers>
<header>Age</header>
<header hidden="isSmallScreen()">Gender</header>
<header>Size</header>
<headers>
<body>
<row>
<col>20</col>
<col hidden="isSmallScreen()">F</col>
<col>180</col>
</row>
<row>
<col>21</col>
<col hidden="isSmallScreen()">M</col>
<col>185</col>
</row>
</body>
<table>
方法searchRowsByTextInColumn(headerIndex,searchText)
已经实现,在该级别没有问题
问题是调用searchRowsByTextInColumn()
时仍未定义的headerIndex
。这是正常的,因为它位于异步代码中
现在我如何才能使搜索工作正常?当
searchRowsByTextInColumn(headerIndex,180)代码>执行时,headerIndex尚未完成赋值,因为元素.all().filter()
异步运行
function findHeaderIndex(header) {
return element.all(by.css('table > headers > header'))
.getText().then(function(headers){
return headers.indexOf(header);
});
}
function searchRowsByTextInColumn(header, cellText) {
return findHeaderIndex(header).then(function(index) {
var xpath = util.format('//table/body/row[ col[%s][text()="%s"] ]',
index, cellText);
return element.all(by.xpath(xpath));
});
}
searchRowsByTextInColumn('size', 180).then(function(rows){
expect(rows.count()).toBe(1);
// if above expect not work, try below
rows.count().then(function(rowCount){
console.log('rowCount: ' + rowCount);
expect(rowCount)).toBe(1);
});
});
您应该使用async/await或回调函数。将headerIndex
的代码移动到函数中,并在找到索引后返回索引?也许可以把它命名为findHeaderIndex()
?我用async/await
实现了它,非常感谢@tehbeardone。没问题。很高兴它成功了。在我看来,您的函数searchRowsByTextInColumn()
返回的是承诺,而不是ElementArrayFinder
。这意味着rows.count()
无法工作。我说得对吗?(很抱歉我迟了回答)仍然不起作用:错误TS2345:类型为“1”的参数不可分配给类型为“Expected”的参数。
似乎expect()
只接受封装在链接到ElementFinder
的类型中的承诺,而不是简单的JS承诺。
function findHeaderIndex(header) {
return element.all(by.css('table > headers > header'))
.getText().then(function(headers){
return headers.indexOf(header);
});
}
function searchRowsByTextInColumn(header, cellText) {
return findHeaderIndex(header).then(function(index) {
var xpath = util.format('//table/body/row[ col[%s][text()="%s"] ]',
index, cellText);
return element.all(by.xpath(xpath));
});
}
searchRowsByTextInColumn('size', 180).then(function(rows){
expect(rows.count()).toBe(1);
// if above expect not work, try below
rows.count().then(function(rowCount){
console.log('rowCount: ' + rowCount);
expect(rowCount)).toBe(1);
});
});