Skip to main content

Development

Automating REST services using Soap UI Pro

“A Web service is a method of communication between two electronic devices over a network. It is a software system designed to support interoperable machine-to-machine interaction over a network.”

There are various tools available to test web services. Some of them are Soap UI, Soap UI Pro, Test Maker, Web Inject etc. The most common tool we use is Soap UI.

Soap-UI Pro (the licensed version) has come up with a user friendly UI. It gives utility to create the test data (to read/write from external files) step, create a data connection (to read/write from database tables) step and property Transfer (to transfer property between steps) step. It also provides a utility called Groovy script to achieve any validations/operations not possible with default SOAP UI steps.

Let’s look at the task that needs to be performed:-

Objective: – To automate a sample login (authenticateMember) REST web service using Soap UI Pro.

Resources: – Soap UI Pro tool, Input Excel data file, Output Excel data file.

Solution: – I will be using pre-defined soap UI steps to read/write data from external excel file and groovy script to perform few validations/operations those are not possible with the default steps.

Step 1:- Prepare the Input Excel data file (Sheet Name- Login)

InputDataSheet

InputDataSheet

Assumption is that the service accepts username and password; we are passing both the parameters from the test data input sheet.

Step 2:- Prepare the Output Excel data file (Sheet Name – Login)

OutputDataSheet

OutputDataSheet

Assumption is that the service returns statusCode and statusDesc. Let’s see what the extra output fields are:-

  • testResult – Insert either of Pass/Fail based on assertions.
  • statusCode – Insert the status code returned from the REST response.
  • statusDesc – Insert the status description returned from the REST response.
  • Request – Insert the raw REST request.
  • Response – Insert the complete REST response returned from the service.

Step 3:- Create the Automation test suite.

  • Open Soap UI Pro, import your project.
  • Go to the webservice endpoint and right click to see the context menu.
  • Click on option “Generate TestSuite”.
Context Options

Context Options

  • In the Generate Test Suite pop-up, check the checkbox authenticateMember (sample service name) service and click OK.

 

Add Service Request

Add Service Request

  • Once done, we will be able to see the test suite (authenticateMember) generated with the selected service.

ProjectTree-1

 

Step 4:- Create testcase under test suite.

  •  Go to the created test suite and right click to see the context menu.
  •  Click on option “New TestCase”.
Context Options

Context Options

  • In the New Test Case pop-up, give the name of the new testcase and click OK.
NewTestCase

NewTestCase

 

  • Once done, we will be able to see the test case (AuthenticateMember) generated.
AuthenticateMember

AuthenticateMember

 

Step 5:- Create test steps under created test case.

(i)  Create DataSource step to read username and password from the test data input sheet

  • Go to the AuthenticateMember testcase and right click to see the context menu.
Context Options

Context Options

 

 

 

  • Click on option “DataSource”.
  • In the Add Step pop-up, give the name of the step and click OK.
DataSource

DataSource

  • Once done, we will be able to see the test step (DataSource) generated.
DataSource Step

DataSource Step

 

  • In the DataSource step, click on the dropdown next to DataSource label and select “Excel”.
DataSource Configuration

DataSource Configuration

 

  • Since we need 2 values (Username and Password), we will be creating 2 properties in this step to capture the values from the input data excel file. To create property, click on “+” sign on the left corner, name the property name and click OK.
AddProperty

AddProperty

 

 

  • Create properties with name “Username” and “Password”.
DataSource with properties created

DataSource with properties created

 

  • Now we need to browse for out test input data sheet in the File: text box, give the proper sheet name in Worksheet text box and give the cell number where Username and Password cells are located in the test input data sheet.
  • Once done run the test step, to run click on the green coloured Play button. We will be able to see the properties populated with the values we stored in test input data sheet.
DataSource executed

DataSource executed

 

(ii)  Create REST request Test Step

  • Assuming that authenticateMember REST request takes Username and Password as test input.
  • Go to the AuthenticateMember testcase and right click to see the context menu.
Context Options

Context Options

 

  • Click on option “REST Test Request
  • In the Add Step pop-up, give the name of the test step and click OK
REST Test Request

REST Test Request

  • Once done, we will be asked to select the REST request which we want to import. Select the appropriate REST request(depending on the project) from the dropdown and click OK
New Rest Request

New Rest Request

 

 

  • Once done, we will be able to see REST Test Request test step created.
REST Test Request created

REST Test Request created

 

(iii)  Parameterize the input data in created REST Test Request test step.

  • Go to the Request tab where we need to enter Username and Password.
REST Test Request with request parameters

REST Test Request with request parameters

 

 

  • To remove the hard coded values; right click on “admin” so that context menu appears and selects the value; GetData-> DataSource -> Username. Do the same for Password.
ParameterizeUsername

ParameterizeUsername

 

 

  • Once done, we will be able to see the input data has been parameterized.
Parameterized

Parameterized

