Forum Discussion

derekwrobel's avatar
derekwrobel
New Contributor
13 years ago

DataSink - possible to save/output multiple xpath nodes?

As part of my tests, in addition to performing asserts, I also output specific xpath nodes of the response messages into the Excel document used as a DataSource. This way, in the excel document, anyone else can quickly see what the expected response message is, and what the actual was.

However, I need to extract multiple nodes because of the structure of our response:
<Resposne>
<Message>
<string>Response 1</string>
<string>Response 2</string>
<string>Response 3</string>
</Message>
</Response>

I've tried several different ways to grab all <string> tags but it seems DataSink will only grab the first one it finds:
//Message/*/text()
//string/text()

Not sure what I need to do to include all strings when using DataSink to export the values to Excel. Anyone have any ideas? The problem is that I do not always get a response with 3 strings. It can be anywhere from 1-5 (possibly more).

3 Replies

  • M_McDonald's avatar
    M_McDonald
    Super Contributor
    Can't you just concatenate references to the individual elements:

    ${Test Request#Response#//Response[1]/Message[1]/string[1]/text()}, ${Test Request#Response#//Response[1]/Message[1]/string[2]/text()}, ${Test Request#Response#//Response[1]/Message[1]/string[3]/text()}, ${Test Request#Response#//Response[1]/Message[1]/string[4]/text()}, ${Test Request#Response#//Response[1]/Message[1]/string[5]/text()}
  • Thanks, although this would work, as I mentioned, the amount of nodes is different for each iteration of data. Sometimes, I may only have 1 string. Other times, I may have 5. I don't want to have to have 5 concats just in case. It just looks messy and confusing if someone needs to work on the project. For now I put it in, but would like a more elegant solution if possible.
  • M_McDonald's avatar
    M_McDonald
    Super Contributor
    Sure, you can do it with a Groovy step:

    groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )

    holder = groovyUtils.getXmlHolder(context.expand( '${Test Request#Response}' ))
    return holder.getNodeValues("//Response/Message/string").join(",")

    Assuming 3 strings returned you will get

    [tt:2vmbmkup]Response 1,Response 2,Response 3[/tt:2vmbmkup]