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
here1. 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