Forum Discussion

shubhimu's avatar
6 years ago

How to Parametrize gpath using XMLSLuper


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="">
<ns2:abc xmlns:ns2="http://xyz" xmlns:ns3="http://xyz" xmlns:ns4="http://xyz">


def xml = new XmlSlurper().parseText(response)
{it.did } 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 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 } xml.size() xml[0]


Can someone please let me know their input on this ?







3 Replies

  • aaronpliu's avatar
    Frequent Contributor

    Hi shubhimu                             ,


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


    if defined variable to save, then using


    // assumed that variable: a, b, c
    • shubhimu's avatar

      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.




      • shubhimu's avatar

        I got the solution. 


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