Forum Discussion

shubhimu's avatar
shubhimu
Contributor
7 years ago

How to Parametrize gpath using XMLSLuper

Hi,

I want to parametrize Gpath in XMlSlurper from excel file.

My requirement is to provide gpath in excel file  and want to parametrize gpath to get the particular tag.

 

Please see below for my code:

def response ='''<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<ns2:abc xmlns:ns2="http://xyz" xmlns:ns3="http://xyz" xmlns:ns4="http://xyz">
<ns2:deviceUnits>
<ns2:devicePorts>
<ns2:portNumber>1</ns2:portNumber>
<ns2:serviceEntities>
<ns2:networkId>16869467</ns2:networkId>
<ns2:didTargets>
<ns2:did>12003004001</ns2:did>
<ns2:didType>PHYS</ns2:didType>
<ns2:startDate>2012-09-25</ns2:startDate>
</ns2:didTargets>
<ns2:startDate>2012-09-25</ns2:startDate>
</ns2:serviceEntities>
</ns2:devicePorts>
</ns2:deviceUnits>
</ns2:abc>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>'''

 

def xml = new XmlSlurper().parseText(response).Body.abc.deviceUnits.devicePorts.serviceEntities.didTargets.each
{it.did }
log.info xml.size()

 

Uisng above code everything is working fine for me but when I am able to fetch the value of the above mentioned gpath Body.abc.deviceUnits.devicePorts.serviceEntities.didTargets from excel file and set it @  Project level property and when i am passing that variable like below then its not working. I am not sure what will be the return type of Gpath fetched from excel 

 

def pro=testRunner.testCase.testSuite.project

def a=pro.getPropertyValue("gpath")
def xml = new XmlSlurper().parseText(response).a.each
{it.did }
log.info xml.size()

log.info xml[0]

 

Can someone please let me know their input on this ?

 

Thanks,

Himanshu

 

 

 

  • aaronpliu's avatar
    aaronpliu
    Frequent Contributor

    Hi shubhimu                             ,

     

    Your GPath should looks like below then it's able to work

     "Body"."abc"."deviceUnits"."devicePorts"."serviceEntities"

    if defined variable to save, then using

     

    // assumed that variable: a, b, c
    "$a"."$b"."$c"
    • shubhimu's avatar
      shubhimu
      Contributor

      Yeah i know this and its working for me ..

       

      But i dont want to break it in to n number of variables.

       

      I want one variable should suffice the purpose...

       

      Please let me know if we can do this way.

       

      Thanks,

      Himanshu

      • shubhimu's avatar
        shubhimu
        Contributor

        I got the solution. 

         

        def xml=getNodes(resource,xpaths)
        log.info xml.did.size()
        def getNodes(doc,path){
        def nodes = doc;
        path.split("\\.").each {
        log.info "node size for "+ "${it}" +" is "+nodes.size()
        if(nodes.size() == 1){
        nodes = nodes."${it}";
        }else{
        nodes = nodes[0]."${it}"
        }
        log.info "${it}"
        }
        log.info "Nodes is "+nodes
        return nodes
        }