【问题标题】:Putting Faker Gem Values to a Hash将 Faker Gem 值放入哈希中
【发布时间】:2021-02-14 22:00:45
【问题描述】:

我正在使用 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 has?
  2. 如何提取该值并将其插入到文本字段中?
  3. 如何提取该值以验证保存值是否等于 faker 生成的值?

【问题讨论】:

    标签: ruby hash cucumber capybara faker


    【解决方案1】:

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

    hash 只是一个键值对字典,您可以使用hash[key] = value 对其进行设置。

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

    key也可以是符号@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
    

    【讨论】:

      猜你喜欢
      • 2020-02-20
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-14
      • 2012-11-30
      相关资源
      最近更新 更多