Archive
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
Ruby: Using URI and HTTPClient
HTTPClient provides API library for user to access web resource using HTTP
This would be helpful if you need to access a different web URL other than your base URL that you used in your automation and validate the content of the page.
Here’s a snippet of how to use HTTPClient in this particular scenario
require “httpclient” my_url = “http://CheckThisOut:8080” ##method to access my_url using URI def accessPage begin uri_parsed = URI.parse(my_url) client = HTTPClient.new() client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE client.no_proxy = uri_parsed.host uri = my_url #invoke url to take the page content puts “Accessing: #{uri}” resp = client.get uri, :header => {’Accept’ =>‘application/json’} rescue puts(“Error encountered: #{$!.inspect}”) end end puts "response: status = #{resp.status}" puts "response: body = #{resp.body}" fail "bad response #{resp.status} #{resp.reason}" unless resp.status == HTTP::Status::OK page_content = resp.body return page_content end
For more functions and complete library you may refer to http://www.rubydoc.info/gems/httpclient/2.1.5.2/HTTPClient
Recent Comments