automation, qa, web services, rest, soap, soapui

Posts tagged ‘rest’

Automated REST web services using SOAPUI


This is a little example about how we can create an automation framework over a REST API using SOAPUI, ant and jenkins. The main idea is to show how SOAPUI works with REST services and how we can implement this tool to make an stable framework integrating with jenkins.

First at all we need to know some concepts.


REST (REpresentational State Transfer) is an architectural style, and an approach to communications that is often used in the development of web services. The use of REST is often preferred over the more heavyweight SOAP(Simple Object Access Protocol) style because REST does not leverage as much bandwidth, which makes it a better fit for use over the Internet.


SOAPUI is a tool that help us to test web services specially SOAP but also works with REST.


First of all we need an API over which we will test, we can develop a small API, but is better to use a one that already exists. We can find several public APIs in the web, for this example we wil use the lastfm  API (a music site) . This  API provides both  REST and XML-RPC formats. We will use REST.

Create the project.

First thing is create the project opening SOAPUI and go to menu and select:  New REST Project   (CTRL+A+N)


Then we have a dialog requesting the URI in this case:


as a result we have the project created as image below.




We can see a default request created but without parameters, if we execute this request we will have a response with errors as image below, this occurs generally due to malformed URI.


Call to methods

Now we going to do some examples of calls to methods using URIs with the right parameters. Right Click on the service and select New Resource


after Select New Resource from menu a dialog will be displayed, we need to enter the URI for this we will use the URI below:************&format=json


This is a REST request get the similar artists to Cher. once we click on OK  button SOAPUI creates the request with the parameters as the image below


Now we can execute the method clicking on green arrow and we will see the response en the right side of editor as image below



Second example is a method called gettoptracks this get the top tracks from an artists we going to use the URI below:************&format=json



and the results should be something similar to the image below


Suites and Test Cases.

The request generally is used for test the methods, but these request should be included in suites as test cases and those will be executed in our automation project.

We can create a test case clicking on the plus icon next to Method drop-down


Once created the test case and test suite a new request editor is displayed with the same parameters but additionaly with assertion option


In this case we will add a simple assertion: Valid HTTP Status code



as this is a little example the value for status code will be 200




once the assertion has been created we can execute the request and if all is ok the result should be passed



SOAPUI includes a tool ( execute the suites and test cases and generate outputs(reports) You can open the testrunner dialog in project, right click and select Launch TestRunner, the image below should be displayed.



We need to configure TestRunner path in Basic tab and the outputs in Reports tab, the next step click on Launch button and the tests will be executed by testrunner, here is very important to take note about the command execution as show the image below, this command will be used after by ant in jenkins integration


once all test were executed the image below should be displayed


Integration with Jenkins.

In order to integrate our project with jenkins, we need an xml file (build.xml) that will be used in an ant call, below is the build.xml file example. You can notice that the exec tag is the command line that testrunner executed in SOAPUI.

<project name="Soapui Nightly Build" default="testreport" basedir=".">
 <property environment="env" />
 <property name="label" value="${env.JOB_NAME}-${env.BUILD_NUMBER}" />
 <mkdir dir="reports/REST_${env.BUILD_NUMBER}" />
 <target name="soapui">
 <exec dir=".">
 <arg line="sh /opt/SoapUI-5.0.0/bin/ -e -r -a -j -f reports/REST_${env.BUILD_NUMBER} ~/soapui/REST-Project-1-soapui-project.xml
 <target name="testreport" depends="soapui">
 <junitreport todir="reports/REST_${env.BUILD_NUMBER}">
 <fileset dir="reports/REST_${env.BUILD_NUMBER}">
 <include name="TEST-*.xml" /> </fileset>
 <report todir="reports/html" styledir="/usr/share/ant/etc" format="noframes"> </report>

Job Configuration.

Now that we have the build file we can create a job in jenkins, providing the Name and Description for the project like below image:



then we need to setup the build.xml file for this we click on  Add Build Step and Select Invoke ant option



Now we going to add the reports for this the option Add post-build action need to be added. in this case we will choose 3:

Archive the artifacts. to see the detailed executions in txt format

Publish HTML reports. to see the global report in a HTML format

Publish xUnit test result report. to see the junit results




Once created and configured the job the last step is run the job and verify the results (html, txt).

Once executed we will have the results like below image


Below is the HTML report that shows the results of the testing


and the txt file that  shows the report of an individual test




Thanks to everyone who will read, reviewed it and recommended it. I will appreciate your support.