Book Image

JMeter Cookbook

By : Bayo Erinle
Book Image

JMeter Cookbook

By: Bayo Erinle

Overview of this book

Table of Contents (16 chapters)
JMeter Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Recording a script via HTTP(S) Test Script Recorder


Recording scripts is where you will spend most of your time. It is often the first step to develop test plans for applications. Though you can develop test plans manually by building them from within the JMeter GUI, or generating them via some domain-specific language (DSL), recording scripts by JMeter's HTTP(S) Test Script Recorder is one of the available options you have when building your test plans.

How to do it…

In this recipe, we cover how to record test scripts with HTTP(S) Test Script Recorder. Perform the following steps:

  1. Start JMeter. Perform the following steps:

    • Open the command-line prompt

    • Change to the directory of your JMeter install (JMETER_HOME)

    • Change to the bin directory

    • Execute the following command:

      For Windows, type the following command:

      	jmeter.bat
      

      For Mac OS/Unix, type the following command:

      	./jmeter
      
  2. Once the GUI is opened, click on the Templates… button (the icon that appears immediately to the right of the new test plan icon) on the toolbar.

    Note

    Templates were added in JMeter 2.10, so don't expect to see them in versions prior to that.

  3. In the drop-down menu of the Select Template box, choose Recording and click on the Create button.

  4. Click on the HTTP(S) Test Script Recorder element (under WorkBench) and change Port (under Global Settings) from 8888 to 7000.

    • You can use a different port if you want to. What is important to choose is a port that is not currently used by an existing process on the machine. The default is 8888.

  5. Leave the rest of the components with the default set values.

  6. The template does an excellent job and is configured with sensible defaults. The defaults provide the following:

    • Target the recorded actions to Recording Controller added by the template.

    • Group a series of requests that constitute a page load. We will see more on this topic later.

    • Instruct the script recorder to bypass recording requests of a series of elements that are not relevant to test execution. These include JavaScript files, style sheets, and images.

    • Add the often-used components, including user-defined variables, HTTP Request Defaults, and HTTP Cookie Manager, to the test plan.

    • Add Thread Group and Recording Controller to the test plan.

    • Add a View Results Tree listener to view results of the test plan.

  7. Click on the Start button at the bottom of the HTTP(S) Test Script Recorder component.

With these settings, the test script recorder server will start on the port 7000, monitor all requests going through that port, and record them to a test plan using the default recording controller. For details, see the following screenshot:

HTTP(S) Test Script Recorder configuration

How it works…

These steps instruct JMeter to act as an HTTP proxy and listen for incoming and outgoing requests from your browser to the Internet on the assigned port, in our case 7000.

We now need to configure the browser to use the proxy server we just configured and redirect requests to the assigned port. There are several ways to do that, but we will focus here on the two most common ways.

There's more…

Modern browsers have a vibrant and active plugin ecosystem that allows you to extend the capabilities of your browser with an added plugin. FoxyProxy is one such plugin (http://getfoxyproxy.org/). It is a neat add-on for a browser that allows you to set up various proxy settings and toggle between them on the fly, without having to mess around with system settings on the machine. It really makes the work hassle free. Thankfully, FoxyProxy has a plugin for all major browsers including Google Chrome, Firefox, and Internet Explorer. If you are using any of those, you are lucky; head over and grab it!

Changing the machine system settings

The other common way to configure the browser is to change the system settings. The following are the details on how to configure Windows and Mac OS.

On Windows OS, perform the following steps to configure a proxy:

  1. Navigate to Start | Control Panel | Network and Internet | Internet Options.

  2. In the Internet Options dialog box, click on the Connections tab.

  3. Click on the LAN Settings button.

  4. To enable the use of a proxy server, check the Use a proxy server for your LAN (These settings will not apply to dial-up or VPN connections) box as shown in the following screenshot.

  5. In the proxy Address box, enter localhost in the IP address.

  6. In the Port number text box, enter 7000 (to match the port you set up for your JMeter proxy earlier).

  7. If you want to bypass the proxy server for the local IP addresses, select the Bypass proxy server for local addresses checkbox.

  8. Click on OK to complete the proxy configuration process. This is shown in the following screenshot:

    Manually setting a proxy on Windows 7

On Mac OS, perform the following steps to configure a proxy:

  1. Navigate to System Preference | Network | Advanced….

  2. Go to the Proxies tab.

  3. Check Web Proxy (HTTP).

  4. Under Web Proxy Server, enter localhost.

  5. For port, enter 7000 (to match the port you set up for your JMeter HTTP proxy earlier).

  6. Do the same for Secure Web Proxy (HTTPS).

  7. Click on OK, as shown in the following screenshot:

    Manually setting a proxy on Mac OS

For all other systems, consult the related operating system documentation.

With both JMeter's HTTP(S) Test Script Recorder and browser configured to use it, we are now ready to record a test. Perform the following steps:

  1. Point your browser to a website of your choice.

  2. Navigate to a few links.

  3. Notice that all user actions are being captured under the Thread Group component in the JMeter GUI.

  4. Stop the recording by stopping HTTP(S) Test Script Recorder.

The HTTP(S) Test Script Recorder server is set up to listen to requests on a particular port and then set the browser or system to redirect all requests to the assigned port. This allows us to capture the user actions as they interact with web applications, allowing us to replay their actions like we did in the Executing a test script recipe.

See also

  • The Recording scripts via the Chrome browser extension recipe in Chapter 8, Beyond the Basics

  • The Writing Test scripts through Ruby DSL recipe

  • The Converting HTTP web archives (HAR) to JMeter test plan recipe