(iv)  Execute the REST request and create assertions.

  • Assuming that if we pass valid Username and Password, statusCode=0 and statusDesc=Success is returned.
  • Hence we need to create assertions for the above two fields.
  • Go to statusCode field in REST response and right click so that context menu appears.
REST Response

REST Response

 

 

  • Select AddAssertion -> forContent.
Add Assertion

Add Assertion

  • Verify Xpath for the source node of “statusCode” and Expected Result
Xpath Match Config

Xpath Match Config

 

 

  • Do the same for statusDescription.
  • Once done, we will be able to see 2 Assertions created at the left bottom of REST request step.
CreatedAssertions

CreatedAssertions

(v) Create Groovy script to validate the assertions.
  • We will be writing a customized groovy code to validate the assertions whether they are PASSED or FAILED. If they are passed then we will insert “Passed” into the execution result column in output data sheet. Else insert “Fail” if any one of the assertions failed.
  • Go to the AuthenticateMember testcase and right click to see the context menu.
Context Options

Context Options

  • Click on option “Groovy Script
  • In the Add Step pop-up, give the name of the test step and click OK.
Groovy Script

Groovy Script

  • Once done, we will be able to see the test step (Groovy Script) generated.
Groovy Script step

Groovy Script step

(vi)  We will be writing code to validate the assertions.

//import class for assertion

import com.eviware.soapui.model.testsuite.Assertable

//Create variables

def testStepSrc = testRunner.testCase.getTestStepByName(“REST Test Request”)

def propertyVal = testRunner.testCase.getTestStepByName(‘DataSink’)

// Count number of assertions created in REST request

def counter = testStepSrc.getAssertionList().size()

// if status matches string “VALID”, then pass else fail

for(i=0;i<counter;i++)

{

String status=testStepSrc.getAssertionAt(i).getStatus()

if(status!=’VALID’)

{

output=’FAIL’

}

else

{

output=’PASSED’

}

}

testRunner.testCase.testSuite.setPropertyValue(“result”, output)

//Create a property Status and put the value either pass/fail in it depending on the assertions status

propertyVal.setPropertyValue(“Status”,output)

 

(vi)  Create DataSink step

  • DataSink step is required to setp up property values and the same can be directly inserted into the output data sheet.
  • Since we will be inserting 5 values (result, statusCode, statusDescription, request and response), we will be creating 5 properties in DataSink step and insert the same into output data sheet.
  • Go to the AuthenticateMember testcase and right click to see the context menu.
Context Options

Context Options

 

 

  • Click on option “DataSink” option.
  • In the Add Step pop-up, give the name of the test step and click OK
Data Sink

Data Sink

 

  • Once done, we will be able to see DataSink step created.
Data Sink step

Data Sink step

 

 

  • Create 5 properties and the values will get supplied from the propertyTransfer step (this will be added in the next step).
Create Data Sink properties

Create Data Sink properties

 

  • To create the properties follow the same approach as we followed while creating properties in DataSource
  • Enter the Configuration parameters; here we will be passing the path of output file, the specific worksheet and cell number on which we want to insert the values.
  • No need to give values to the created properties, this will get inserted from propertyTransfer step.

 

(vii)  Create PropertyTransfer

  • Property transfer is required to transfer property values from one step to another. We will be transferring property values from REST response and GroovyScript to Data Sink
  • Go to the AuthenticateMember testcase and right click to see the context menu.
Context Options

Context Options

 

  • Click on option “Property Transfer” option.
  • In the Add Step pop-up, give the name of the test step and click OK
Property Transfer

Property Transfer

 

  • Once done, we will be able to see Property Transfer step created.
Property Transfer step

Property Transfer step

 

 

  • We will be creating 4 property values (statusCode, statusDescription, request and response). The property status will be populated to DataSink step from GroovyScript.
Property Transfer created

Property Transfer created

  • After we are done creating the properties, we need to select Source, Property and Target, Property. This will enable transferring of properties. To transfer statusCode property

 

Source – REST Test Request

Property – ResponseAsXML

Xpath – node description of statusCode

 

Target – Data Sink

Property – statusCode

 This will transfer the statusCode value to DataSink’s statusCode property. Do the same for the other properties as well.

 

  • After you are done, execute this step. Open DataSink step and execute, we will be able to see property values for statusCode, statusDescription, request and response.
Data Sink step populated

Data Sink step populated

  • This is how the project flow will look like. Because the steps are dependent on one another hence proper flow is mandatory for the execution to go through.
ProjectTree

ProjectTree

 

 

Step 6:- Execute the test suite

  • Execute the testsuite by clicking on the Play button for the test suite.
Test Suite

Test Suite

 

 

  • After we are done, verify the DataSink We will be able to see all the property values.
Data Sink populated

Data Sink populated

 

 

  • Verify the output Data sheet, The data has been inserted to the output data file starting from the cell we mentioned in the DataSink step.
OutputDataSheet (After execution)

OutputDataSheet (After execution)

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Sohit Kanwar

More from this Author

Categories
Follow Us
TwitterLinkedinFacebookYoutubeInstagram