cancel
Showing results for 
Search instead for 
Did you mean: 

Store a value from XML/XPath output of a REST query in a SoapUI property for later use

SOLVED
Highlighted
Occasional Contributor

Store a value from XML/XPath output of a REST query in a SoapUI property for later use

I'm new to SOAP, REST, and SoapUI. I'm working to build a test case that simulates a user login & then extracts the user list from the server. My log in command is working, and I get the XML output I want:

 

<MGRestTokenResponse xmlns="http://schemas.datacontract.org/2004/07/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
   <Message>success</Message>
   <Token>---THIS IS THE DATA I WANT---</Token>
</MGRestTokenResponse>

 

I'm trying to get the Token value stored in a SecurityToken property inside SoapUI but nothing is working. I've tried using XmlToolBox  to get an XPath result and I've been given:

 

/MGRestTokenResponse[@xmlns="http://schemas.datacontract.org/2004/07/"]/Token/text()

 

or

/MGRestTokenResponse[@xmlns="http://schemas.datacontract.org/2004/07/"]/Token/text()

 

but nothing I'm trying is working.

 

The environment I'm working in is REST only. How do I extract values from the responses & transfer them to a property?

 

 

Tags (4)
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Occasional Contributor

After posting on StackOverflow, I finally got an answer to this. The Groovy Code I posted previously *is* one way to get the Token. However, I was more interested in getting the XML property directly in the Property Transfer step.

 

To do this, I set the Source to the previous request (GetSecurityToken) and set the Property to Response, Path language XPath. Now; inside the txt field for Source I needed to palce the following:

declare namespace ns1='http://schemas.datacontract.org/2004/07/'; 
 //ns1:MGRestTokenResponse[1]/ns1:Token[1]

For the Target, I specify my test step properties and then the sectoken property. This causes the XML value to be transfered in a single step versus storing the entire output & using Groovy code to extract it.

View solution in original post

5 REPLIES 5
Highlighted
Occasional Contributor

I found this solution in the SoapUI forum recently: https://community.smartbear.com/t5/SoapUI-Open-Source/Can-t-extract-a-value-from-a-label-to-a-Proper...

 

Is it the same for REST as it is with SOAP?

Tags (3)
Highlighted
Occasional Contributor

I'm still trying to figure this out, but no matter what I Google/YouTube for I simply cannot find it. It has to be something I'm doing wrong as I can't be the only person that needs to transfer data from an XML/JSON response into a Property inside of SoapUI for later use.

 

Why is this proving so challenging?

Tags (3)
Occasional Contributor

Ok; after *MUCH* digging, I've come up with something that is working. However, I'm not sure if this is correct as I can't tell if I'm using the tool properly. My solution was the following:

  1. Use a "Property Transfer" step in the following manner:
    1. Transfer: Xfer Token
    2. Source: GetSecurityToken, Property: ResponseAsXml, Path lang: XPath
    3. Target: Set Test Properties, Property: sectoken, Path lang: XPath
  2. Run the following Groovy code:
    def content = context.expand('${Set Test Properties#sectoken}')
    def xml = new XmlSlurper().parseText(content)
    def token = xml.getAt("Token")
    testRunner.testCase.getTestStepByName("Set Test Properties").setPropertyValue("sectoken",token.toString())

Is this the correct way to access XML responses inside of SoapUI? Should I be doing any of this inside the Property Transfer step? This is giving me the result I'm after, but I do not know if this is the correct way to do it in SoapUI.

Tags (3)
Highlighted
Occasional Contributor

After posting on StackOverflow, I finally got an answer to this. The Groovy Code I posted previously *is* one way to get the Token. However, I was more interested in getting the XML property directly in the Property Transfer step.

 

To do this, I set the Source to the previous request (GetSecurityToken) and set the Property to Response, Path language XPath. Now; inside the txt field for Source I needed to palce the following:

declare namespace ns1='http://schemas.datacontract.org/2004/07/'; 
 //ns1:MGRestTokenResponse[1]/ns1:Token[1]

For the Target, I specify my test step properties and then the sectoken property. This causes the XML value to be transfered in a single step versus storing the entire output & using Groovy code to extract it.

View solution in original post

Highlighted
Contributor

def str='<MGRestTokenResponse xmlns="http://schemas.datacontract.org/2004/07/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">'+
'<Message>success</Message><Token>---THIS IS THE DATA I WANT---</Token></MGRestTokenResponse>'
def xml=new XmlSlurper().parseText(str);
xml.depthFirst().each{
if(it.name().equalsIgnoreCase("Token"))
{
log.info "Token : "+it.text()
testRunner.testCase.setPropertyValue("Token",it.text())
}
}

 

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors