The regular expression in LoadRunner is one of the easiest options to implement the correlation logic for handling dynamic values in the script. web_reg_save_param_regex function helps to capture the dynamic values in the response using a regular expression token. The main advantage of web_reg_save_param_regexp over web_reg_save_param is that you have the option to escape the content (string) to capture the required value. See the example given in this post later. If you have expertise in writing the regular expression then web_reg_save_param_regexp is the ultimate option for you.
Why the function name is like this?
web_reg_save_param_regexp function is used for web protocol (denoted by ‘web’). This function registers a request (denoted by ‘reg’) to find out the dynamic value based on the regular expression (denoted by ‘regexp’). After registering the request, the next task is to save the dynamic value in a parameter (denoted by ‘save_param’ words). Hence it is named as ‘web_reg_save_param_regexp’.
Follow the navigation to open the web_reg_save_param_regexp input box:
- Press ‘Ctrl+Alt+B’ or View -> Steps Toolbox
- Type ‘web_reg_save_param_regexp’ in the search box. VuGen will show you the matched content.
- Double-click on the ‘web_reg_save_param_regexp’ function. The window will appear with the following inputs:
Attributes of ‘web_reg_save_param_regexp’:
a. Parameter Name: The name of the correlation parameter that saves the dynamic value. This is a mandatory field.
b. RegExp: This is a mandatory attribute. In this attribute, you have to write a regular expression to capture the dynamic data. Refer to the post to learn how to write regular expressions.
c. Group: This is an optional attribute. Group attribute provides the option to capture multiple dynamic values using the same regular expression. If you pass 0 (zero) then it saves the entire expression along with the specified text. The maximum value is 10. Example: If you have specified the regular expression as given below and you need to capture SurName, then Group will be 2
"RegExp=Name=(.*?) and SurName=(.*?)",
d. DFEs: This is an optional attribute. It is a comma-separated list of Data Formats Extensions that are to be used before the required search operation is to be performed.
e. Ordinal (ORD): This is an optional attribute. It is used to capture dynamic values on a specific occurrence. If you specify the value of Ordinal (ORD) =ALL then all the occurrences of the matched boundary are saved in an array. Each element of the array is represented by the ParamName_index. In the following example, the parameter name is c_custID:
web_reg_save_param_regexp(
"ParamName=c_custID",
"RegExp=customerID=(.*?);",
"NotFound=warning",
"Group=2",
"Ordinal=ALL",
LAST);
The first match is saved as c_custID_1, the second match is saved as c_custID_2, and so forth. You can retrieve the total number of matches by using the following parameter ParamName_count. For example, to retrieve the total number of matches saved to the parameter array you can use the below statement:
customerCount=atoi(lr_eval_string("{c_custID_count}"));
h. Warn if text was not found (Default is Error) (NotFound): This is an optional attribute and has ‘Error’ as a default value. This option handles the situation when the defined boundary does not match and generates an empty string. It has the following input options:
- ERROR: This is a default value. When a boundary is not found then LoadRunner highlight it as an error in the log.
- WARNING: LoadRunner does not issue an error. If the boundary is not found, it sets the parameter count to 0 and continues executing the script. The ‘warning’ option is good when you want to see whether the string was found or not, but do not want the script to fail.
Note: If ‘Continue on Error’ option is enabled for the script then even when NOTFOUND is set to ‘ERROR’; the script continues when the boundary is not found, but an error message is written to the Extended log file.
i. Scope: This attribute comes under the ‘Filters (SEARCH_FILTERS)’ option. This is also an optional attribute with the default value “All”. It specifies the scope of the search for dynamic value within the response. The input options are:
- All: The scope of the search is body, headers, and resources
- Headers: It indicates to search only in the headers
- Body: The scope of the search is only body, not headers
- Cookies: The scope of the search is only cookies.
j. Header Name (HeaderNames): To enable this attribute you need to select the ‘Scope’ type as Headers. Here, you can provide a comma-separated list of HTTP response header names then LoadRunner will search the dynamic values only in the specified headers. This is an optional attribute.
k. RequestURL: The dynamic values will be searched only in the responses of specified requests. The URL can contain the * wildcard. It is specially used to correlate the value coming in the response of flex_amf_call.
l. Content Type (ContentType): LoadRunner searches the dynamic value in the responses with the specified ContentType header. The ContentType can contain the * wildcard. This is an optional attribute.
m. Frame ID (RelFrameID): This is an optional attribute. The hierarchy level of the HTML page relative to the requested URL. The possible values are ‘All’ or a number.
Note: GUI level scripts do not support RelFrameID.
n. Ignore Redirection (IgnoreRedirections): This is an optional attribute with the default value “No”. The inputs are:
- Check (Yes): By setting the ‘IgnoreRedirections=Yes’, it instructs LoadRunner not to search the dynamic value in the response of the redirected request. You can identify the redirected request by its response code HTTP 3XX.
- Uncheck (No): LoadRunner even searches for the existence of dynamic value in the response of the redirected request.
Example: ‘web_reg_save_param_ex’ with attributes
web_reg_save_param_regexp(
"ParamName=c_custInfo",
"RegExp=Name=(.*?) and SurName=(.*?)",
"NotFound=warning",
"Group=2",
"Ordinal=ALL",
SEARCH_FILTERS,
"Scope=BODY",
"IgnoreRedirections=Yes",
"RelFrameID=2",
"RequestUrl=http://myperformancetetsingdemosite.com",
LAST);
How to use the web_reg_save_param_regexp function in the script?
- Identify the dynamic value in the script (How?)
- Locate the cursor before the request which has a dynamic value in the response.
- Right click -> Insert -> New Step -> Steps Toolbox -> (Search for keyword) web_reg_save_param_regexp -> (Double Click on) web_reg_save_param_regexp -> ‘web_reg_save_regexp – Save Data to a Parameter’ window will open -> Provide the required input and press Ok.
OR
If you know the correct syntax and attributes of the web_reg_save_param_regexp function then you can simply type in the script.
Important Point: LoadRunner’s correlation functions are always written/placed before the request whose response contains the dynamic value.
In addition, there are four more correlation functions in the LoadRunner. Refer to the link to get more details:
- web_reg_save_param
- web_reg_save_param_ex
- web_reg_save_param_xpath
- web_reg_save_param_json
- web_reg_save_attrib
Frequently used regular expression tokens:
- (.*?) – It matches any string including a new line
- (.+?) – It matches the strings including letters, digits and space (exclude new line)
- ([a-zA-Z]+) – It matches any characters between a-z or A-Z
- ([\d]) OR ([0-9]+) – It matches any decimal digit
Refer to the link to learn more about regular expression tokens and how to write them.
How to escape the content in the response and capture the correct value?
Example: A request has the below response including class, type, category, foodcategory and name.
class=livingthing,type=mammal,catagory=animal,foodcatagory=carnivores,name=lion;
class=livingthing,type=mammal,catagory=animal,foodcatagory=carnivores,name=tiger;
class=livingthing,type=mammal,catagory=animal,foodcatagory=carnivores,name=leopard;
class=livingthing,type=mammal,catagory=animal,foodcatagory=carnivores,name=dog;
class=livingthing,type=reptile,catagory=animal,foodcatagory=carnivores,name=snake;
class=livingthing,type=reptile,catagory=animal,foodcatagory=carnivores,name=lizards;
class=livingthing,type=mammal,catagory=animal,foodcatagory=carnivores,name=cat;
If you want to capture the names of all the mammals then probably you will write like this:
class=livingthing,type=mammal,catagory=animal,foodcatagory=carnivores,name=([a-z]+);
By using an escape token you can shorten the token string like this:
class=livingthing,type=mammal.*?name=([a-z]+);
Try at your end and see the magic!
How to write regular expression for below scenario and take input data individually and total count under specific category in vugen
“License1″[
{
“ab”: “8888”
},
{
“ab”: “7777”
“License2”: [
{
“ab”: “111111”
},
{
“ab”: “222222”
Hi Raj,
What do you mean by specific category? Are you referring ‘License1’ as a category?
Yes.
For example it should work like below:
for license1 should store the total count as 2 and should iterate n number of counts. here its 2 times
for license2 should store the total count as 2 and should iterate n number of counts. here its 2 times
Hope i am not confusing.
Just let me know how to write regular expression and take input data individually and total count under license1 and license2 separately.
Yes
If License1 is constant then you can use the below RegEx:
“License1”.\W+“ab”: “([0-9]+)”\W+“ab”: “([0-9]+)”
In case you want to capture all the “ab” value irrespective of License then use the below RegEx:
“License\d”.\W+“ab”: “([0-9]+)”\W+“ab”: “([0-9]+)”
Hi ,
Does the group save the both dynamic values using one expression .?
Example :
Server response
draw”:2,”recordsTotal”:17820
My Regular expression
draw\”:(.*?),\”recordsTotal\”:(.*?),”
I have given group as 0 but its capturing entire expression . ie draw”:2,”recordsTotal”:17820
instead of giving “2 ” and “17820”
can you help me to understand how I can capture only “2 ” and “17820” using one expression.
Thanks
Give group as 2. Since you are giving 0, hence it is capturing full statement.
But if we place 2, it will capture 17820 only. right? and now 2.
Means,
Group=0 stands for complete string(including search criteria).
Group=1 stands for first dynamic value(from regular expression) in search string
Group=2 stands for first dynamic value(from regular expression) in search string