Forum Discussion

skelkar's avatar
skelkar
Contributor
10 years ago

Query for getChildNodes() method ?

Hi,

I have a rest response as below here I want to assert that barometer element count is 6.

 

   <Barometer>
      <OneDay>
         <MLCR>2</MLCR>
         <MLGR>2</MLGR>
         <MLVL>2</MLVL>
         <MMCR>2</MMCR>
         <MMGR>2</MMGR>
         <MMVL>2</MMVL>
         <MSCR>2</MSCR>
         <MSGR>2</MSGR>
         <MSVL>2</MSVL>
      </OneDay>
      <OneMonth>
         <MLCRT>-2</MLCRT>
         <MLGRT>-1</MLGRT>
         <MLVLT>-3</MLVLT>
         <MMCRT>-1</MMCRT>
         <MMGRT>-1</MMGRT>
         <MMVLT>-2</MMVLT>
         <MSCRT>-2</MSCRT>
         <MSGRT>-1</MSGRT>
         <MSVLT>-2</MSVLT>
      </OneMonth>
      <OneWeek>
         <MLCRT>-3</MLCRT>
         <MLGRT>-2</MLGRT>
         <MLVLT>-3</MLVLT>
         <MMCRT>-2</MMCRT>
         <MMGRT>-1</MMGRT>
         <MMVLT>-2</MMVLT>
         <MSCRT>-1</MSCRT>
         <MSGRT>1</MSGRT>
         <MSVLT>-2</MSVLT>
      </OneWeek>
      <OneYear>
         <MLCRT>2</MLCRT>
         <MLGRT>2</MLGRT>
         <MLVLT>2</MLVLT>
         <MMCRT>2</MMCRT>
         <MMGRT>2</MMGRT>
         <MMVLT>2</MMVLT>
         <MSCRT>2</MSCRT>
         <MSGRT>1</MSGRT>
         <MSVLT>2</MSVLT>
      </OneYear>
      <ThreeMonth>
         <MLCRT>1</MLCRT>
         <MLGRT>1</MLGRT>
         <MLVLT>-1</MLVLT>
         <MMCRT>2</MMCRT>
         <MMGRT>2</MMGRT>
         <MMVLT>1</MMVLT>
         <MSCRT>1</MSCRT>
         <MSGRT>2</MSGRT>
         <MSVLT>1</MSVLT>
      </ThreeMonth>
      <ThreeYear>
         <MLCRT>2</MLCRT>
         <MLGRT>2</MLGRT>
         <MLVLT>2</MLVLT>
         <MMCRT>2</MMCRT>
         <MMGRT>2</MMGRT>
         <MMVLT>3</MMVLT>
         <MSCRT>2</MSCRT>
         <MSGRT>2</MSGRT>
         <MSVLT>2</MSVLT>
      </ThreeYear>
   </Barometer>

 

I got the script which gives me this assertion as below -

 


String xPath = "//*:Response/*:Barometer"

int expectedElementCount = 6

def responseNodes = holder.getDomNode(xPath).getChildNodes()
int responseElementCount = 0
for(int i = 0; i < responseNodes.getLength(); i++) {
    def currentNode = responseNodes.item(i)
    short nodeType = currentNode.getNodeType()
    if(nodeType != currentNode.TEXT_NODE) {
        responseElementCount++
        
    }


assert responseElementCount == expectedElementCount

 

Now my question is -

 

Why responseNodes.getLength() = 13 ?

i.e. if I do log.info responseNodes.getLength() it gives me 13.

 

Why is that? and is there any other way to get barometer element count as 6

I do not want to count what is under OneMonth or oneYear just parent nodes under barometer which is 6.

 

pLEASE advise.

 

 

4 Replies

  • Similarly if I change the xpath to -

    String xPath = "//*:Response/*:Barometer/*:OneDay"

    then -

    log.info responseNodes.getLength() = 19 ?

    I would like to know how is the figure 19 is being calculated and is the scrpt i am using is correct .

    • dchandran_1's avatar
      dchandran_1
      New Member

      Hi Skelkar,

       

      I read your script and I think, because you have given getchildnodes() in the holder, it is calculating child nodes as well. 

      Try with out giving getchildnodes() and check if you get the expected result. 

       

      Cheers,

      Dhivya

      • skelkar's avatar
        skelkar
        Contributor

        I have to use the holder to access barometer and its child nodes.

        Script is correct and working fine I just wanted to understand why the count is 13.

        How soap ui is calculating the lenth as 13 if some one can answer and clarify that would be gr8 help.