Forum Discussion

sanj's avatar
sanj
Super Contributor
7 years ago
Solved

xpath can help you when contains does not work!

Look at the following text

Expected - En savoir plus sur l'aide avec un fauteuil roulant à l'aéroport.
Actual - En savoir plus sur l'aide avec un fauteuil roulant à l'aéroport.

I tried using contains - could not find the french content

But when I used xpath to point to the actual content no issues there!

So sometimes contains may not work and you will have no choice but to use xpath

Spent my whole morning and this one really baffles me

  • Relative XPATH has made my life a bit easier. 

     

    Let's say you have the following XML Structure

     

    <Parent>

          <Child1>

                <DataPiece>A</DataPiece>

          </Child1>

          <Child2>

                <DataPiece>B</DataPiece>

          </Child2>

          <Child3>

                <DataPiece>C</DataPiece>

                <DataPiece2>1</DataPiece2>

          </Child3>

    </Parent>

    Let's say you want Child3, DataPiece2.

    Normally you have to use //Parent/Child3/DataPiece2. That's listing out the entire XPATH from start to finish. That can get cumbersome really quickly.

    A relative xpath would instead be
    //DataPiece2

    This is really helpful, but you have to be careful in your response that DataPiece2 doesn't show up at multiple levels. 

     


    Does that help at all?

7 Replies

  • groovyguy's avatar
    groovyguy
    Champion Level 1

    I've found that the "Contains" or "Not Contains" assertions can be very misleading and lead to false positives and/or negatives, depending on how your assertions are set up. I typically only use them with specific UUID sort of data types where I know there's a very very slim chance that the data will show up if I don't want it to.

     

    I typically stick to XPATH, or groovy scripting with XPATH. 

    • sanj's avatar
      sanj
      Super Contributor

       groovyguyCan you please tell me how I can use relative paths?

      Yeah groovy scripting or xpath gets rids issues that contains may have especially when one is doing string searches.

       

       

      • groovyguy's avatar
        groovyguy
        Champion Level 1

        Relative XPATH has made my life a bit easier. 

         

        Let's say you have the following XML Structure

         

        <Parent>

              <Child1>

                    <DataPiece>A</DataPiece>

              </Child1>

              <Child2>

                    <DataPiece>B</DataPiece>

              </Child2>

              <Child3>

                    <DataPiece>C</DataPiece>

                    <DataPiece2>1</DataPiece2>

              </Child3>

        </Parent>

        Let's say you want Child3, DataPiece2.

        Normally you have to use //Parent/Child3/DataPiece2. That's listing out the entire XPATH from start to finish. That can get cumbersome really quickly.

        A relative xpath would instead be
        //DataPiece2

        This is really helpful, but you have to be careful in your response that DataPiece2 doesn't show up at multiple levels. 

         


        Does that help at all?