【问题标题】:ElementFinder.filter function not returning a value when boolean result is true当布尔结果为真时,ElementFinder.filter 函数不返回值
【发布时间】: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


【解决方案1】:

添加一些代码来检查表中是否有th

.filter(async (elem, _) => {
    const thCount = await elem.$$('th').count();

    if(thCount > 0) {
      console.log(`there are ${thCount} ths`);

      const textValue = await elem.$$('th').first().getText();
      console.log(`${textValue} = ${tableName} => ${textValue === tableName}`)
      return textValue === tableName;   
    }
    else {
        console.log("there are 0 ths");
        return false;
    }
})  

或者您可以添加catch() 以查看filter() 中发生的任何故障

.filter(async (elem, _) => {
    const textValue = await elem.$$('th').first().getText();
    console.log(`${textValue} = ${tableName} => ${textValue === tableName}`)
    return textValue === tableName;
})            
.count()
.catch(function(err){
    console.log('error: ' + err)
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 2015-02-13
    • 1970-01-01
    • 2014-03-14
    • 1970-01-01
    • 2020-01-21
    • 1970-01-01
    相关资源
    最近更新 更多