Forum Discussion

Zuzana's avatar
Zuzana
New Contributor
8 years ago

XPath in groovy script: How to use a variable in contains() function?

Hello,

 

I struggle with using a groovy variable inside a contains() function of XPath statement within a SoapUI property.

 

Having this xml:

 

<ResultSet>
  <Row>
   <Id>1</Id>
   <Description>Something1</Description>
  </Row>
  <Row>
   <Id>2</Id>
   <Description>Something2</Description>
  </Row>
</ResultSet>

 

 

This works:

 

log.info context.expand('${JdbcRequest#ResponseAsXml#//Row[ Description/contains(., "thing2") ]/Id}');

Result: 2 (which is correct).

 

 

These syntaxes don't work:

 

groovyVariable = "thing2"
log.info context.expand('${JdbcRequest#ResponseAsXml#//Row[ Description/contains(., groovyVariable) ]/Id}');
log.info context.expand('${JdbcRequest#ResponseAsXml#//Row[ Description/contains(., ${groovyVariable}) ]/Id}');
log.info context.expand('${JdbcRequest#ResponseAsXml#//Row[ Description/contains(., "${groovyVariable}") ]/Id}');
log.info context.expand('${JdbcRequest#ResponseAsXml#//Row[ Description/contains(., "$groovyVariable") ]/Id}');
log.info context.expand('${JdbcRequest#ResponseAsXml#//Row[ Description/contains(., $groovyVariable) ]/Id}');

Result: 1 (first element in the structure) or nothing.

 

Is there a way how to resolve a groovy variable inside an XPath function please? I bet there  must be a combo I didnt try and which is the correct one :).

 

I use ReadyAPI 2.1.0.

 

Thank you,

Zuzana

 

 

  •  

    log.info context.expand('${JdbcRequest#ResponseAsXml#//Row[ Description/contains(., "' + groovyVariable + '") ]/Id}')

    The string is closed before the variable is added then the end of the string

     

  • PaulMS's avatar
    PaulMS
    Super Contributor

     

    log.info context.expand('${JdbcRequest#ResponseAsXml#//Row[ Description/contains(., "' + groovyVariable + '") ]/Id}')

    The string is closed before the variable is added then the end of the string

     

    • Zuzana's avatar
      Zuzana
      New Contributor

      Hi Paul,

       

      Thank you for the advice, this solution works.

       

      Zuzana