What is REST? Why is it Needed
REST (Representational State of Transfer) is a lightweight option for developing web services the uses the HTTP protocol –a fact that makes is simpler with less overhead than a web service that uses the SOAP protocol.
Today we'll be looking at soapUI but you can also programmatically test a rest service using a library like rest-assured without using a vendor tool.
(If you're interested in learning how to use soapUI to test SOAP web services, check out my post/video: soapUI – How to test a web service.)
Twitter API
For this example, I'll be using the Twitter API (GET statuses/user_timeline Resource) to perform a search for and return the last five tweets on my Twitter timeline:
https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=jcolantonio&count=5
How to use soapUI to test REST service
Here are the steps to follow to get started using the free version of soapUI with REST testing:
-
- Create a new project in soapUI named TWITTER REST (make sure you select the Opens dialog to create REST Service); click “OK”
- Create a new project in soapUI named TWITTER REST (make sure you select the Opens dialog to create REST Service); click “OK”
-
- In the New REST Service dialog, enter the Service Endpoint:
https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=jcolantonio&count=5
- Click on the “Extract Resource/Method” and click “OK”
-
-
- On the New REST Resource dialog, click on the “Extract Params” button. You should have two parameters named id and count. Click “OK”
- On the New REST Resource dialog, click on the “Extract Params” button. You should have two parameters named id and count. Click “OK”
-
- In the New REST Method dialog, enter the Method Name, “GET JOE TIMELINE” and click “OK”
- In the New REST Method dialog, enter the Method Name, “GET JOE TIMELINE” and click “OK”
-
- Click on the “GET JOE TIMELINE” Request 1 and click on the green run arrow
- Click on the “GET JOE TIMELINE” Request 1 and click on the green run arrow
- Cool! You should be able to see the XML results returned from the Twitter service:
Now let's add assertions to validate the response
In order to verify that the correct response has been returned, you'll need to add an assertion. For this example our assertion is going to verify that five twitter entries were indeed returned by response of the service.
-
- First, click on the request's “Add this REST request to a testcase” icon.
- First, click on the request's “Add this REST request to a testcase” icon.
-
- Name the testsuite “TWITTER REST,” and the TestCase “GET TIMELINE.”
- Add the Request to TestCase. Accept all the default values and click “OK.”
- Name the testsuite “TWITTER REST,” and the TestCase “GET TIMELINE.”
-
- You should now have a TWITTER REST testsuite under your soapUI project:
- You should now have a TWITTER REST testsuite under your soapUI project:
Click on “GET JOE TIMELINE” Request and click “Assertions” then click on the “Add an assertion” icon.
-
- In the “Add Assertion” screen, click on Property
Content>XPath Match and click the “Add” button.
- In the “Add Assertion” screen, click on Property
-
- For the XPath Expression, enter:
- For the XPath Expression, enter:
declare namespace ns1='http://www.w3.org/2005/Atom'; count(//ns1:feed/ns1:entry)
-
- Expected Result: enter “5.”
- Expected Result: enter “5.”
- Click the “Save” button and re-run.
- Your “assertion” should be green.
How to Test a REST service and Rest Easy
That's it! You can now rest easy in the knowledge that you now know how to test a REST service using soapUI!
Hi Joe,
can you please suggest how to automate gwt graphs(pie charts,bar charts, performance graph).
I need to capture x-axis and Y axis values( i.e 2010-2011,2011-2012) in graphs.We are using QTP-10.00 and Application developed in Google Web Development ToolKit(GWT).
Thanks in Advance.
Regards
Venkat gudi
venkat » Hi Venkat – I’ve been trying against some Google graph samples without any luck. I have not tried creating my own sample to see if there is a method that can be exposed that will return X and Y info. You might want to check with your developers to see if this is an option.
Thanks joe…
Hi Joe,
Can you tell me, what I must change in the count(//ns1:feed/ns1:entry) because I dont have in the xml File.
Thanks
xenon » Are you following this exact example? It should be there unless something has changed with the API.
Hi,
I am trying the same thing form the SoapUI but i am not able to get the response in SoapUI. Do we need to change some settings in the Soapui to connect to Internet like proxy setting..?? i am able to get the number of tweets in browser using SoapUI API but not from SoapUI. please help.
thanks in advance
Rohit » Hi – You might need to add your companies proxy settings under soapUI>File>Preferences>Proxy Settings.
What if if the response is like below? How do i need to test with xPath expression?Can you please guide me.
<![CDATA[
type=”text/javascript”>
]]>
Hi Joe,
You might also be interested in WCFStorm.Rest (http://www.wcfstorm.com/wcf/learn-more-rest.aspx). It’s also a REST test tool which has some pretty good features IMHO
Vams » Check out the answer for this question on stack overflow: http://stackoverflow.com/questions/568315/how-do-i-retrieve-element-text-inside-cdata-markup-via-xpath
Hi Joe,
Do you have sample video or steps for POST JSON request and assertions.
I am now ok with GET but still not with POST. It would really help.
Thanks,
Jaymin.
Hi Joe,
I am new to web services testing and to soapUI tool. But then with the help of few online resource I see a good picture on “what is a webserivce , HOw to works, what is Get and Post request?”. And then I used soapui tool to try a REST service and i got output as what i needed. Now my query is, if soapui tool is making life easy to send GET or POST requests with just few data’s (such as url) and clicks then there is a possibilty to do same thing without using tool. If it is possible then can you give me some inputs on how to do it?Thank You krish
krish » Sure you could use straight programming langues like C# and Java. What language are you using?
Jaymin » Hi Jaymin – I’m working on an example and will post it once I get it working. Thanks!
Heyy Joe.. Am also looking for POST JSON sample video.. When i was searching for solution, i got this link http://www.soapui.org/forum/viewtopic.php?t=11065 but am not good enough to explore this.. :( Please make a sample and share if possible very soon..
Regards,
Ganesh
Ganesh » Hi Ganesh – I’m working on it. Just finding it hard to find a sample API that I can use to do this with.
Ok Joe.. :) thanks for ur update.. :)
I had followed the same steps but getting pls help
Dear Joe,
I am new to the testing of HTTP services. I have found the SOAP UI as a tool to automate my testing activities. Could you please help me with what I am trying to achieve.
I have a set of XSD’s and XML messages and I need to fire the message over an http service, get the response back. I have around 52 messages, each having 300 more test cases. I need to automate all the cases and make a functional testing for an entire flow.
Please help me in setting up a project with this and to achieve the result.
Dear Joe,
if I would like to test whole Twitter web service what endpoint should I choose?
Best Regards,
Not sure what you mean by the “whole” twitter web services. I think you would have to use a bunch of the twitter apis to create a scenario like that. I don’t think there is just on wdsl for all the twitter functionality.
Hi Joe, is it possible to define our own response so that webservice return the same ?
for example when you connect to rest webservice with request A then we know webservice will alway send respose B but I want response as per my requirement let say response C. is it possible ?
Please can you reply to my email at kapil.jain@cgi.com
Ho not sure I understand – you want to mock a response that you know you will always get without actually calling the webservice that created the response?
Hello Joe,
Do you have any video for load testing in soap ui? Also could you explain the parameter use in load testing like thread, delay, limit..etc.
Thanks
Hi Joe Colantonio,
I am using SOAP UI PRO 4.6.3 version. Here i am unable to given REST service Name and also not editing. It is taking url name like(localhost).In File–> New REST Project —> url. example “http://localhost/CurrencyConvertor.asmx”. then it is taken service name as “localhost”. But here i need to give domain name.
Can you please help me.
Thanks,
Ram.
Hi Sagar – unfortunately I don’t have any videos for load testing using loadUI. I will add it to my list of possible future posts. thanks for the suggestion!
Are you sure that it is a REST service and not a SOAP Web Service?
We need to get the oauth key right? can you please give some info on this
This does not work with updated twitter api
Hi Ram – yeah twitter changed their API awhile back and I have not updated the post yet. It is on my to-do list. thanks!
After the 6th step I’m getting following error
Sorry, that page does not exist
How can we set Matrix parameters of same name in soapUI tool testing?
Twitter changed their API. You know need to pass it outh authentication to have it work. I have on my to-do list to redo this with the new method
Hi Joe,
1) From where screen_name parameter should be taken?
2) Is there any need to add authorization in SOAP UI?
i have post the rest api request where i am getting the error in response HTTP Status 415 – Cannot consume content type…so my question is where i need to set the content type
thanks in advance
regards
mahadev
hi,
i have 40+ rest so how to use all rest api in rest one by one without any human interface.can i use script in SOAPUI? if yes, so please give me a step how to use it