遍历数组 Capybara Cucumber

Iterate over array Capybara Cucumber

我正在努力寻找解决方案。

我正在访问网页上的 table。应用特定过滤器后,每一行中的一个数据项必须包含特定值。

我尝试用 table 数据创建一个数组,使每一行都有自己的索引。

有了索引后,我想在此行范围内查找我要查找的特定值。

到目前为止,我得到了以下信息:

results_table = all('table#clickable-rows tr')

    results_table.each do |row|


     within(results_table[row]) do
        table_data = all('table#clickable-rows td')
          expect(table_data[3]).to have_text TEXT
      end
    end
  end

这是我正在努力解决的迭代问题。有人对此有解决方案吗?

谢谢

这里有几处错误 - 一旦你开始迭代 results_table 行就是实际的行元素(不是行的索引),所以你不应该索引到 results_table再次。此外,一旦您调用了 within(element),所有 CSS 查找将与该元素相关,因此您不需要再次查找 table(除非寻找 table嵌入原文table)。你可能想要更像

的东西
results_table = all('table#clickable-rows tbody tr')
results_table.each do |row|
  within(row) do
    table_data = all('td') # you could also just find the third one with nth-child if you only want that one column
    expect(table_data[3]).to have_text TEXT
  end
end

结束

或不使用 within

results_table = all('table#clickable-rows tbody tr')
results_table.each do |row|
  table_data = row.all('td')
  expect(table_data[3]).to have_text TEXT
end

这里要注意的一件重要事情是默认情况下 all 不会等待行出现,所以如果这是 运行 使用支持 JS 的驱动程序,你可能想要使用一些东西喜欢

results_table = all('table#clickable-rows tbody tr', minimum: 1) #you can adjust minimum if you need to wait for more rows to be on the page

确保 table 行实际出现在页面上

在社区的帮助下,我找到了解决方案。

我的 results_tabletable_data 变量需要包含 tbody。一旦我添加了这个,它就能找到想要的内容。

results_table = all('table#clickable-rows tbody tr')
    results_table.each do |row|
      within(row) do
        table_data = all('table#clickable-rows tbody tr td') 
          expect(table_data[3]).to have_text TEXT
     end
   end
end