Favourite Quote

Every artist was first an amateur. - Ralph Waldo Emerson The more difficulties one has to encounter, within and without, the more significant and the higher in inspiration his life will be. - Horace Bushnell

Tuesday, February 21, 2012

How to add a Xpath Assertion in JMeter

As i've mentioned in my earlier posts about how assertions can be of help in validating the right response code

Xpath Assertion eases life when we have to validate if an object exists.

For ex:

Let's again take a shopping cart example.

1.Search for an item
2.Add selected item to shopping cart > now we want to verify if the Add cart button is available on the page.

Therefore we need to add a xpath assertion.

Below screen shot should give a clear idea & how an expression is built.



Let's have a closer look at the Xpath Assertion.

Add a xpath Assertion :

1. Right Click on the HTTP Sampler > Assertions > Xpath Assertions.

Once you'ev added the Xpath Assertion, now we have to add appropriate values so that we can assert object or value.

1. Give a name to Xpath Assertion just like i have given not necessarily same.

2. XML Parsing Options - Check the Checkbox's against "Use Tidy(tolerant parser) & Quiet".

3. Xpath Assertion - contract the xpath & pass the xpath string within the Xpath Assertion.

For ex : I am validating to see if the button with id "bestAvailableTickets_LookForTicketsButton"" exists on that page.

I have left the "True if nothing matches" as unchecked as i want it to match.
automationwithselenium.blogspot.com-Google pagerank and Worth

How to post parameters with request in - JMeter

We most often have to post our request with dynamic parameters or even static parameters,It would be handy if we did find a way to post our parameters.

For ex:

Let's say that :

1.I have a shopping cart as my application

2.I am seaching for a item

3.Adding it to my shopping basket

Assuming that we figured out what are the parameters that we need to pass to the request.

We can use FireBug as a tool to identify what are parameters that are sent with a request quickly (or)
We can use LiveHTTP Headers to do the same functionality of identifying what are variables that are dynamic,static,values/names of the object.



So let's have a closer look at the image above.

1. Add a sampler viz., the HTTP Request give a name to the request just as i have given.

2. Method is "POST" we have different webmethods for each request we do.

3. POST method uses web method which means that you are posting a request to the
server & the response will be sent inturn to the server.

4. To avoid the hiccups we face in future let's parametrise the dynamic content by passing it as a variable either as user parameter or from a CSV file.

5. Click on the Follow Redirects & Use Keepalive so that we can have a look at if there are any redirests that occur.

8. Now let's have a look at the parameters which we have to pass through our request.

9. Make sure you know what the application code rather id for an object is called as,you can find out this by using Firebug or Live HTTP Headers both the name & value.

10.We send the Name Value pair as request so name is the id/name that uniquely identifies an object on the application.

11.It sometimes rather often happens that we don't want to change the values frequently in those scenarios you can read values from a CSV file.
automationwithselenium.blogspot.com-Google pagerank and Worth

Adding Response Assertions


It is often important that we validate the response we receive for every request we had sent to the server.

We can achieve this by adding a response assertion to the sampler.

There are various types of assertions you will have to identify what serves your purpose & work accordingly.

Add a response assertion by right clicking on the sampler > Assertions > Response Assertion - Have a look at the fig1




Now that we have added a response assertion let's have a closer look at the settings & values we are looking for identifying/validating our response.

1.Add a name to teh response assertion so that you know when it fails.

2.Select the radio button Main Sample only - this looks for text you are looking for in the main sampler.

3.Let's select "Text Response" as we are going to validate the same.

4.Pattern of matching - Helps in identifying what operation rules you want to apply a wider search pattern would be "Contains" so that pattern matching ignores the pre & post text & just look for "Pattern String".

5.In Patterns to Test - Click the Add button just below the patterns to test "enter the text you want to validate".


It is that easy ... Adding a response assertion. automationwithselenium.blogspot.com-Google pagerank and Worth