0

I'm writing automated tests using Cucumber, Capybara, WebDriver, SitePrism, and Faker. I am new to this and need some help.

I have the following steps..

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

In config/env_config.rb I have set up an empty hash...

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

@verify = {}
end

Now I want to hash the value generated by Faker in the Given step so I can validate it saved properly in the When step. I also want to enter the value generated by faker in the script below into a search field.

@page.producttitle.set Faker::Name.title
  1. How do I push the values generated by faker to the @verify has?
  2. How do I pull that value and insert it into a text field?
  3. How do I pull that value to verify the save value equals the value generated by faker?
Mohammad Yaser Ahmadi
  • 4,664
  • 3
  • 17
  • 39
Ponchooo
  • 83
  • 1
  • 9

1 Answers1

1

1. How do I push the values generated by faker to the @verify has?

A hash is simply a dictionary of key-value pairs, which you can set with hash[key] = value.

The key can be a string @verify['new_product_name'] = Faker::Name.title

The key can also be a symbol @verify[:new_product_name] = Faker::Name.title

Since the value you generate may be used multiple times within the step definition (once for storing it in the @verify hash, and once for setting the field value) I personally prefer to first store it in a local variable, and reference that where needed.

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

2. How do I pull that value and insert it into a text field?

You can reference values by their key. So after you have stored the value in the hash, you could do this @page.producttitle.set @verify[:new_product_name]

Or if you stored it in a local variable as suggested above, you would just do this

@page.producttitle.set new_product_name

3. How do I pull that value to verify the save value equals the value generated by faker?

Similarly, you can assert that a field value equals what you've stored in the hash. An example would be @page.producttitle.value.should == @verify[:new_product_name]

Putting this all together:

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
Matt Metzger
  • 521
  • 3
  • 4