Wednesday, March 2, 2011

JMeter handling dynamic ViewState parameter in Seam framework

Testing applications built upon Seam Java framework is possible with JMeter like any other web applications, though there are some pitfalls.

One of them is ViewState parameter. This parameter is dynamic and it means that we cannot just leave it hardcoded in our JMeter script because it will be broken the next time you try to execute it. Why this happens? Let's dig a little deeper. Though for pure black-box testing we don't need to know how Seam framework works, right? Ok, then we only need to know how it affects our tests.

As testers we need to know one thing: if we pass ViewState parameter in the POST request, it should be the same as returned by the previous GET request.
For example you have two subsequent requests:
  1. GET http://x.x.x.x:8080/some_page.seam
  2. POST http://x.x.x.x:8080/some_page.seam
The first request opens some page and the second one performs some action in already opened page (for instance - expands some tab, tree, whatever).

If you analyze the response data with listener you may notice ViewState parameter that is returned as response to the first request where we open the page:
...
<input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="j_id5" />
...

To work it properly you must pass javax.faces.ViewState parameter in the second request and the value must be the same: j_id5.

But remember? This is a dynamic parameter, so we cannot just pass j_id5 value always, it will change and the test will break. We need to parse it from the first request, store in a variable and then use it in the second request instead of hardcoded value. Let's see how we can do it.

At first we need to create JMeter user variable, let's call it VIEWSTATE.
Add -> Config Element -> User Defined Variables
Then we need to add Regular Expression Extractor as a child to the first request.
Add -> Post Processors -> Regular Expression Extractor
Regular expression is: id=\"javax.faces.ViewState\" value=\"(.+?)\"
It will extract the value of javax.faces.ViewState parameter from the response data and assign it to VIEWSTATE variable.

We use this variable in the second request:
Now your JMeter tests can successfully cover applications built upon Seam framework.