Many of the blog readers have asked this question in past “How to create a dynamic XPath assertion?”. And almost everybody had it’s own explanation about the word Dynamic [with Xpath]. So here i am sharing my understanding on the same :

For me a ‘dynamic assertion’ is one which automatically changes the expected result based on my Request input. For instance (referring to Weather Forecast webservice), if i am requesting the weather data for “Jodhpur, India” then my response should contain “Jodhpur” in the <City Name> tag. Similarly, if i request “Bangalore, India” then response will contain “Bangalore” in the <City Name> tag.

Let’s elaborate the given example & find out how a Regular xPath assertion is added. And then we will modify our existing assertion into a Dynamic assertion.

<!-- My request looks like following -->


<!-- Response for the above request -->

<Detail>Sunny Weather</Detail>

So, now we have both the request & response available with us. How about you try putting some xPath assertion over the response? Something like the cityName=”Jodhpur” and DegreeUnit=”Celsius” etc. I guess that was easy? Anyways, here is my solution :

<!-- xpath expression to fetch the value of City Name from the response -->

<!-- expected result value should be equal to -->

Now change the input request data (i.e., CityName) to Bangalore. And hit the same request again. See it is failing. Did you observed this? This failure of the test request occurred due to the failure of earlier added xPath assertion. So to test the same request (with different data) either i have to create multiple copies of the request and modify the assertion accordingly. Or modify the assertion everytime whenever i change the request data. Now this sounds very painful. Here is the approach to make life simpler.

Put the following code in the expected result :

<!-- expected result value should be equal to -->
Now this is simpler and more useful. It is just using the regular “Property Expansion” code and that would be evaluated during execution of assertion method call. No need to change the assertion content over and over again for a change in request content data.
BTW, the same approach can be used in the “Contains”, “Not Contains” and few other assertion also. Or maybe you can store your result (expected data) in a property test step and match them using this approach.
Till next blog, happy reading and happy sharing 🙂
  1. Ajay says:

    Hi Pradeep,

    Once again you hit the pain area. Good job.

    I am interested to know how it can work when city name is not part of you response and you want to assert on some other value which is not part of your request.

    I read request data (for example employee number) from the database at runtime and use in the request. The response contains lets say FirstName, the value in FirstName is unpredictable since employee number is picked up at runtime. In such a case, I simply test if FirstName is null or not in the response. If it is not, test case is passed.

    Thanks again.


    • Hi Ajay,

      In that case, need to put in some additional stuff.
      A small script to fetch the corresponding data value from the database (say a JDBC test step with a query taking parameter input as ’employee id’ which returns employee name in response) and update it that value into a custom property.
      Now the property expansion of this custom property can be used. Hope this will help.

      {Pradeep Bishnoi}

  2. shilparaju says:

    let me know if my understanding is correct :

    we get value from Database can be passed to property and we can use the same property name in assertion with xpath expression and compare the results

  3. Ajay says:

    Hi Pradeep

    I am getting what you are saying.

    The actual services are not that straightforward as we took a sample to explain the problem. They always query number of tables, join the data and produce resultant record. It is not impossible to fetch the record using such a complex queries and populate custom property or property. However the choice remains at how far we want to go and at what cost.

    Thanks for the reply.

  4. Sabari says:

    Hi Pradeep,

    Your blog rocks…. Thanks for all the stuff shared…. I want to know we can compare the JSON Request value with the XML response.

    • Hi,

      I wasn’t able to get a good grip over your question. Comparing JSON request value with XML response?
      If response is XML, then you can fetch the specific node value using groovy script Or xPath and store it in some property variable.
      Then you can easily compare 2 property values using groovy script.

      Hope this will help.

      {Pradeep Bishnoi}

      • Sabari says:

        Hey,… Thats indeed useful,,,, can you provide me a sample. I am new to this Tool and indeed in need of automating rest services, I would like to have more updates how this can be done.

  5. Sonal says:


    i am a newbie. have a very simple query. how can i validate a field in the response using the conatins assertion? i wish to use regular expression.

    ex if i want to validate a field say “ChangeMetricWeightUnitResult” in the below response


    so that the field only contains digits and i should be able to check the number of digits in the field.

  6. SOftwaretester says:

    Hi Sonal,

    How about an assertion to check if \d..?

  7. Thank You says:

    how to extract value of a parameter from json body in request??

  8. Joseph says:

    Hi guys…
    I’m trying to divide a number for assertion purpose. How can i do it? actually I want to take the Xpath value and then divide it by 2 in my assertions.
    Can anyone help me on that please

  9. Anurag says:

    Pradeep i need to put an assertion check on the count that count be greater then 1.Test failes if it fails to do that.can u pls let me know how to do that?

  10. Jitendra says:

    Hi Pradeep Thanks for the nice article. I have a query though:

    Is there any way to apply assertion on the Varying responses of the same request? Project structure is like this:

    .Test Request


    …Test Request

    ….Data Loop

    I am storing a specific node’s value from 1st request in the data source and iterating on those values in the 2nd request. Each time response is different. Now I wanted to add some assertion to verify the response of the second request. Pl. let me know if need any other information.


  11. Shay Marad says:

    If i want to validate the Temperature is less than 45, how can i do it?

  12. Ravi says:

    How to use XPathin HP service test ,Can anyone plzz reply

  13. Srikanth says:

    This is really helpful. You saved my day. Thanks.

  14. hemantvarhekar says:

    please share the WSDL with mr

Leave a Reply

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

You are commenting using your 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