使用 cucumber/capybara 在散列中存储和提取数据

Storing and extracting data in hashes with cucumber/capybara

我在 cucumber/capybara/site_prism 上用不同的登录凭据进行了很多测试,这些测试非常混乱。我想尽可能地统一它们;这个解决方案似乎不错 https://blog.jayway.com/2012/04/03/cucumber-data-driven-testing-tips/

但是在按照示例进行操作时,我 运行 将其作为步骤定义的第一行

Your block takes 1 argument, but the Regexp matched 2 arguments.

显然,我误解了应该如何处理散列;有人可以帮忙吗?我的测试数据较少的代码如下 黄瓜

Given I login as "ad" with the following data:
    |role|usern       |userpass     |
    |ad  |adcccount   |adpassword   |
    |ml  |mlaccount   |mlpassword   |

步骤定义

Given /^I login as "(ad|ml)" with the following data:/ do |user|
 temp_hash = {}
    if (user == "ad")
      temp_hash = $ad
    elsif (user == "ml")
      temp_hash = $ml
    end

    usern = temp_hash["usern"]
    userpass = temp_hash["userpass"]

 @app = App.new
  @app.login.load
  @app.login.username.set usern
  @app.login.password.set userpass
  @app.login.btn_login.click
end

您收到该错误是因为匹配的第二个参数是 data_table。您的步骤定义需要

Given /^I login as "(ad|ml)" with the following data:/ do |user, data_table|
  ...

如果您查看所链接文章中的 Given /I create new user named "(user_1|user_2|user_3)" with the following data:/ do |user, data_table| 步骤,您会看到相同的内容,尽管 data_table 中没有使用多个条目,所以我不是 100%确定你在你的例子中试图做什么。

谢谢,托马斯;您的提示导致以下代码按预期工作

Given /^I login as "(ad|ml)" with the following data:/ do |login_role, data|

  temp_hash = {}
  data.hashes.each do |hash|
    if hash[:role] == login_role
      temp_hash[:role] = hash[:role]
      temp_hash[:usern] = hash[:usern]
      temp_hash[:userpass] = hash[:userpass]
    end
  end

    usern = temp_hash[:usern]
    userpass = temp_hash[:userpass]

  @app = App.new
  @app.login.load
  @app.login.username.set usern
  @app.login.password.set userpass
  @app.login.btn_login.click
  expect(@app.dashboard).to be_displayed
end