Record and Playback in JMeter

Before starting with the scripting using JMeter let's know what this tool is all about and how it is different from others. JMeter is a java desktop application developed by Apache for load testing of web applications and services. It's a free of cost, open source tool that requires JRE for its functioning. As compared to other tools it requires more customization and has less analysis capability. But its features can be very much enhanced by ever increasing plug-ins available online for free.

Now let's get started with JMeter-

First of all download JMeter package from http://jmeter.apache.org/download_jmeter.cgi. Under binaries click on apache-jmeter-.zip md5 pgp for windows and apache-jmeter-.tgz md5 pgp for Linux. JMeter does not require any installation, once downloaded extract the package, go to bin directory and click on JMeter.bat file for windows and JMeter.sh for linux (from here on we'll be considering windows as our OS). After a command prompt window, JMeter main screen should appear. If it doesn't then you haven't installed Java Runtime Environment (JRE), so, get it installed.JMeter Screen
In the main JMeter screen we can see the two controls in the left pane - 
1. Test Plan
2. Workbench
Test plan is the area where all the scripting will be done and saved. Whereas Workbench is the area which we use for temporary purpose, basically it is used for proxy setting as we will see in subsequent steps.
To begin with, first of all we will create virtual users which are called Threads in JMeter nomenclature. These threads will simulate users that will execute a particular test scenario for a predefined time or predefined executions. To create thread groups- highlight Test Plan, right click on it, and click on Add-> Threads (Users) -> Thread Group. Once done you will have Thread group control under Test Plan hierarchy, rename it according to your application's user scenario (like Read unread mails). 
Next step is to add transaction controllers within the thread group. For this right click thread group and click on Add->Logic Controller->Transaction Controller make sure to click on generate parent sample checkbox. Add a transaction controller for each step of the user scenario of the thread group created e.g. considering the "Read unread mail" user scenario discussed in the second post "Planning and execution of load test", there would be transaction controller for User launch the application, User log in with valid credentials, Click on the links of unread mails, Log out and Exit the application. Name all the transaction controllers accordingly.
Similarly, we can create other thread groups and their corresponding scenarios also.

So, now we have got a skeleton where we can record and create scripts, next thing we will do is to record http or any other type of requests inside the transaction controllers. For this we need to setup proxy in the JMeter and our browser. Although we can record calls with any browser but it is recommended to use Mozilla Firefox just because of its plug-in 'Firebug'. If you don't have then get both of them downloaded and installed as firebug is very important for validating whether all calls are successfully recorded or not. 

For recording, first we need to set proxy in our browser. Steps to set proxy in Mozilla are-
1. Click on Tools-> Options
2. Click on Advanced, under Advanced click on Network tab
3. Click on setting, a connection Settings from would appear
4. Click on manual proxy configuration radio button
5. Now type 127.0.0.1 in the HTTP Proxy textbox and any available port in Port textbox like 9001
Proxy Setting
Let's see what we have done, the IP address we have entered in HTTP Proxy textbox is loopback address that is software loopback interface of the machine itself on which we are working and the port we have specified is the Port through which all the traffic will be routed, this port will be used in JMeter also. 

Next step is to set JMeter for recording calls in the transaction controllers created. Steps for this are -
1. Right click on WorkBench.
2. Click on Add-> Non Test Elements-> HTTP Proxy server
3. Enter any port available (the one we have entered in Port of our browser), the reason why I say available is, at times the port we choose e.g. 8080 which is by default port may be getting used by some other processes. In such cases we will get error stating the port is in use. Then we have to choose some other port.
4. Under Target Controller dropdown select the dropdown in which we want to do the recording.
5. Click on start button to start the recording.
Proxy JMeter
Now whatever we do in our browser will get saved in the form of http requests in the transaction controller that we have chosen. Suppose we chose "User launch application" as our target controller then we will click on start button on JMeter then we will go to our browser and launch the application. Again go to JMeter and check whether any requests are recorded in the "User launch application" transaction controller or not. If yes then click on stop button, chose next transaction controller from the target controller dropdown (User enter valid credentials and click on Login button), click on start button, go to browser and enter credentials and click on login button. Go to JMeter and click on stop button. Perform these steps for every transaction controller.

Now we are done with recording, next thing is to add Listeners to our Test Plan that are used for interpretation of results obtained on running our test. For this right click on Test Plan-> Add-> Listeners. You will get a list of all the Listeners available, as of now use Aggregate graph and View Result Tree Listeners.

Time to run our first test-
Go to Thread Group and verify that value of Number of Threads, Ramp up Period and Loop Count is set to 1. Now press Ctrl+R key or click on Run->Start to run the test. On top right you will see green light indicating that the test is running. Once complete check for results and graphs in Listeners.

Some of the transaction controllers may fail (check in view result tree- failed come in red) because lots of scripting is still required in the test plan. It's better to start with a simple website that has no authentication checks and backend programming.

So, this was all about record and playback. In the next posts we will discuss "Parameterization and Correlation" that are actually heart and soul of scripting. 

- See more at: http://artoftesting.com/performanceTesting/record.html#sthash.E3OXSJbL.dpuf