Home > ruby > Cucumber: Using JSON Parser

Cucumber: Using JSON Parser

JavaScript Object Notation(JSON) is a lightweight data-interchange format useful to generate and parse data

Sample Data:

{‘user’:{‘FirstName’:’User1′, ‘LastName’:’Sample’, ‘Gender’:’Female’, ‘BirthDate’:’01/01/1990′}}

Suppose we are to use the above user data, parse it into an hash and use the array hash as input to a sign up screen.

Here’s a snippet of how to use JSON in this particular scenario


require “json”

my_data = "{'user':{'FirstName':'User', 'LastName':'Sample', 'Gender':'Female', 'BirthDate':'01/01/1990'}}"

##method to parse user data

def getNewUserData

@new_user = JSON.parse my_data

{

:firstName => new_user['user']['FirstName'],

:lastName => new_user['user']['LastName'],

:gender => new_user['user']['Gender'],

:birthDate => new_user['user']['BirthDate']

}

end

##method to populate the sign up screen

def fillOutSignUpScreen

step %Q[I enter "#{@new_user[:firstName]}" to the "signup.firstname"]

step %Q[I enter "#{@new_user[:lastName]}" to the "signup.lastname"]

step %Q[I enter "#{@new_user[:gender]}" to the "signup.gender"]

step %Q[I enter "#{@new_user[:birthDate]}" to the "signup.birthdate"]

end

For more functions and complete library you may refer to http://www.ruby-doc.org/stdlib-2.0/libdoc/json/rdoc/JSON.html

Advertisements
Categories: ruby Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

%d bloggers like this: