如何在带有 Ruby 的 Cucumber 中使用 table 来验证 Web 表单上的数据

How to use a table in Cucumber with Ruby to verify data on web form

我将 Cucumber 与 Ruby 和 Watir webdriver 一起使用。

我想做的是验证预先填充在 Web 表单上的数据是否与 Cucumber 功能文件中的 table 中的数据相匹配。我需要帮助来编写步骤定义文件中的 Ruby 代码。这是我目前所拥有的:

Cucumber feature: 
Then I will be able to view my information pre-populated from IAM as follows:

|First Name/Given Name  |Chimwemwe       |
|Last Name/Surname      |Rossi           |
|Country                |USA             |
|Address                |fdafda          |
|City                   |fdafd           |
|State                  |Louisana        |
|Postal Code            |99999           |

Then (/^I will be able to view my information pre-populated from IAM.$/) do   |table|
    information = table.rows_hash
    information.each do |entry|
    contact_info = entry [0]
    if @browser.text_field(:name=>'firstName').verify_contains(contact_info[0])==true
        puts "Passed"
    else
        puts "Failed"
    end
end

我现在只做第一行,直到我开始工作。我希望它最终遍历 table。

当我尝试 运行 脚本时,我得到的只是这个错误:#table is a Cucumber::Core::Ast::DataTable.

我是 Ruby/Cucumber 的新手,这是我迄今为止编写的最复杂的脚本。任何有关如何执行此操作的帮助都会非常有帮助。我知道我需要一个阵列,但我在网上看了这么多,我觉得我的大脑要爆炸了。谢谢。

除非您有一种简单的方法将 Cucumber table 映射到 Watir 字段,否则迭代可能没有用。最简单的方法是直接检查每个被测字段是否匹配 table.

我不确定您使用的是哪个断言库,但作为示例,以下使用 RSpec 预期:

Then (/^I will be able to view my information pre-populated from IAM.$/) do |table|
  information = table.rows_hash

  expect(@browser.text_field(:name=>'firstName').value).to eq(information['First Name/Given Name'])
  expect(@browser.text_field(:name=>'lastName').value).to eq(information['Last Name/Surname'])
  # etc. for each field
end

请注意,测试将在第一个不正确的字段处失败。如果您想一次断言所有字段,您可以将它们检索到 Hash 并将其与 table:

进行比较
Then (/^I will be able to view my information pre-populated from IAM.$/) do |table|
  form_fields = {
    'First Name/Given Name' => @browser.text_field(:name=>'firstName').value,
    'Last Name/Surname' => @browser.text_field(:name=>'lastName').value
  }
  expect(form_fields).to eq(table.rows_hash)
end