【发布时间】:2019-07-06 20:07:07
【问题描述】:
我正在尝试使用量角器运行测试以计算表中的行数。我正在测试的页面有 5 个已加载的表,这些表没有 Id 参数,但它们在各自的表标题行中确实有不同的名称。因此,我提取所有表格元素,然后使用检查第一个标记行内的文本的函数进行过滤。
要提取行数,我使用的代码类似于:
// Step definition - used in cucumber.
Given(/^the (\w+) table should have (\d+) rows$/,
async (tableName, expectedRowCount) => {
const parsedRowCount = Number.parseInt(expectedRowCount);
const actualRowCount =
await element
.$$('table')
.filter(async (elem, _) => {
const textValue = await elem.$$('th').first().getText();
console.log(`${textValue} = ${tableName} => ${textValue === tableName}`)
return textValue === tableName;
})
.first()
.$$('tr')
.count();
assert.strictEqual(actualRowCount, parsedRowCount);
});
当它运行时,console.log 为我要打印的表打印“Account = Account => true”,并为其他所有内容打印错误的语句。
如果我尝试调试并找出有多少元素通过过滤器函数:
// Step definition - used in cucumber.
Given(/^the (\w+) table should have (\d+) rows$/,
async (tableName, expectedRowCount) => {
const parsedRowCount = Number.parseInt(expectedRowCount);
const actualRowCount =
await element
.$$("table"))
.filter(async (elem, _) => {
const textValue = await elem.$$('th').first().getText();
console.log(`${textValue} = ${tableName} => ${textValue === tableName}`)
return textValue === tableName;
})
.count();
assert.strictEqual(actualRowCount, parsedRowCount);
});
我发现实际上没有元素通过过滤器功能。我不明白为什么没有元素被传递,当 console.log 清楚地显示我感兴趣的表的返回值将返回 true 时。如果不是传入一个元素参数,而是传入一个索引参数(过滤函数的第二个参数)并带有一些条件,例如index === 1,那么正确的表就会通过并且答案是正确的。
谁能解释为什么我的过滤功能不起作用?
【问题讨论】:
-
请确认该页面上的所有
table至少有一个th,否则elem.$$('th').first()应该抛出异常 -
我在stackoverflow上的第一个问题非常相似stackoverflow.com/questions/48016927/…从那时起我意识到整个方法是错误的,即使我能够解决它,因为它可能在很多地方出错。如果您附上您的 html 文档并在评论中 ping 我,我会向您展示一些更好的方法来解决您的问题
标签: javascript node.js protractor cucumber