0

我正在使用 Cucumber、Capybara、WebDriver、SitePrism 和 Faker 编写自动化测试。我是新手,需要一些帮助。

我有以下步骤..

Given (/^I have created a new active product$/) do
@page = AdminProductCreationPage.new
@page.should be_displayed
@page.producttitle.set Faker::Name.title
@page.product_sku.set Faker::Number.number(8)
click @page.product_save
@page.should have_growl text: 'Save Successful.'
end

When (/^I navigate to that product detail page) do
 pending
end

Then (/^All the data should match that which was initially entered$/) do
 pending
end

在 config/env_config.rb 我设置了一个空哈希...

Before do
# Empty container for easily sharing data between step definitions

@verify = {}
end

现在我想对 Faker 在Given步骤中生成的值进行哈希处理,以便验证它在When步骤中是否正确保存。我还想将faker在下面脚本中生成的值输入到搜索字段中。

@page.producttitle.set Faker::Name.title
  1. 如何将 faker 生成的值推送到 @verify ?
  2. 如何提取该值并将其插入文本字段?
  3. 如何提取该值以验证保存值等于 faker 生成的值?
4

1 回答 1

1

1.如何将faker生成的值推送到@verify有?

哈希只是键值对的字典,您可以使用hash[key] = value.

键可以是字符串 @verify['new_product_name'] = Faker::Name.title

钥匙也可以是符号@verify[:new_product_name] = Faker::Name.title

由于您生成的值可能会在步骤定义中多次使用(一次用于将其存储在 @verify 哈希中,一次用于设置字段值)我个人更喜欢首先将其存储在局部变量中,并在需要时引用它.

new_product_title = Faker::Name.title
@verify[:new_product_title] = new_product_title

2. 如何提取该值并将其插入文本字段?

您可以通过键来引用值。因此,在您将值存储在哈希中之后,您可以执行此操作 @page.producttitle.set @verify[:new_product_name]

或者,如果您按照上面的建议将其存储在局部变量中,您只需执行此操作

@page.producttitle.set new_product_name

3.如何拉取该值以验证保存值是否等于faker生成的值?

同样,您可以断言字段值等于您存储在哈希中的值。一个例子是@page.producttitle.value.should == @verify[:new_product_name]

把这一切放在一起:

Given (/^I have created a new active product$/) do
  @page = AdminProductCreationPage.new
  @page.should be_displayed

  # Create a new title
  new_product_title = Faker::Name.title

  # Store the title in the hash for verification
  @verify[:new_product_title] = new_product_title

  # Set the input value to our new title
  @page.producttitle.set new_product_title

  @page.product_sku.set Faker::Number.number(8)
  click @page.product_save
  @page.should have_growl text: 'Save Successful.'
end

When (/^I navigate to that product detail page) do
   pending
end

Then (/^All the data should match that which was initially entered$/) do
  @page.producttitle.value.should == @verify[:new_product_title]
end
于 2014-10-09T16:23:13.427 回答