Forum Discussion

dbaray's avatar
dbaray
Occasional Contributor
7 years ago

Using getXMLHolder with parameters?

I've seen tons of examples of getXMLHolder calls passing the teststepname#request or teststepname#response and that works fine. However, my input XML arrives via CSV file and gets assigned to DataSourceCsv#xml. I can log DataSourceCsv#xml and it contains the right stuff, e.g.

<transaction><apiname>DoSomething</apiname><company>3457714</company><house>100258</house><cust>1</cust><opr>INF</opr><lockpid>0</lockpid></transaction>

 

I have to update the lockpid value before executing the request, doing getXmlHolder, then holder["//lockpid"] = whatever. I have tried:

def holder = groovyUtils.getXmlHolder("DataSourceCsv#xml");

and then created a test case param and set its value to DataSourceCsv#xml and tried

def holder = groovyUtils.getXmlHolder("paramXML");

and

def holder = groovyUtils.getXmlHolder( '${#TestCase#paramXML}');

 

All result in "org.apache.xmlbeans.XmlException: error: Unexpected element CDATA error at line X". Do I have to manipulate my param in some way so getXmlHolder likes it, or is there a completely different way to do this when you don't have static XML?

  • I don't use the XML holder to manipulate XML so there may be a better way to use it, but the holder has a "getXml()" method which returns a string so you should be able to do something like.

     

    testRunner.testCase.setPropertyValue("paramXML", holder.getXml())

    The holder is an instance of the XmlHolder class.

     

    (As an aside I use the XmlSlurper and XmlParser to manipulate XML I personally find it a little bit more intuitive than the groovy utils / XML holder method.)

  • Lucian's avatar
    Lucian
    Community Hero
    When referencing properties from within a groovy script you need to expand the values like:

    context.expand( '${YourDataSourceStep#someProp}' )
    • dbaray's avatar
      dbaray
      Occasional Contributor

      Thanks so much for your reply. Should have known that, I had to context.expand to log it too. Getting close. Now after I update my holder["//lockpid"] and holder.updateProperty I get the new pid value logged, but my original property isn't changed...

       

      log.info("incoming xml= " + context.expand( '${#TestCase#paramXML}'));

      def  holder = groovyUtils.getXmlHolder(context.expand( '${#TestCase#paramXML}'));

      holder["//lockpid"] = context.expand( '${#TestCase#paramLockPid}' );

      holder.updateProperty();                            this is not updating my TestCase#paramXML property

      log.info("pid now=" +  holder["//lockpid"]);  this shows the new pid value

      log.info("outgoing xml= " + context.expand( '${#TestCase#paramXML}'));   outgoing xml = incoming

       

      Do I now have to do some kind of set property like ...testRunner.testCase.setPropertyValue("paramXML", something); ???

       

      • Radford's avatar
        Radford
        Super Contributor

        I don't use the XML holder to manipulate XML so there may be a better way to use it, but the holder has a "getXml()" method which returns a string so you should be able to do something like.

         

        testRunner.testCase.setPropertyValue("paramXML", holder.getXml())

        The holder is an instance of the XmlHolder class.

         

        (As an aside I use the XmlSlurper and XmlParser to manipulate XML I personally find it a little bit more intuitive than the groovy utils / XML holder method.)

  • Radford's avatar
    Radford
    Super Contributor

    Just to add to what Lucian has said, the documentation explaining property expansions can be found here.

     

    If you are unsure about property expansions, from within the Groovy script editor you can always use the right click menu "Get Data" option. This allows you to construct your property expansion via a visual point and click method. I still use this method all the time.

    • dbaray's avatar
      dbaray
      Occasional Contributor

      Thanks Radford, the right click Get Data trick works great.