Dynamic calculation of request parameters
In this section the term "parameters" is used to describe the parameters specified to the right of the ? sign in the HTTP request string. The functions are used to define the values of HTTP request parameters and provide the ability to dynamically set the required values for the Param=Value pairs. If a function is used, the declaration of a parameter looks like this: Param=$function. The functions substitute the resulting values, with one exception being the $rxname function (see below).
Now let us examine the available functions and the resulting values that can be generated.
$NRand
Syntax:
ParamName=$NRand({from},{to}[,{prefix}])
A random integer from the [from,to] range; precede by a {prefix} (if one specified).
Example:
The test script is:
[General]
clients=1
requests=3
server=myserver
uri=/view
[Request1]
param=$NRand(1,20,ID)
If you run this script , three HTTP requests will be sent to the server:
http://myserver/view?param=ID5
http://myserver/view?param=ID12
http://myserver/view?param=ID2
The part of the param value after ID is generated randomly. If there is nothing specified instead of ID in the script, i.e. the script is as follows:
[General]
clients=1
requests=3
server=myserver
uri=/view
[Request1]
param=$NRand(1,20)
then three different requests will be sent to the server upon the script run:
http://myserver/view?param=5
http://myserver/view?param=12
http://myserver/view?param=2
$ARand
Syntax:
ParamName=$ARand({from},{to})
Random alphanumeric sequence with length in the range [from, to].
Example:
Launching the test script below:
[General]
clients=1
requests=3
server=myserver
uri=/view
.. ..
[Request1]
param=$ARand(1,20)
results in issuing three HTTP requests:
http://myserver/view?param=g
http://myserver/view?param=fndy5lgs5pck
http://myserver/view?param=xfglfgjl4asd098ljk6c
Here all values of param are randomly generated.
$LRand
Syntax:
ParamName=$LRand(value1[,{value2}, ])
Random value from the list of values {value}.
Example:
Launching this test script:
[General]
clients=1
requests=3
server=myserver
uri=/view
.. ..
[Request1]
param=$LRand(test4,bobby,alpha)
Results in sending three HTTP requests to the server:
http://myserver/view?param=alpha
http://myserver/view?param=test4
http://myserver/view?param=bobby
$List
Syntax:
ParamName=$List(value1[,{value2}, ])
Similar to $LRand, but the elements are used in the order they are listed (not randomly). The 1st request uses the 1st value from the list, the 2nd one uses the second value and so on. After reaching the last value the list is looped, starting again from the first listed value.
$Seq
Syntax:
ParamName=$Seq({prefix}[,{startvalue}[,{clientshift}]])
Augmenting sequence. Increases by 1 with each run of [TestSeq]. The initial value is {startvalue} (if specified, otherwize 0). If the {clientshift} is specified, then the starting value for different virtual clients will be shifted using this value. You can also specify several leading zeroes for the startvalue, so that the numeric component always keeps the same length.
Example 1:
The following test script:
[General]
clients=1
requests=3
server=myserver
uri=/view
.. ..
[Request1]
param=$Seq(test,2)
Results in sending three HTTP requests to the server:
http://myserver/view?param=test2
http://myserver/view?param=test3
http://myserver/view?param=test4
Example 2:
If the following test script is launched:
[General]
clients=3
requests=3
server=myserver
uri=/view
.. ..
[Request1]
param=$Seq(test,2,5)
the virtual client #1 will send three HTTP requests to the server:
http://myserver/view?param=test2
http://myserver/view?param=test3
http://myserver/view?param=test4
virtual client #2 will send three different HTTP requests:
http://myserver/view?param=test7
http://myserver/view?param=test8
http://myserver/view?param=test9
and virtual client #3, again, three different HTTP requests:
http://myserver/view?param=test12
http://myserver/view?param=test13
http://myserver/view?param=test14
$UrlParam
Syntax:
ParamName=$UrlParam[({name})]
The response to the previous request will be searched for a GET request containing a parameter with name {name}. The value of this parameter will be substituted. If {name} is not explicitly set, then the name of the target parameter (ParamName) will be used - i.e., Myparam=$UrlParam is completely equivalent to Myparam=$UrlParam(Myparam).
Example 1:
The HTML code of the response contains:
<A href=http://myserver/view?sessionid=fchhab02df¶m=test111</a>
Use of Myparam=$UrlParam(SessionID) in the next request will result in the actual value of Myparam=FCHHAB02DF.
This function is very useful when you need to use the parameter values generated by the server on-the-fly. This relates, e.g., to various Session variables.Example 2:
You need to automate a simple scenario of navigating through a certain site using the respective HTTP requests:
1.User registration:
http://myserver/register?User=Alex&Pass=qwerty (a POST request)
2.After registration the user is redirected to the main category listing the links to all resources of the site. At this stage the SessionID session variable is generated. The user selected the Main category by clicking the respective link:
http://myserver/view?SessionID=FCHHAB02DF&Category=main
3.The user selects a link to a particular resource:
http://myserver/view?SessionID=FCHHAB02DF&Category=main&Item=67
If we record this sequence of HTTP request, it would result in the following test script:
[General]
clients=1
requests=1
server=myserver
[Req1]
$Uri=/register
$Method=POST
User=Alex
Pass=qwerty
[Req2]
$Uri=/view
$Method=GET
SessionID=FCHHAB02DF
Category=main
[Req3]
$Uri=/view
$Method=GET
SessionID=FCHHAB02DF
Category=main
Item=67
But we will not be able to use this script - after sending Req2 an error will occur. The reason is that we try to use the old value of SessionID. Consequently, we need that each time after user registration the test script operates with a correct (currently generated) value of SessionID. We can modify the script to achieve this:
[General]
clients=1
requests=1
server=myserver
[Req1]
$Uri=/register
$Method=POST
User=Alex
Pass=qwerty
[Req2]
$Uri=/view
$Method=GET
SessionID=$UrlParam(SessionID)
Category=main
[Req3]
$Uri=/view
$Method=GET
SessionID=$UrlParam(SessionID)
Category=main
Item=67
and thus solve the problems related to SessionID. Now before sending the Req2 request the $UrlParam function will parse the response (the returned HTML code) to request Req1, find the first link containing the required SessionID and substitute the actual value of SessionID into Req2 (in the Example this value is FCHHAB02DF).
$Hidden
Syntax:
ParamName=$Hidden[({name})]
The function searches for an element containing <.... name={name} value={value}> using the specified {name}. The found {value} will be used. If {name} is not specified explicitly, the name of the target parameter (ParamName) will be used. The syntax of this type is used typically to define input elements in HTML forms. The advantage of this function is that it allows to work e.g. with hidden form elements with dynamically generated values (again the Session variables).
Example:
The response to the previous HTTP request contains a form with a set of hidden elements:
<form action="/cgi-bin/select.pl" method="get">
<input type="text" name="index" size="25" maxlength="25">
<input type="submit" value="Search">
<input type="hidden" name="restrict1" value="yes">
<input type="hidden" name="restrict3" value="no">
....
....
</form>
and the values of these elements should be used in the subsequent HTTP request. Let us compose a test script for solving this problem:
[General]
clients=1
requests=1
server=myserver
[Req1]
$Uri=/
[Req2]
$Uri=/cgi-bin/select.pl
$Method=GET
index=AnyText
restrict1=$Hidden
restrict3=$Hidden
In the test script above, before sending the Req2 request the $Hidden function will parse the HTML code of response to request Req1. For the restrict1 parameter the function will find the fragment < .name="restrict1" value= >, for restrict3 - respectively, < .name="restrict3" value= >. Respective values will be returned and used as values of parameters restrict1 and restrict3 in Req2.
$RegExp
Syntax:
ParamName=$RegExp(/{regexp}/,{replacestring})
The response to the previous request will be scanned for a match to the specified regular expression {regexp}, and the resulting function value will be built according to the search results and the {replacestring} syntax. The syntax is similar to Perl syntax (see description in Syntax Reference ->Regular Expressions).
Example:
You need to automate the submitting of a form with comboboxes. Only the description of combobox values are static (in this case - minimal, middle and maximum), but the values are generated by the server and not initially known. The HTML code of such a form may look like this:
<form action=send method=post>
.....
.....
<select name="weight">
<option value="20">minimal</option>
<option value="40">middle</option>
<option value="60">maximum</option>
</select>
</form>
Let us compose a test script for solving this problem:
[General]
clients=1
requests=1
server=myserver
[Req1]
$Uri=/
[Req2]
$Uri=/send
$Method=POST
weight=$RegExp(/value=\"(\d)\">minimal</,$1)
Before sending the Req2 request $RegExp parses the HTML code of response to the Req1 request. For the weight parameter it will find the fragment <option value="20">minimal</option>, which matches the regular expression. The value 20 will be extracted and returned by the $RegExp function.
$RxName
Syntax:
$RxName(/{regexp}/,{replacestring},{value})
Modifies the parameter name according to the regular expression. No matter which parameter name was initially used in the script, it will be replaced by the result built according to {replacestring}. The rules are the same as for $regexp(). The {value} can be any correct value used in the request parameters, e.g. the ones listed above.
Example:
You need to submit a form for which the names of input elements are dynamically generated by the server, and thus initially unknown. The form is like follows:
<form action="send">
<input type="checkbox" name="Option124">Food
<input type="checkbox" name="Option78">Juice
<input type="checkbox" name="Option471">Biscuit
</form>
The test script for performing this task looks as follows:
[General]
clients=1
requests=1
server=myserver
[Req1]
$Uri=/
[Req2]
$Uri=/send
AnyText=$rxname(/name="(Option\d+)">Juice/i,$1),on
Before sending the Req2 request the $RxName function parses the HTML code returned in response to Req1 and finds the code fragment name="Option78">Juice matching the regular expression. The value Option78 is extracted and used as the parameter name, the value of this parameter is then set to on.
$Var
Syntax:
ParamName=$Var({name}[{,default}])
Uses a variable set earlier by the $exportvar function. If the variable with name {name} is not defined, the {default} value will be used instead (if present).
Example:
Let us examine once again the example for the $RegExp function and solve the problem in a different manner. You need to automate the submitting of a form with comboboxes. Only the description of combobox values are static (in this case - minimal, middle and maximum), but the values are generated by the server and not initially known. The HTML of such a form may look like this:
<form action=send method=post>
.....
.....
<select name="weight">
<option value="20">minimal</option>
<option value="40">middle</option>
<option value="60">maximum</option>
</select>
</form>
Let us compose a test script for solving this problem:
[General]
clients=1
requests=1
server=myserver
[Req1]
$Uri=/
$ExportVar=VarName,/value=\"(\d)\">minimal</,$1
[Req2]
$Uri=/send
$Method=POST
weight=$Var(VarName)
After sending the Req1 request the $ExportVar function parses the HTML code returned in response to Req1 and finds the code fragment <option value="20">minimal</option> which matches the regular expression. The value 20 is extracted and assigned to the VarName variable. Before sending the Req2 request the $Var function returns the value of the VarName variable (defined earlier), and assigns this value to the weight parameter.
All functions parsing the HTML code of the responses have one common limitation - they can operate only with the code of the previous request. Defining and using external variables with $ExportVar and $Var functions allows to overcome this limitation and transfer data between non-subsequent HTTP requests.
$File
Syntax:
$File({path filename}[{,mimetype}])
File upload. Multipart/form-data will be used. If {mimetype} is not explicitly specified, application/octet-stream will be used. Used to upload a file to the server, e.g. when sending e-mail messages with attachments via the Web interface of the mailing system.
Example:
Usually the code for file upload looks as follows:
<form method="post" action="upload" enctype="multipart/form-data">
<input type="file" name="filename">
</form>
If you need to upload the JPEG image image.jpg located in the C:\Graphics\Jpg\ folder, the test script will look as the one below:
[General]
clients=1
requests=1
server=myserver
[Req1]
$Uri=/upload
$Method=POST
filename=$File(C:\Graphics\Jpg\image.jpg,image/pjpeg)
$Clid
Syntax:
ParamName=$Clid({prefix})
{prefix} plus the ordinal number of the virtual client. Can be used to create values which should be different for different clients, but the same for a single client.
$GVar
Syntax:
Paramname=$gvar(OptionName, DefaultValue)
Defines the parameter value when running as a Win32 console application (see Console mode).
OptionName the name of command line option,
DefaultValue default value of $gvar function.Upon the launch of a test script woth a $gvar function the latter substitutes the value of the respective option from the command line (or the default value, if the requested OptionName was not found in the command line). Can be used for defining any parameters (or their components). CANNOT be used to define a parameter name (i.e., cannot stay to the left of the = sign).
Examples (correct use):
Server=$gvar(server, myserver)
$Server=$gvar(server, myserver)
param=$gvar(param, value1)
param=abc$gvar(param, value1)Example (incorrect use):
$gvar(paramname, value)=value9