follow me icons

Tuesday, October 12, 2010

How to setup Cucumber YAML Configuration File

Application configurations should be placed in one location and we can do this by using an external YAML file called config.yaml. On my blog on Setting up a cucumber project, I have not covered on how to create a config.yml file. This file is where you normally put your BASE_URL address and other configuration files for you to use in your projects.

For examples, you might want to run your test against a test, staging or production environments and for that you need to tell your code which url you want to test. So, here is how you write a config.yml file and how to use it.

I assume that you have the same project structure mentioned in here

1. Create a new config.yml file under features\support\config.yml

google_australia:
base_url: http://www.google.com.au

google_singpaore:
base_url: http://www.google.com.sg

2. Create a new "features\support\lib\configuration.rb" files to load the config.yml

require 'yaml'

class Configuration
def self.[] key
@@config[key]
end

def self.load name
@@config = nil
io = File.open( File.dirname(__FILE__) + "/../config.yml" )
YAML::load_documents(io) { |doc| @@config = doc[name] }
raise "Could not locate a configuration named \"#{name}\"" unless @@config
end

def self.[]= key, value
@@config[key] = value
end

end

raise "Please set the TEST_ENV environment variable" unless ENV['TEST_ENV']
Configuration.load(ENV['TEST_ENV'])

3. Add the following lines in your "features\support\env.rb" file

require File.dirname(__FILE__) + '/../lib/configuration';
BASE_URL = Configuration["base_url"]

4. Now modify "features\step_definitions\search_steps.rb"

Change

Given /^I am on the main google search$/ do
visit "http://www.google.com"
end

to


Given /^I am on the main google search$/ do
visit "#{BASE_URL}"
end

5. Now before you run the project you just need to set the TEST_ENV

set test_env=google_australia

This will run your test againts www.google.com.au. If you set the test_env=google_singapore then
your test will run againts www.google.com.sg

3 comments:

  1. Did exactly as above but got the following error:
    "undefined local variable or method '_FILE_' for main:Object (NameError).
    I am new to cucumber so please any help would be hugely appreciated.

    ReplyDelete
  2. You can run again with --guess to make Cucumber be more smart about it
    (Cucumber::Ambiguous)
    C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/cucumber-2.1.0/lib/cucumber/runtime/support_code.rb:199:in `step_match_without_cache'
    C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/cucumber-2.1.0/lib/cucumber/runtime/support_code.rb:190:in `step_match'
    C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/cucumber-2.1.0/lib/cucumber/runtime/support_code.rb:140:in `find_match'
    C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/cucumber-2.1.0/lib/cucumber/filters/activate_steps.rb:28:in `attempt_to_activate'
    C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/cucumber-2.1.0/lib/cucumber/filters/activate_steps.rb:24:in `map'
    C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/cucumber-2.1.0/lib/cucumber/filters/activate_steps.rb:24:in `new_test_steps'
    C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/cucumber-2.1.0/lib/cucumber/filters/activate_steps.rb:18:in `test_case'
    C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/cucumber-2.1.0/lib/cucumber/filters/activate_steps.rb:8:in `test_case'
    C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/cucumber-core-1.3.0/lib/cucumber/core/test/case.rb:22:in `describe_to'
    C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/cucumber-2.1.0/lib/cucumber/filters/quit.rb:11:in `test_case'
    C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/cucumber-core-1.3.0/lib/cucumber/core/test/case.rb:22:in `describe_to'
    C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/cucumber-core-1.3.0/lib/cucumber/core/test/filters/locations_filter.rb:17:in `block in done'
    C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/cucumber-core-1.3.0/lib/cucumber/core/test/filters/locations_filter.rb:16:in `each'
    C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/cucumber-core-1.3.0/lib/cucumber/core/test/filters/locations_filter.rb:16:in `done'
    C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/cucumber-core-1.3.0/lib/cucumber/core/filter.rb:61:in `done'
    C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/cucumber-core-1.3.0/lib/cucumber/core/test/filters/tag_filter.rb:18:in `done'
    C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/cucumber-core-1.3.0/lib/cucumber/core/compiler.rb:23:in `done'
    C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/cucumber-core-1.3.0/lib/cucumber/core/gherkin/parser.rb:39:in `done'
    C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/cucumber-core-1.3.0/lib/cucumber/core.rb:29:in `parse'
    C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/cucumber-core-1.3.0/lib/cucumber/core.rb:18:in `compile'
    C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/cucumber-2.1.0/lib/cucumber/runtime.rb:70:in `run!'
    C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/cucumber-2.1.0/lib/cucumber/cli/main.rb:32:in `execute!'
    C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/cucumber-2.1.0/bin/cucumber:8:in `'
    C:/Ruby22-x64/bin/cucumber:23:in `load'
    C:/Ruby22-x64/bin/cucumber:23:in `'


    I am getting those errors what does that mean???

    ReplyDelete
  3. it means that you have two or more steps definitions that are the same. You can try checking your steps definition again to make sure they are unique.

    ReplyDelete