Pages

Friday 28 April 2017

Cheat Sheet for Regular Expression in Jmeter

Apache Jmeter, Regular Expression, RegEx
In Jmeter, Regular Expression play very important role. It is used to identify dynamic values in response which is coming from server. Regular expression captures dynamic value coming from server matching left and right boundaries. 

There are multiple types of regular expression we need to apply on day to day basis in our scripting. This blog will cover all types of regular expression with Example.


1. (\d+) and (\d) and [0-9]+


 It is used to capture decimal dynamic values coming from server. If you want extract just one digit from dynamic values (\d) can be used. if you want to extract multiple decimals in a one go (\d+) is used


Example:


a. <decimal-Values>123456</decimal-Values>


Regular expression to capture 123456: 


<decimal-Values>(\d+)</decimal-Values>


or


<decimal-Values>(\d\d\d\d\d\d)</decimal-Values>


or


<decimal-Values>([0-9]+)</decimal-Values>


b. <decimal-Values>5</decimal-Values>


Regular expression to capture 5:


<decimal-Values>(\d)</decimal-Values>


2. (\D+) and (\D)


It is used to capture all dynamic value coming from server except decimal. It capture alphabets, White Space, New lines, Special Character. 


Example:


a. <Random-Values>a$%^sdg @ASzG<Random-Values>


Regular expression to capture a$%^sdg @ASzG:


<Random-Values>(\D+)<Random-Values>


b. <Random-Values>@<Random-Values>


Regular expression to capture @:


<Random-Values>(\D)<Random-Values>


3. (\w+) and (\w)


It is used to capture alphabets coming from server in capital or small letter. (\w+) is used for capturing multiple alphabets and underscore and (\w) used for capturing single alphabet.


Example:


a. <Sample-Words>abcdEFGH<\Sample-Words>


Regular Expression for capturing abcdEFGH:


<Sample-Words>(\w+)<\Sample-Words>


b. <Sample-Word>H<\Sample-Word>


Regular Expression for capturing H:


<Sample-Words>(\w)<\Sample-Words>


4. (\W+) and (\W)


It is used to extract all dynamic data except alphanumeric. It extract white space as well Special characters except underscore. 


Example:


a. <Target-Value>$@#$@  $#^*</Target-Value>


Regular Expression for extracting $@#$@  $#^*:


<Target-Value>(\W+)</Target-Value>


b. <Target-Value>$</Target-Value>


Regular Expression for extracting $:


<Target-Value>(\W)</Target-Value>


5. (\s) and (\s+)


it is used to capture white space from dynamic data coming from server. We generally don't need to capture white space as a Regular expression but it can be used in combine with other operations like (\d+) or (\w+). For more details see below example.


Example:


a. <Target-data>1234567   abcdefgh</Target-data>


Regular expression for capturing 1234567 and abcdegfh separately when No of white space is not fixed (i.e. sometimes there is 2 white space between strings sometimes 3 white space and sometimes more than 3):


 <Target-data>(\d+)\s+(\w+)</Target-data>


Note: we have not given \s+ in bracket as we don't want to capture it in variable, but it is working as a supportive Regular expression to capture Numeric and Alphabets 


6 (\S+) and (\S)


It is used to capture all the dynamic data except White Space. 


Example:


a. <Target-data>12ad@_!@#$%^&*()</Target-data>


Regular Expression to Capture 12ad@_!@#$%^&*():


<Target-data>(\S+)</Target-data>


7.[a-z]+ and [A-Z]+


It is used to extract any alphabets between a-z. [a-z] is used to extract only small letter. [A-Z] is used to extract only capital letter. Both can be used together to extract mixed letter.


Example:


a. <letter>performanceoptimize</letter>


Regular expression to extract performanceoptimize:


<letter>([a-z]+)</letter>


b. <letter>PERFORMANCEOPTIMIZE</letter>


Regular expression to extract PERFORMANCEOPTIMIZE:


<letter>([A-Z]+)</letter>


c. <letter>PerForManCeoPtiMizE</letter>


Regular expression to extract PerForManCeoPtiMizE:


<letter>([a-zA-Z]+)</letter>


d. if you want to find specific range of characters like between c to j that can also be done using range of c-j. it applies to both capital and small letter


<letter>cdefj</letter>


Regular expression to extract cdefj:


<letter>([c-j]+)</letter>


e. if you want to find only specific character without specifying any range, it is possible as shown below


<letter>agfefhaaeeffg<\letter>


Regular Expression to extract agfefhaaeeffg:


<letter>([aefgh]+)</letter>


8. [^a-z]+ and [^A-Z]+


it is used to match any character, decimal, special character and White space other than mentioned inside box bracket. i.e. [^a-z] will match any character, decimal, special character and white space other than a-z alphabets or [^A-Z] will match any character, decimal, special characters and white space other than A-Z alphabets. 


Example:


a. <letter>1234!@#$%^&*()_-++==</letter>


Regular Expression to extract 1234!@#$%^&*()_-++==:


<letter>([^a-z]+)</letter>


b. <letter>abc1234!@#$%^&*()_-++</letter>


Regular Expression to extract abc1234!@#$%^&*()_-++:


<letter>([^A-Z]+)</letter>


9.[a-z0-9@#$]+ and such Regular expression


If you want to match very specific set of alphabetes, special characters or numeric then you can use this custom facility for it.


Example:


a. If you want to extract tokenId and you know that it will have capital letter, small letter and 0-9 numbers and it won't have any special character or any white space. Then you can use following Custom Regular Expression.


<tokenid>ArtrDeR5HGHDDSsdccasds2342sdscDF</tokenid>


Regular Expression to extract ArtrDeR5HGHDDSsdccasds2342sdscDF:


<tokenid>([a-zA-z0-9]+)</tokenid>


b. if you just want to extract only special character with white space that can also be handled with below custom method


<tokenid>!@#$%^&*   (#@$#$@#$%#)_+_</tokenid>


Regular Expression to extract !@#$%^&*   (#@$#$@#$%#)_+_:


<tokenid>([!@#$%^&*() _+]+)</tokenid>


10. (.*?) and (.+?)


It is used to extract alphanumeric, Special character and white space. Generally you should avoid using both of this in regular expression as it can capture everything. 


Example:


a. <test>asdas!@##sdad@#@`211231adsdSAWSSAD</test>


Regular Expression to capture asdas!@##sdad@#@`211231adsdSAWSSAD: 

<test>(.+?)</test>


11. there are other different type of Regular expression but it is not widely used. you can see below brief about it



Tokens
Meaning
Example
a+
Matches one or more consecutive a character
<test>aaaaaaaa</test>

RegEx to find aaaaaaaa:

<test>(a+)</test>

a*
Matches zero or more consecutive a character
<test>aaaaaaaa</test>

RegEx to find aaaaaaaa:

<test>(a*)</test>

a?
Matches an `a` character or nothing
<test>a</test>

RegEx to find aaaaaaaa:

<test>(a?)</test>

a{n}
Matches exactly n consecutive characters
<test>aaaaa</test>

RegEx to find aaaaa:

<test>(a{5})</test>
a{n,}
Matches n or more consucative characters
<test>aaaaa</test>

RegEx to find aaaaa:

<test>(a{2,})</test>
a{m,n}
Matches consecutive characters between m and n

<test>aaaaa</test>

RegEx to find aaaaa:

<test>(a{2,5})</test>

a+?
Matches as few as possible
<test>aaaaa</test>

RegEx to find aaaaa:

<test>(a+?)</test>
ab|cd
Matches either ab or cd 
<test>ab</test>

RegEx to find aaaaa:

<test>(ab|cd)</test>
\n, \t, \r 
\n is for matching new line character, \t is for matching tab and \r is for matching carriage return


Easiest way to build any regular expression is to use available online tool which is very user friendly and allows you to create regular expression more effectively. Personally, I am using http://www.regextester.com/ for building up my regular expression.

You might face more challenge in finding correct and more accurate Regular expression for your work. You can ask me in comment section or in query section about problems you are facing while matching regular expression. 




Friday 21 April 2017

Apache Jmeter- Steps to Record Scripts

Performance Optimizer-Apache Jmeter-Recording
Below are steps to record Jmeter Script for HTTP/HTTPs protocol:

1. Launch Jmeter in GUI mode:
      Go to <Jmeter_Home>/Bin folder and click on jmeter.bat


2. Right Click on Test Plan  and Select  Add ->  Threads(users) -> Thread Group (see Below image) 

Performance Optimizer-Apache Jmeter-Recording
Thread Group is used to provide details of No of Concurrent users, Ramp Up, Loop count and Scheduling

3. Right click on ThreadGroup and Select Add -> Logic Controller ->  Recording Controller (see below image)

Performance Optimizer-Apache Jmeter-Recording
All Recorded samples are kept inside Recording Controller

4. Right click on Thread Group and Select Add -> Config Elements -> User Defined Variables (see below image)
Performance Optimizer-Apache Jmeter-Recording

5. In User Define Variables you need to include all static data. For example: Server Name, Port,etc.


Apart from static data you should also add user input data. For example: If you are recording flow of any application and which require ID and Password then you should add ID and password in user defined variables along with other input fields like Name, PhoneNo, etc.  



Note: Benefit of adding static data and user input data in user define variable is that you don't need to replace it explicitly, it will be replaced as a jmeter variable directly. You will understand it better by the time you complete reading this blog. 


6. Right Click on workBench and Select Add-> Non-Test Elements -> HTTP(S) Test Script Recorder (see below image)
Performance Optimizer-Apache Jmeter-Recording
HTTP(s) Test Script recorder are used for start and stop recording, Test Plan Creation and for Applying basic Excludes

7. Add View Result Tree inside Test Script Recorder. Right click on Test Script Recorder -> Add->Listner-> View Results Tree (see below Image)
Performance Optimizer-Apache Jmeter-Recording
View Result tree is used to see Request Response of Recorded samples

8. Launch your browser(Chrome/FireFox/IE) and change proxy setting like below

      Address: LocalHost or IP address of your machine
      Port : 8080

Chrome:
1. Go to Settings
2. Click on change Proxy Settings
3. Click on LAN settings
4. Click checkbox of use Proxy server and give input as per below images 
Performance Optimizer-Apache Jmeter-Recording


Performance Optimizer-Apache Jmeter-Recording
Firefox:
1. Go to Setting -> Options
2. Go to Advanced
3. Go to Network tab
4. click on Settings
5. set up manual proxy as per below image 
Performance Optimizer-Apache Jmeter-Recording




Performance Optimizer-Apache Jmeter-Recording
Performance Optimizer-Apache Jmeter-Recording
9. Go to HTTP(s) script recorder and click "Start"

Note: If you are getting below error, for now you can ignore it and Click "OK" to continue Jmeter scripting.



Performance Optimizer-Apache Jmeter-Recording

10.  Right Click on Recording Controller and Select Add->Logic Controller -> Transaction controller (See image below)

Performance Optimizer-Apache Jmeter-Recording
Change the name of Transaction Controller as per your Transaction steps. For Example: if you are going to Login into application give the transaction Controller name as Login. (see image below)
Performance Optimizer-Apache Jmeter-Recording

11. Go to HTTP(s) Test Script Recorder and select Target Controller as your Test Plan -> Thread Group -> Recording Controller -> Login (see image below)


12. Now go to browser and Login into Application and Come back to Jmeter instance. You can see below that "Server", "Port", "UserID" and "Password" are already as jmeter variable as we have defined it inside User Defined Variables.



13. Almost every application create session id, token id, etc after login and which remain constant for entire session of users. So after login check whether session id or token id is created or not and make Regular expression of it. In general term we have to find dynamic values which are coming from server and which needs to be correlated. 

How to apply Regular Expression?
A. Go to View result tree 



B. Find Sample which contains Token Id or Session Id and make regular Expression of it.

For Example: In above screenshot, "var tokenValue_p="JROM-NkbY-5RKt-l5Pf-0Een-ULLk"" is our TokenID 

Go to that sample in Transaction Controller and apply Regular Expression as below  and keep original value in comment section of Regular Expression as shown below 

(How to Add Regular Expression? Right click on Sample, Select Add -> Post Processors -> Regular Expression Extractor) 


14. Go to User Define Variables and Add one more variable as Token_ID, It's original Value and Description as Correlated, just as shown below


15.  Now Once again create new new Transaction controller for next transaction same as Point 10
Performance Optimizer-Apache Jmeter-Recording

16. Change the Target Controller same as Point 11

17. Record Next transaction and come back to jmeter. You will see that Server, Port, UserID, Password and Token_ID is auto replaced by jmeter (see below image)
Performance Optimizer-Apache Jmeter-Recording
18. Once again check for any other Dynamic values coming from server and create regular expression of it and add those variables in User Defined Variables, same as Point 13 and 14

19. Repeat Point 11,12,13 and 14 until end of your traversal flow

20. Click on "Stop" button in Test Script Recorded after completion of your flow

21. After end of script recording you don't need to do Any Correlation and parameterization as it is already handled in above steps

22. *Very Important Step* Go to User Define Variables. 
 a. variables which has correlation comment as description, please delete those variables from User Define Variables 
b. Variables which don't have any comment as description, either keep those variable as it is or create CSV data set config as per your requirement 


Performance Optimizer-Apache Jmeter-Recording


Upcoming Topic: Regular Expression Extractor