Code Snippet: Using a config file for Ruby Automation 

Question:

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?

Answer:

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):
 {
  Environment:
   {
     "QA-1": "http://qa1.test.com/ ",
     "QA-2": "http://qa2.test.com/ ",
     "PROD": "http://www.test.com/ "
   },
 Account:
  {
     "username": "test_user",
     "password": "password"
  }
 }

Example (Code):

$env = ENV['LOC']<
file = File.read('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