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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Web Service Testing Using Neoload
  • Implementing WOPI Protocol For Office Integration
  • Building REST API Backend Easily With Ballerina Language
  • How To Validate HTTP Post Request Body - Restful Web Services With Spring Framework | Spring Boot

Trending

  • How to Convert XLS to XLSX in Java
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 3: Understanding Janus
  • Memory Leak Due to Time-Taking finalize() Method
  • The Ultimate Guide to Code Formatting: Prettier vs ESLint vs Biome
  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.

By 
Remi Jullian user avatar
Remi Jullian
·
Updated Oct. 23, 18 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
35.3K 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.

Related

  • Web Service Testing Using Neoload
  • Implementing WOPI Protocol For Office Integration
  • Building REST API Backend Easily With Ballerina Language
  • How To Validate HTTP Post Request Body - Restful Web Services With Spring Framework | Spring Boot

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!