循环通过Capybara中传递的参数

时间:2015-01-21 21:11:59

标签: capybara parameter-passing variadic-functions

我想创建一个Capybara方法来读取表的内容,它采用可变数量的参数并遍历参数。

这是我的方法:

Then /^I should see a table record with "(.*?)", "(.*?)", "(.*?)"$/ do |invisible, name, address, phone|
  rows = page.all(".table-bordered tr")
  expect(rows.any? { |record| record.has_content? name }).to be_true
  rows.each do |record|
    if record.has_content? name
      expect(record.has_content? address).to be_true
      expect(record.has_content? phone).to be_true
    end
  end
end

我使用相同的CSS表结构来创建程序中其他位置有更多列的表。因此,无论表格是3列还是12列,我都希望能够使用相同的方法,因此我不会编写笨拙的代码。

如何分配可变数量的参数并循环遍历Capybara中的每个参数?

1 个答案:

答案 0 :(得分:0)

def assert_my_table(name, *row_data)
  # It will be much faster than looping through all rows
  row = page.find(:xpath, "//*[@class='table-bordered']//tr[./td='#{name}']")

  # retrive row contents only once (again, it will be faster than retrieving it again for each of the columns you want to assert)
  row_text = row.text

  row_data.each do |text|
    expect(row_text).to include(text)
  end
end
assert_my_table(name, address, phone)