How to Use Regular Expression for dynamic values in Jmeter? – Devstringx
What Is Apache JMeter?
Apache JMeter is a 100% java based open source testing application with a graphical interface. JMeter uses to analyze and measure the Load and Performance behavior of web applications. JMeter is very useful in Load, Stress, Performance, Functional, and regression testing for web applications.
What is a Regular Expression?
In JMeter, the role of the regular expression is very important to identify dynamic values that come in a response from the server. In micro-focus LoadRunner the dynamic values are captured by correlation. A regular expression is a pattern that is used to specify a set of strings required for getting dynamic values.
Regular Expression Extractor
A regular expression extractor is a post processor which executes after the response arrives and always adds in the HTTP request sampler whose response contains a dynamic value you need to capture that value and pass it in the next request where the desired request is required.
Step 1: Download the Web Tour Application
Download and install the WebTours application and start the server of the WebTours application.
Step 2: Launch the WebTours Application
Open Chrome browser and enter the local system IP address followed by web tours, like this: 192.198.57.175:1080/webtours/
Step 3: New Sign-Up and Login
In the WebTours application do a new sign-up by entering the required details.
After that, open the network tab of the chrome browser by inspecting the page.
Now login with the same new credential and see the request and responses.
Step 4: Create a Test Plan in Jmeter
- Create test plan
- Add a new thread group
- Add an HTTP request sampler name as the login
- Now enter all required details like protocol, IP address, port number, HTTP request and port number, and path from the login request URL.
- From the network, tab copy the payload view source and paste in Jmeter parameters by clicking add by clipboard and HTTP request done.
Step 5: Submit Flight Details and Book a Flight
Now click on Flight in the WebTours application and book a flight by entering payment details.
Step 6: Create the same HTTP request sampler for the flight book in JMeter
Create a separate HTTP request sampler for selecting a destination, selecting a flight and confirming details request, and filling all details as same do for login.
Step 7: Now Save & Run the test plan
Now add the listener view results tree, save, and run the test plan.
Now everything runs correctly as we assume but the issue is that it is not logged in correctly and the error displayed “You have reached this page incorrectly(probably a bad user session value)” but still book tickets.
So here the problem is the user session which was valid only for that user at that time not for all of the time.
Now if I run the same test plan again then the user-session changes as displayed below:-
This is a correlation. So I need to generate user sessions unique in every run by programming regular expressions. Now find where this value call
Now, need to grab this value and input that into login.pl payload dynamically every time so that your test will never fail.
Step 8: Create a get session HTTP request sampler
Now create an HTTP request name as get session at first of all samplers and enter the port, IP address, and path. It runs and gets a session in the result tree response body.
Now introduce a Regular expression extractor to extract this session value from here.
In Get Session > add Post Processor > regular expression extractor > and add expression and create variable ‘sessionid’.
Enter the regular expression – name = “user-session” value=”(.+?)”
Now update the login User session value as $(sessionid).
Now save and see the result. It looks great.
Now we log in correctly with a unique user session id and after the login message displayed the Welcome to WebTours reservation page correctly. So by Regular expression extractor, we can use dynamic values in JMeter.
Recommended to Read