follow me icons

Friday, March 18, 2011

SOAP request in Cucumber using Savon Ruby Gems

Here is how I use Savon for my project:

To install/configure Savon:
1. gem install savon
2. add "require 'savon'" in your env.rb files

soap_example.feature

Scenario: Example of sending a soap request using Savon Gems
Given the user creates a new wsdl client

When the user sends a "findGeocodedAddress" request
|street_name | street_type | street_number | street_suffix |
|Burrows Road| S | 20/2 | UnDefined |
Then the user should get a success response
And findGeocoded response should have "11" addresses


soap_example_steps.rb

Given /^the user creates a new wsdl client$/ do
@client = Savon::Client.new do
wsdl.document = "http://service.example.com?wsdl"
end
end

When /^the user sends a "([^\"]*)" request$/ do |request, table|
xml = find_geocoded_address_xml table.hashes[0]
@soap_response = @client.request :wsdl, request do |soap|
soap.body = xml.target!
end
end

#I am using Builder to generate my xml structure
def find_geocoded_address_xml address
xml = Builder::XmlMarkup.new(:indent => 2)

xml.credentials{
xml.token(TOKEN)
xml.password(AUTH_PASS)
}
xml.address{
xml.structuredAddress{
xml.street{
xml.streetName(address["street_name"])
xml.streetType(address["street_type"])
xml.streetSuffix(address["street_suffix"])
}
xml.streetNumber(address["street_number"])
xml.suburb(address["suburb"])
xml.state(address["state"])
xml.postcode(address["postcode"])
}
}
return xml
end

Then /^the user should get a success response$/ do
@soap_response.success?.should == true
@soap_response.soap_fault?.should == false
@soap_response.http_error?.should == false
end

Then /^findGeocoded response should have "([^\"]*)" addresses$/ do |num_address|
addresses = @soap_response.to_array[0][:find_geocoded_address_response][:addresses]
@num_address_result = num_address.to_i
if addresses.class == Array
addresses.count.should == @num_address_result
else
#returns as a hash that means only one result
@num_address_result.should == 1
end
end

2 comments:

  1. Thank you so mch....it helped me so easily..pls keep up the good work.

    ReplyDelete
  2. Do you have additional settings for Capybara running your server locally during testing on example.com (local DNS)? Trying to get past setting up a server before running tests if I can. Can't find the right introspection URL to pass to Savon.

    ReplyDelete