Code Snippet: Using a config file for Ruby Automation 


I have two test environments plus production and I need to run automation against them. What is the easiest way to configure your test automation to run against different site?


Create a configuration file for storing information such as environmental information and user information. This way you only have one place to update, rather than multiple. I like to use JSON files for this purpose.

Example (JSON):
     "QA-1": " ",
     "QA-2": " ",
     "PROD": " "
     "username": "test_user",
     "password": "password"

Example (Code):

$env = ENV['LOC']<
file ='config.json')
$data_hash = JSON.parse(file)
testEnv = $data_hash['Environments'][$env]
ENV['base_url'] = testEnv
ENV['user_email'] = $data_hash['Accounts']['username']
ENV['user_password'] = $data_hash['Accounts']['password']

Note that ENV[] is a way of making a global variable that will remain constant throughout your execution of tests.

One Reply to “Code Snippet: Using a config file for Ruby Automation ”

Leave a Reply