DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
What's in store for DevOps in 2023? Hear from the experts in our "DZone 2023 Preview: DevOps Edition" on Fri, Jan 27!
Save your seat
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. SoapUI: Using Context Variables and XmlHolder to Access SOAP Requests and Responses

SoapUI: Using Context Variables and XmlHolder to Access SOAP Requests and Responses

Learn how to use context variables and the XmlHolder class to access SOAP requests and responses.

Remi Jullian user avatar by
Remi Jullian
·
Oct. 23, 18 · Tutorial
Like (3)
Save
Tweet
Share
31.30K Views

Join the DZone community and get the full member experience.

Join For Free

A context variable is a special variable in SoapUI used to store values that can be used in subsequent test steps within a test case. The scope of this variable is between multiple steps in one test case within a SoapUI project, i.e., there is only one context per test case. In this article, we'll learn how to use context variables and the XmlHolder class to access SOAP requests and responses

Create a Groovy script in SoapUI called "Groovy Script." Add the following code:

//Context Variable Scope
context.setProperty("Myname","Remi")

Create one more Groovy script called "Groovy Script 1." Add the below code:

log.info context.getProperty("Myname")

If we want to access the property "Myname" in the Groovy script, we need to run both scripts together as a test case. Select the test case and run it.

To see the output, we have to go to the SoapUI log. SoapUI logs can be accessed by going to the bin folder of SoapUI. By default, this will be available in C:/ProgramFiles/SmartBear/SoapUI/bin/Soapui.log.

Using Context Variables to Access the Request and Response XML

Context variables are also used to access the request and response XML defined in the same test case.

Consider the below project structure in SoapUI:

Image title

Create a Soap request “AddRequest” and tag “Add” operation in the calculator WSDL.

AddRequest will look as below:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/">
   <soap:Header/>
   <soap:Body>
      <tem:Add>
         <tem:intA>5</tem:intA>
         <tem:intB>5</tem:intB>
      </tem:Add>
   </soap:Body>
</soap:Envelope>

AddResponse will look as below:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <AddResponse xmlns="http://tempuri.org/">
         <AddResult>10</AddResult>
      </AddResponse>
   </soap:Body>
</soap:Envelope>

By creating a Groovy script using a context variable with the "expand" method, we can access the request and response XML, as below:

//Using context object retrieve the request of a soaprequest - AddRequest
def req = context.expand('${AddRequest#request}')
log.info req

//Using context object retrieve the response of a soaprequest - AddRequest
def res = context.expand('${AddRequest#response}')
log.info res

If we want to parse and get the tag values in the request and response XML, we can with the help of a class called XmlHolder. This is a class in the Groovy API which is used to parse the request and response XML.

We have to import the package com.eviware.soapui.support.XmlHolder for the XMLHolder class to run successfully. Using the “response” object, we can read the response of the XML. Sometimes we want a single tag to be retrieved, sometimes we need multiple tags or duplicate tags to be retrieved.

To retrieve a single tag from the XML, we use the getNodeValue method.

//To retrieve the result of request and response tags using getNodeValue Method

def reqvalue1 = request.getNodeValue('//tem:intA')
log.info reqvalue1

def reqvalue2 = request.getNodeValue('//tem:intB')
log.info reqvalue2

def resvalue = response.getNodeValue('//ns1:AddResult')
log.info resvalue

To retrieve multiple tags from the XML, we use the getNodeValues method. It returns a string array.

//To count the total tags coming in the input and output

def req_items = request.getNodeValues('//tem:intA') // There is one tag
log.info "Total request items -> "+req_items.length

def res_items = response.getNodeValues('//ns1:AddResult')// There is one tag
log.info "Total response items -> "+res_items.length

For full example to access the Soap request and response:

//Groovy Script

import com.eviware.soapui.support.XmlHolder

//Context Variable Scope
context.setProperty("Myname","Remi")

//Using context object retrieve the request of a soaprequest - AddRequest
def req = context.expand('${AddRequest#request}')
log.info req

//Using context object retrieve the response of a soaprequest - AddRequest
def res = context.expand('${AddRequest#response}')
log.info res

//To parse and get the tags in req/res we have to use XMLHolder class
def request = new XmlHolder(req)
def response = new XmlHolder(res)

//To retrieve the result of request and response tags using getNodeValue Method

def reqvalue1 = request.getNodeValue('//tem:intA')
log.info reqvalue1

def reqvalue2 = request.getNodeValue('//tem:intB')
log.info reqvalue2

def resvalue = response.getNodeValue('//ns1:AddResult')
log.info resvalue

//To count the total tags coming in the input and output

def req_items = request.getNodeValues('//tem:intA') // There is one tag
log.info "Total request items -> "+req_items.length

def res_items = response.getNodeValues('//ns1:AddResult')// There is one tag
log.info "Total response items -> "+res_items.length

//To print the values of tags - use for loop

for (x in req_items){
log.info x
}
//Groovy Script 1

log.info context.getProperty("Myname")


Summary

Context variables in SoapUI are common for all steps in a test case. In our example, the context is the same for the Soap request "AddRequest" and for the Groovy scripts "Groovy Script" and "Groovy Script 1."

Requests SOAP Web Protocols

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Spring Boot Docker Best Practices
  • Comparing Flutter vs. React Native
  • Simulating and Troubleshooting StackOverflowError in Kotlin
  • How To Convert HTML to PNG in Java

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: