2013-05-23

Function Helper in Jmeter

In this article we are going to see function helper of jmeter. As we know, we use jmeter function to get various information for using as data while testing and we use this format {$__functionName(parameter1, parameter 2, ...)} to use them

So, what is Function Helper? It's simple an UI that helps to understand the functions and provide the variable string directly usable in the jmeter scripts. To start Function Helper, we need to
open jmeter  > Click Options > click Function Helper Dialog.


And we will get this UI.
 

The working is simple,
Step1: Select a function from drop down
Step2: Insert value according the description of the paramter
Step 3: Click Generate and we will get formatted string used in jmeter script.
If we need multiple variables, we can add or delete( depends on which function we use)

Thanks..:)


Test Plan-Thread-sample information using Jmeter Functions

In this following article we are going to see how can we retrieve current Test Plan , Thread and sample information. These are static, so they are rarely used.

-To get name of current running Test Plan
${__TestPlanName}
-Only used in function that calls the full test plan.

-To get number if thread currently running
${__threadNum} 
-It is independent thread wise.
-not useful for test plan

-To know the current sampler name
${__samplerName()}
-Not useful for test plan
-Sometime it can be used with header manager/cookie manager to get value from current sample


Thanks..:)

Calculation with Jmeter functions

In this article we are going to see the function in Jmeter used for Calculations. Calculation like randomizing, incremental, simple adding etc. 

${__counter(FALSE,myVeriable)}
This is a simple counter that increments in +1.
-It uses Integer variable , so limit is 2147482647
-Counter function is fully independent 
-It has 2 parameter, First one(mandatory) take TRUE/FALSE and 2nd one is the name of Variable to store the data.
-TRUE means, it will count user separate among users
-FALSE means, the count user will be globally. All users will be counted centrally.

${__intSum(4,5,myVeriable)}
-It takes minimum 2(up to nth) integer value as variable and returns result in a variable as sum of those.

${__longSum(9999999999,22222222222,myVeriable)}
-It takes minimum 2 (up to nth) Long valueas variable and returns result in a variable as sum of those.

${__UUID}
-It returns a pseudo random type 4 Universally Unique IDentifier (UUID)
Note: I actually have not got much information about that.

${__RandomString(5,shantonu,myVeriable)}
-It returns a random string from given set of characters(second parameter) of length(first parameter which is mandatory ) and stores in a variable(third parameter)

${__Random(1,100,myVariable)}
-It returns a random integer value from given set of range (first parameter for min & second parameter for max) stores in a variable(third parameter)

Thanks..:)

Regular Expression with Jmeter Function

In this article we are going to see how we can use regular expression with a function to retrieve information from response data. We will see full detail of the function.

${__regexFunction((?i).*?abcd.*,$2$,ALL,,,ReturnOnNoMatch,myVariable,InputVariableTextToParse)}

This is the function with Seven Parameters.

First Parameter(mandatory) :The regular expression which will be applied to response data. It grabs all the matches.

Second Parameter(mandatory) : This is the template string that will replace the function at run-time. To refer to a group captured , we need to use the syntax: $[group_number]$. Ie: $1$, or $2$.

Third Parameter : We have to insert our choice of matching in here for the use in case of multiple matches.We can choose in 4 ways.
An integer - Tells JMeter to use that match. '1' for the first found match, '2' for the second, and so on
RAND - Tells JMeter to choose a match at random.
ALL - Tells JMeter to use all matches, and create a template string for each one and then append them all together.
A float number between 0 and 1 - tells JMeter to find the Xth match using the formula: (number_of_matches_found * float_number) rounded to nearest integer

Fourth Parameter : If ALL selected from 3rd, this argument item(character/string) will be added between every value during append

Fifth Parameter :  Default returning value if no match found

Sixth Parameter : A reference name for reusing the values parsed by this function. Stored values are ${myVer}  and ${myVer_g#} where "#" is the group number from the regular expression ("0" can be used to refer to the entire match).

Seventh Parameter :  Input variable name. If specified, then the value of the variable is used as the input instead of using the previous sample result.

-It can also store variable for further use
-As it can use reference variable, we can use previous results as parameters.
-Vastly used for quick data and report processing.

Thanks..:)

String operations with jmeter functions

In this article we are going to see some necessary string operations that can be done using jmeter function.
Jmeter provides a lots of functions for operation over string/data. Some technique are describe below.

1. ${__escapeOroRegexpChars([^"].+?,MyVer)}
-This will escape the ORO Regexp meta characters to java \Q\E Regexp engine. From example, it will be \[\^\"\]\.\+\?

 -It has two parameter, first one(mandatory) the string to escape and the second one is the variable name to save.

2. ${__char(65)
-This function converts from a integer value(parameter) to Unicode character . In the example, it will give A.
- The parameter value is integer(default Decimal, can be Hex, binary, octal)
-This allows us to add characters to a field.
-Helpful for identifying items.
3. ${__unescape(\t\n\r)}
-The __unescape function returns a string result of evaluating a Java-escaped string like as __char() above.
-It has one parameter which is mandatory. This is more like Jaca-escaped string.
-This allows  us to add characters to a field.
-Helpful for identifying items.


4. ${__escapeHtml( "Shantonu" & "Test" )}
-It escapes characters in a string using HTML entities,
-It supports HTML 4.0
-It has one parameter, the string to escape. This function will hide spatial string (our chosen) and shown with HTML formatting. The example will be converted in to
"Shantonu" & "Test"

5. ${__unescapeHtml("Shantonu" & "Test")}
-It is opposite of Escape HTML , so  we will give HTML formatted text and get string formatted data.
From the example we will get "Shantonu" & "Test"


Thanks..:)

2013-05-22

How to run BeanShell/JavaScript/JEXL in Jmeter?

How to access Jmeter Properties ?

As we know previously , we can change jmeter properties by editing properties files stored in /bin directory. I have a full 18 posts for all type of property definitions. In this section , we will see that how can we read and write the properties while jmeter is running.

Jmeter has three build in function doing that. They are as follows
 
1.  ${__property(remote_hosts,myVariable,127.0.0.1)}


-It returns a value of jmeter property(remote_hosts). If the value is not present in the file, default value will be given.
- It has 3 parameter, first one is property name(mandatory) , second one is the variable name where to store, third one the default value of the property.
-If we do not use the 2nd and third one, it will give the value of the property.
Ex- ${__property(user.dir)} - returns user.dir value as string
-If we use 1st and 2nd parameter, the return value will be saved to the variable mentioned in2nd parameter. It is same if whether we use third one or not.
-Default is returned if there is no value.

Note : To get default values, we don't need to define function names or parameters.

2.  ${__P(remote_hosts,127.0.0.1)}

-This is same as previous one but we can not store in a variable. So, except the variable parameter, it has property name(mandatory) & default.
-If no default value give, it assumes 1(use for loop, thread and others)
-It is mainly used in command line to get the property values.

3.  ${__setProperty(remote_hosts,127.0.0.1,True)}

-This is opposite functionality as _property. We can set a property with values.
-It has 3 parameter, first one is property name(mandatory) , second one is the property value to store, third one shows whether we want to return the original value. So third parameter is True/False
-So, if we choose False, it will return a empty string.
 -It is used for changing property time to time after executing threads.

Thanks..:)

How to read file in jmeter?

As we know previously that can read file in jmete by different config elements. In this article we are going to see functions that are used in jmeter used for the same purpose. That means, we can use the function directly reading file without any component.

We can read file in 4 ways. They are

1. String from File :
It is used for reading a string form a text file. This function is same as CSV Data Set config element. It does not supports multiple files.
-Each time function call, it reads the next line
-All thread share the same function, so during execution, different thread will get different line sequentially.
-At the EOF, it will start from begin of the file(a maximum loop can be set)
- If there is an multiple reference in single thread, it will open the file separately(even we use different file)
-It is best practice to use different variable names for each function call.
-Reference variable is resolved(initiated) each time function call
- It returns ERR if there is an error opening file. 
-This is very useful when test required a lot of variable.

 ${__StringFromFile(D:\Shantonu.txt,myVeriable,,)}

-It has 4 parameter, File name(mandatory, full path), Variable name , Start Sequence Number & End sequence number as integer. Sequence number is used for defining reading file pattern.


Note for using Sequence number: 
-If sequence numbers are used, the path name is used as the format string(not file path) for java.text.DecimalFormat 
-# or 0[any digit] is used as for representing as Number
-"." is a formatting character so it is enclosed in single quotes like '.'
-If the path name does not contain any special formatting characters, the current sequence number will be appended to the name, otherwise the number will be inserted according to the formatting instructions. Example :  if we use
${__StringFromFile(Shantonu#'.'txt,myVeriable,1,2)}
It will read Shantonu1.txt and Shantonu2.txt. Like as start with number 1 to 2.

-If the start sequence number is omitted, and the end sequence number is specified, the sequence number is interpreted as a loop count, and the file will be used at most "end" times. In this case the file name is not formatted . Example :  if we use
${__StringFromFile(Shantonu.txt,myVeriable,,2)}
It will read Shantonu.txt twice. That means end sequence 2 is the loop count of reading file.

2. File to String :
It is used to read entire file at a time. It returns ERR if there is an error opening file. 

${__FileToString(D:\Shantonu.txt,ASCII,${userName})}

-It has 3 parameter, file name(mandatory), Encoding (to read file) and variable name(where to put value.
-File name is the full path to file.
-Encode depend on what type of file we use(like ASCII). If blank, it use platform default(os)
-We can use new variable by assigning a new variable(like myVer) or we can use our stored variable and use this(like ${userName}). In case of stored variable, the value will be assigned by this function.
-The file name, encoding and reference name parameters are resolved on each time function called


3. CSV file Reading :
This function is same as CSV Data Set config element. But, it supports multiple file reading(using . *alias)
${__CSVRead(D:\Shantonu.csv,0)}
-It returns a string from a CSV file. From starting, it read full file in to internal array and if it found blank, it assumes as end of file.[So, we can comment at the end] 
-This function has 2 parameter, File path and, column number.
-File parameter (path) is case secretive. Ex- if shantonu.csv and Shantonu.csv are given, it will operate separately creating separate internal array.

-Each thread has a internal pointer to its current row in the file array. When a thread first refers to the file it will be allocated the next free row in the array, so each thread will access a different row from all other threads. [Unless there are more threads than there are rows in the array.]. That means , if we run 5 threads, and if we have 5 lines in CSV file, each thread will get one after another sequentially. Not that, every one will get all 5 lines.

-The function splits the line at every comma by default. If we want to enter columns containing commas, then you will need to change the delimiter to a character by setting the property: csvread.delimiter
-The second parameter takes either column number as integer or "next". Next will go to next line of file.
-We can use this function more than one time in same variable like
${__CSVRead(D:\Shantonu.csv,0,D:\Shantonu.csv,next)}
-As this file is stored in memory, so better not to use this to save memory.

4. Xpath matching with XML file :

It reads XML file and matches XPath each time the function called. After reaching to end of the file, it will start again.

${__XPath(D:\ShantonuTest.XML,.//*[@id='birthday_reminders_link']/div/div/div/span/strong)}

-It has two parameter,  first one is local path of xml file with name and 2nd one is the xpath to match. Both are mandatory fields.
-It returns the next match Xpath(if it found matching)
-It returns Empty string if no matches with warning message to jmeter log 

-It takes a lot of memory as all Node List stays in memory

Thanks...:)

How to write log in jmeter?

In this article we are going to see functions of jmeter used for writing custom Log while running jmeter test. As, default log is stored in /bin/jmeter.log file, this will store in there.

There ate two functions are used for running custom jmeter logs. 

1. ${__log(StringForLogAndReturn,DEBUG,Text thrown,Comments)}
-It has 4 parameter
-it provides a string as output and logs the string
-The first parameter(StringForLogAndReturn) is only mandatory parameter. Text in here will be logged and return as function return. So, we can use this for console output also.
- 2nd parameter for LOG LEVEL, means which level of log will be written. There are
OUT , it will show log to command line(java System.out).
ERR ,it will show log to System.err(systems error log)
DEBUG , Only provides digging info(helpful for post/pre processors)
INFO, this is default log from jmeter
WARN or ERROR , Only warning messages
-3rd parameter is used to throw(like exception) a throw-able to the logger if it is non empty. That means, we can throw an exception along with log by this.
-4th parameter is comments. It is typical user comments.


2. ${__logn(StringForLogAndReturn,DEBUG,Text thrown)}
-It has 3 parameter
-it logs the string but provides an empty string.
- Like as __log, all parameters are same except it does not have comments as parameter.

Both are used in
-When we need to debug steps /requests/processing logs
-When we want to know detail information about what happened during execution
-When we are skipping all listeners and wan to see the errors(or our specific info) only
-When we want extracted information/parameters in the log(we can use variable inside every parameter)

Thanks...:)

How to get current time in Jmeter?

In this article we are going to see functions of jmeter used for getting the current time when jmeter test is running.

Form jmeter function, we can this to get current time

${__time(HMS,shantonuTime)}

It has two parameter . first one is the format and second one is the variable name where the time will be stored. So, either we can get the data and we can also store data with defined variable. Both are optional. This function supports following format

YMD = yyyyMMdd
HMS = HHmmss
YMDHMS = yyyyMMdd-HHmmss
USER1 = whatever is in the Jmeter property time.USER1
USER2 = whatever is in the Jmeter property time.USER2
And, I use shantonuTime as variable name.

Note :  This function returns current time in millisecond. If we use formation, then it converts to corespondent simple date format.

Thanks..:) 

2013-05-20

How to find a webelement or webelements in Robotium?

In this article we will see the functions used for getting a web element from a web application using Robotium while doing testing.

We need to initiate the Solo object. After initiation, we will uses solo to get those.

For Single web element : 

Solo object have a function named  solo.getWebElement(), where we use two parameter to get element. First parameter is for element finder(By object) and 2nd one is the index(nth found), So
-The function returns a web element
-We need to specify By object( the object finder)
-We need to specify index. (Zero based integer index and in here 0 means found 1)

-To select a web element by its class name(Actually it is the type name defines what type of web element that is, like button, text box etc)
solo.getWebElement(By.className("Textbox"),0);

-To select a web element by its css selector
solo.getWebElement(By.cssSelector("CSSAsString"),0);

-To select a web element by its ID
solo.getWebElement(By.id("idAsString"), 0);

-To select a web element by its  Name
solo.getWebElement(By.name("NameAsString"), 0);

 -To select a web element by itsTag Name
solo.getWebElement(By.tagName("tagNameAsString"), 0);

-To select a web element by its text content
solo.getWebElement(By.textContent("contentAsString"), 0);

-To select a web element by its Xpath
solo.getWebElement(By.xpath("xpathAsString"), 0);

For Multiple web element : 

There are two function,

1. solo.getCurrentWebElements(By.[--by xpath or above items--])
This is just multiple version of previous single web element finding

2. solo.getCurrentWebElements(); 

For both cases they provide an array list of WebElements displayed in the active WebView.

Thanks..:)

How to press menu items in Robotium?

In this article we are going to see how to check different items from menu. Basically, the items that comes when we press menu button.

We need to initiate the Solo object. After initiation, we will uses solo to get those.

-To click an items with a text present in the menu.
solo.clickOnMenuItem("ItemName");


-To click an items with a text present in the menu or the sub-menu. If it is true in second parameter, it will check all sub-menu items.
solo.clickOnMenuItem("ItemName", false);


Note: This text will be used as regular expression(it will match with all items of the menu)

-To click a menu item based on it's index(0 Based, integer).
solo.pressMenuItem(0);


-To click a menu item based on it's index(0 Based, integer). In here, we need to mention the number of items present in each row(in integer).
solo.pressMenuItem(0, 3);


Note : In here, Index are sequentially made. Ex- if there are 2 rows and 4 items in each column, index 0=1st row, 1st item and 5=2nd row 2nd item

-To click a Drop Down Menu item based on it's deop down menu index and item index( both are 0 Based integer). First parameter is the index of drop down menu, 2nd one is the item index.
solo.pressSpinnerItem(2, 0);
-I here, Negative number in 2nd parameter(as item index) will move clicking to Up, positive number will be to Down.


..Thanks...:)