0

im just new with ruby cucumber and i want to have a data repository like a csv file, where i can add all my data so it will be easy to maintain and its not hardcoded in my feature files

can anyone point me to the right direction how to make this possible

most of the scenario i see are like this

  Scenario: go to google
    When I visit at "google.com"
    Then I click the link "image"

I want to make it like this

Scenario: go to any website
    When I visit at "website_a"
    Then I click the link "image"

and in my CSV file it can look for the id "website_a" and select the data "google.com"

B_B
  • 95
  • 1
  • 9
  • 1
    Check out [Scenario Outlines](https://github.com/cucumber/cucumber/wiki/Scenario-Outlines), they might be a decent compromise. Kind of made for what you're describing. Otherwise, [this answer](http://stackoverflow.com/questions/20955361/importing-csv-as-test-data-in-cucumber) may help give you a clue what to try. – Nick Veys Aug 01 '14 at 14:49

2 Answers2

1

Don't use CSV for this. CSV is OK when transferring data but it's lacking when being used for configuration data.

Instead use YAML. It's easily read, and great for configuration files. Ruby comes with a YAML parser as part of the Standard Library.

Here's how to get started:

require 'yaml'

data = {
  'string' => 'foo',
  'array_of_ints' => [1,2,3],
  'int' => 0,
  'boolean' => true
}

puts data.to_yaml
# >> ---
# >> string: foo
# >> array_of_ints:
# >> - 1
# >> - 2
# >> - 3
# >> int: 0
# >> boolean: true

You can see it's easy to read, and, because it's text, it's easy to modify the YAML file using an editor. Reloading the data is just as easy. If I modify the above code to write to a file I can reload the data and receive a Ruby object containing the data:

require 'yaml'

data = {
  'string' => 'foo',
  'array_of_ints' => [1,2,3],
  'int' => 0,
  'boolean' => true
}

File.write('data.yaml', data.to_yaml)

loaded_data = YAML.load_file('data.yaml')
loaded_data # => {"string"=>"foo", "array_of_ints"=>[1, 2, 3], "int"=>0, "boolean"=>true}

loaded_data['string'] # => "foo"
loaded_data['array_of_ints'] # => [1, 2, 3]
loaded_data['int'] # => 0
loaded_data['boolean'] # => true

You can even imitate a CSV file, though I'm not sure why you'd want to since you have to access records sequentially or by index instead of by being able to directly access them from a hash:

loaded_data = YAML.load(<<EOT)
---
- [Name,         HR, Avg. ]
- [Mark McGwire, 65, 0.278]
- [Sammy Sosa,   63, 0.288]
EOT
loaded_data # => [["Name", "HR", "Avg."], ["Mark McGwire", 65, 0.278], ["Sammy Sosa", 63, 0.288]]
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
0

There are actually a number of different ways you could handle something like this. The way I do this for urls is 'Step Argument Transforms'

https://github.com/cucumber/cucumber/wiki/Step-Argument-Transforms

url_transform.rb (put this in your 'support' folder)

Transform /^(~internet~)$/ do |trans| "http://the-internet.herokuapp.com" end

then in my feature I just use:

Given I go to the "~internet~" page

then in my steps I use:

Given /^I go to the "(.*)" page$/ do |url|
@browser.goto url
puts browser.url

Terminal output:

http://the-internet.herokuapp.com/

(you don't need the "~" characters I just use them to remind me that they are 'Transforms')

This is by no means the best solution but if you're a beginner this is probably a useful trick for urls at least.

Carldmitch
  • 409
  • 2
  • 5