How to tokenize cucumber for calabash

While writing regression tests for one of iOS applications, we faced a small problem. We have two environments development and test. Sometimes we need to execute tests on dev server and sometimes on test. For each of this environments we have different accounts and no way to create identical accounts on both environments.

This problem could be solved by creating two test sets, with different value that would match environment it needs to be executed at. But this brings to much trouble in maintaining two suites of test and make sure they are accurate all the time.

Another problem is uniq ids, if you have some items that are validate just for one session and have to uniq for each execution. This brings another problem for you when you trying to change a value in 10 feature files before each execution. So we decided to add tokens(parameters) our cucumber files. This way we could control all values that will be used in a tests and change then in one place instead of going around and search for desired values.

Here is our solution :

  • Create yaml file with tokens. Something like this :
    Username: xxx
    Password: xxxxx
    FirstName: xxxxx
    LastName: xxxxx
    ContactNumber1: 1234567890
  • Add this file into some separate directory, for example feature/config, just to keep everything in one place
  • Add a method to read config file in your custom step definition file. Something like this :
    require 'yaml'
    def read_token_file
      tokens = nil
        tokens = YAML.load_file('features/config/tokens.yml')
      rescue Exception => e
        puts "Token file is not found. Will continue in standard mode. If you want to use tokens create feature/config/tokens.yml file"
  • Add a method that will replace tokens with it’s value when needed, for example something like this :
    def set_value token
      tokens = read_token_file
      result = token
      if !tokens.nil? && !tokens[token].nil?
        result = tokens[token]

So now if I need to enter a name into a field I would create a customer step that is just a wrapper for out of box step and our new set_value method. Something like this :

Then /^I use keyboard to enter "([^\"]*)" into the "([^\"]*)" (?:text|input) field$/ do |text_to_type, field_name|
  text_to_type = set_value text_to_type
  macro %(I use the native keyboard to enter "#{text_to_type}" into the "#{field_name}" text field)

And write a step like this :
Then I use keyboard to enter "username" into the "username" text field .

Thank you for attention
and hope it was useful
for someone out there!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s