【问题标题】:Using multiple data sets in populate_page_with in page-object gem在 page-object gem 中的 populate_page_with 中使用多个数据集
【发布时间】:2014-01-21 22:57:30
【问题描述】:

我有一个表单,我使用 page-object gem 填充了 populate_page_with data_for。它是这样定义的:

def add_fruit(data = {})
  populate_page_with data_for(:new_fruit, data)
  add_fruit_submit
end

然后我以这种方式调用我的步骤中的方法:

on(AddFruitPage).add_fruit

我的 yml 文件如下所示:

new_fruit:
  color: red
  size: large
  type: apple
  price: 0.75
  ...
another_fruit
  color: orange
  size: medium
  type: orange
  price: 0.99
  ...

我知道我可以通过在我的步骤中执行以下操作来覆盖每个字段:

When(^/I add a banana$/) do
  on(AddFruitPage).add_fruit('color' => 'yellow', 'size' => 'small', 'type' => 'banana')
end

由于另一个水果的数据已经在 yml 文件中,我是否可以使用参数告诉方法要加载哪些数据,而不必在使用方法时指定每个值?比如:

def add_fruit(data = {})
  if(data['type'] == 'another')
    populate_page_with data_for(:new_fruit, data)
  else
    populate_page_with data_for(:another_fruit, data)
  end
end

然后这样称呼它?

on(AddFruitPage).add_fruit('type' => 'another')

Type 是一个可选参数,仅用于加载另一组数据。颜色、大小、类型和价格都映射到在类中定义的页面上的文本字段。有可能做这样的事情吗?

【问题讨论】:

    标签: ruby cucumber page-object-gem


    【解决方案1】:

    如果您使用的是 Ruby 2,则可以使用命名参数 - 为水果类型创建一个命名参数,并将其余部分作为数据数组的一部分。

    我可能不会使用数据中已经存在的“类型”,而是使用指定data_for 的第一个参数的参数。方法定义很简单:

    def add_fruit(fruit_type: :new_fruit, **data)
      populate_page_with data_for(fruit_type, data)
      add_fruit_submit
    end
    

    可以通过多种方式调用:

    add_fruit()  # Specify nothing
    add_fruit(:color => 'red')  # Just specify the data
    add_fruit(:fruit_type => :another_fruit)  # Just specify the fruit type
    add_fruit(:fruit_type => :another_fruit, :color => 'red')  # Specify fruit type and data
    

    如果您使用的是 Ruby 2 之前的版本,您可以这样做:

    def add_fruit(data = {})
      fruit_type = data.delete(:fruit_type) || :new_fruit
      populate_page_with data_for(fruit_type, data)
      add_fruit_submit
    end
    

    【讨论】:

    • 我使用的是 ruby​​ 1.9.3。我会用 add_fruit(:fruit_type => :another_fruit) 调用它吗?
    • 方法的两个版本做同样的事情。所以是的,你可以这样调用方法(加上其他例子)。
    • 谢谢贾斯汀。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-31
    • 2020-02-05
    • 2017-05-05
    • 1970-01-01
    相关资源
    最近更新 更多