简体   繁体   中英

JSF ViewState errors using JMeter

Using JMeter I need to extract the JSF view state value. I am able to extract the value.

Below is the viewstate form response:

   <input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="j_id4" />

My Regular expression extractor is as below:

<input\s+type="hidden"\s+name="javax\.faces\.ViewState"\s+id="javax\.faces\.ViewState"\s+value="([^"]+)".*/>

This works fine. I get j_id4 value correctly and places.

But my big problem is for every API call the value changes. First API call the view state value is j_id4 , but the next API call view state will be j_id5

How do I handle this?

  1. Parsing HTML with Regular Expressions is not the best idea , I would recommend going for CSS/JQuery Extractor instead, the relevant configuration would be as simple as:

    • Reference Name: anything meaningful, ie viewstate
    • CSS/JQuery expression: input[name=javax.faces.ViewState]
    • Attribute: value
  2. If your test assumes 2 (or more) HTTP Request samplers you will need to either apply a CSS/JQuery Extractor to each of them as ViewState will change on each call so you will need to extract it each time. Alternatively you can put CSS/JQuery Extractor at the same level as your HTTP Request samplers, JMeter's Post-Processors are obeying Scoping Rules so single Extractor will be applied to all the Samplers it its scope. Something like:

    JMeter CSS JQuery提取器

You can add 2 regular expression extractors, one will extract j_id , the expression will be as below:

  • Regular expression: value="(.*?)\\d"

The second will extract the number 4 , the expression will be as below:

  • Regular expression: value=".+(\\d)"

Now add a beanshell post processor for each of your API's with the following code:

String j_id = vars.get("j_id");// j_id is the reference name of the first regular expression extractor
int num = Integer.parseInt(vars.get("num"));// num is the reference name for the second regular expression extractor
num = num + 1;
vars.put("num",String.valueOf(num));
vars.put("JSF", j_id + num);

For the first API you can use the same regular expression you were using, after adding the above beanshell post processor to all of the API's that you need to extract the JSF view state value from, use ${JSf} which will hold the value j_id5 for the second API and j_id6 for the third API and so on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM