I have observed many soapui tester/users are struggling with either Groovy Script and/or xPath expression. As part of my continuous efforts to help other fellow testers ( & make my own competitors :( ) i thought of resolving the concerns related to xPath expression. And for concern related to Groovy script, if you are comfortable with any OOPs based programming language (like Java) then you will realize the power of Groovy & it’s Grooviness.

First thing i would say about xPath is that, just a word of motivation, it is most simplest language(okay, expression) to learn after XML. So with my assumption that all the readers/testers are aware of creating & manipulating the XML files, i will begin about xPath.

XPATH – is an expression which is used to navigate the nodes of any XML file.And when we talk about nodes, we always have a root node (Parent of all), then a parent node & few child nodes(which can also be parent to some node). So basically it would a tree structure.


<ns1:Root>
    <ns2:Parent>
       <ns3:Name>Parent 1</ns3:Name>
       <ns4:Child>Child 1</ns4:Child>
       <ns5:Value>1.1</ns5:Value>	
    </ns2:Parent>
    <ns2:Parent attribute="parent 2">
       <ns3:Name>Parent 2</ns3:Name>
       <ns4:Child/>
       <ns5:Value>0.2</ns5:Value>	
    </ns2:Parent>
    <ns2:Parent>
       <ns3:Name parName="Parent3">Parent 3</ns3:Name>
       <ns4:Child>Child 3</ns4:Child>
       <ns5:Value>3.1</ns5:Value>	
    </ns2:Parent>
    <ns2:Parent/>
</ns1:Root>

Following are the Xpath expression which can be used in the Xpath Assertion to check the availability of certain data in response :

To count the number nodes with name Parent (under Root node) :
count(//ns1:Root/ns2:Parent)
#Output : 4


To access the Name Value of the 2nd Parent element node :
//ns1:Root/ns2:Parent[2]/ns3:Name
#Output : Parent 2


To access the node with specific Attribute value (attribute="Parent 2"):
//ns1:Root/ns2:Parent[@attribute='parent 2']/ns3:Name
#Output : Parent 2


To verify the value of the selected node (return type would be Boolean):
//ns1:Root/ns2:Parent[1]/ns3:Name="Parent 1"
#Output : true


To perform logical operation on two results(return true when 2 nodes have correct values):
((//ns1:Root/ns2:Parent[1]/ns3:Name="Parent 1") and (//ns1:Root/ns2:Parent[1]/ns3:Child="Child 1"))
#Output : true


To access the a node on the same level but with specific attribute (say, i want Childnode name where attribute of Parent node is parent3):
//ns3:Name[@parName='Parent3']/../ns4:Child
#Output : Child 3


To select a node with specific Attribute Value :
//ns1:Root/ns2:Parent[@attribute='Parent 2']/ns3:Name
#Output : Parent 2


To select text value of any node :
//ns1:Root/ns2:Parent[3]/ns3:Name/text()
#Output : Parent 3


To get the attribute value of a selected node :
//ns1:Root/ns2:Parent[3]/ns3:Name/@parName
#Output : Parent3


Match certain regular expression with the response :
matches( //ns2:Parent[1]/ns5:Value/text(), '[1].s*')
#Output : true

About these ads
Comments
  1. YoU.Nike says:

    XPath is simple, Groovy scripts are powerful. as usual, I will select xpath to determine which node(s) I need to check, because it’s explicit to show the structure of xml. if have too much values or discontinuous node that need to be checked. I will be prior consider that using groovy instead of xpath expression, because we can make use of looping to avoid creating overmuch xpath expression alternatively.Of course, most of scenarios we can use xpath or xquery expression, expecially for functions of xpath, like count(), boolean(),exists() and so forth.
    Thanks for your sharing and post here.

    Regards,
    Aaron

  2. Rashmi says:

    I need to get multiple attributes from a response and use them later for a different test step request. I am able to get that for single attribute like session ID using he xpath. Not sure how to retrieve multiple attributes.

    My response looks like and I need to just get the values “runtime.Runtime1″ and “runtime.Runtime2″ values-

    runtime.RunTime1
    runtime.RunTime2

  3. pradnya says:

    Hi ,

    I am working on SOAP ui for manual testing and just now I started using SOAP UI pro for automation. one module I automated with xpath which was quite simple but other module , I am not able to automate with xpath. I am not a programmer so do not have experience on java or groovy script. Is there any other way to automate following requirement?

    CI ID, Type and relation as input

    and output:

    ci id1 , type1, relation (e.g IS parent of)
    —> ci id 2, type 2, relation
    —-ci id 3, type 3 , relation

    ci id 1 is parent of ci id 2 and ci id 2 is parent of ci id 3
    so it’s a hierarchy .

  4. pradnya says:

    Hi Pradeep,

    I have sent an email to you 2 days before… Can you please check it?

  5. pradnya says:

    Hi Pradeep,

    I able to parametrized input using property transfer but not getting idea how to validate output response of soapui pro with database values. Can you please help me for this..
    Its very urgent…Thanks in advance… Note that the output varies depending on input parameter like configurationid 1 has only 3 records but 7 may have 2 or maybe 10 records. so I have loop in through response and actual contents of database .I am not sure what property i have to use for output validation also.

    My approach: I have created datasource with excel sheet where I am parametrizing request. that I am passing to soapui request using property transfer. . now I am getting response from these parameters but my problem is as response is very dynamic and not fixed records so what property I have to use to store response or what approach i have to use for validating response and actual contents.

    1
    Distributed server
    impacts

    Response:

    abc
    2
    OPERATING SYSTEM
    IS PARENT OF

    xyz
    3
    APPLICATION
    IS PARENT OF

    xxx
    4
    DISTRIBUTED SERVER
    IS CHILD OF

    zzz
    5
    DISTRIBUTED SERVER
    IS PARENT OF

  6. pradnya says:

    somehow xml nodes are not displying …so just for clarity request has input as ’1′, ‘Distributed system’, ‘impacts’ which is ‘id’ ‘type’ and ‘relationship’. And response contains related types with ‘id’, ‘type’ ,’relationship’

  7. Adi Mor says:

    what if i want to assert that one of the name is Parent 2?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s