Archive for the ‘Software testing’ Category

Work effectively with mocked Web Services using SoapUI

Monday, July 18th, 2011

Today at work we get interesting case. How to test web-service(WS) client when only WSDL description is available,  WS was still in development?

WSDL document is rather self descriptive so it is not a tragedy. At least we know what can be expected as response after specified request.

With help of SoapUI live become easier.

Lets try to create simulation of web-service when only WSDL file is available.

1. Create new project (I use SoapUI v4.0.0):

- for test purpose I choose Allegro Web API – the biggest Polish online auction system

Probably you also noticed option “Create a Web Service Simulation …” – do not forget checked it.

2. Next form lets us generate mocks for chosen services:

I chose doLogin method to avoid passing 25 different parameters.

3. Click ok, provide name of your new created mockService, again click ok

4. If everything went OK your screen should look like image bellow:

 

5. Double click on “do Login” service to open MockResponses List.

6. Right click on ‘Response 1′ and Show MockResponse Editor

In editor we can create response text as we want. For example.

<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:urn="urn:AllegroWebApi">
   <soapenv:Header/>
   <soapenv:Body>
      <urn:doLoginResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <session-handle-part xsi:type="xsd:string">abcdef098765</session-handle-part>
         <user-id xsi:type="xsd:long">345</user-id>
         <server-time xsi:type="xsd:long">12345678</server-time>
      </urn:doLoginResponse>
   </soapenv:Body>
</soapenv:Envelope>

7. Run server 

8. Right click on the ‘Response 1′ Mock and choose ‘Open request’ option.

Then by clicking on green arrow you can test your MockServer. In response window you should receive the same xml which you provide in 6th step.

Lets try this outside SoapUI and write any Python code (Suds module available here):

from suds.client import Client

WSDLURL = 'http://szymon:8088/mockAllegroWebApiBinding?WSDL'

if __name__ == '__main__':
    client = Client(WSDLURL)

    params = {'user-login' : 'testUser',
              'user-password' : 'pass',
              'country-code' : 12,
              'webapi-key': 'xxxxYYYYY',
              'local-version' : 32546}
    response = client.service.doLogin(**params)
    print response

Response

(reply){
   session-handle-part = "abcdef098765"
   user-id = 345
   server-time = 12345678
 }

Do not forget change value for WSDLURL.

It is very easy way to test not exist yet webservice. Manualy defined reponse are usefull for modes:

  • SEQUENCE
  • RANDOM
  • QUERY_MATCH
  • XPATH

more information abut them are available on soapUI project page.

What in case that we need more logic to prepare resposne and it should be depended from request parameter? It is also not a problem. Everything what we need is set response mode as SCRIPT and write logic using groovy.

For example lets return user-id as doubled country code.

def util = new com.eviware.soapui.support.GroovyUtils(context)
def xml = util.getXmlHolder(mockRequest.requestContent)
context.userId = xml.getNodeValue("//country-code").toInteger() * 2

then in response editor change

         <user-id xsi:type="xsd:long">345</user-id>

into      

         <user-id xsi:type="xsd:long">${userId}</user-id>

now the returned response (for country-code = 12) looks like:

(reply){
   session-handle-part = "abcdef098765"
   user-id = 24
   server-time = 12345678
 }

It works! ;)
Here is article about more advanced creation of response in groovy and currently available API.