5

I want to execute Ajax requests in JMeter. I record my test plan with JMeter and BlazeMeter but none can handle Ajax requests. How can I solve my problem?

Or is there any tool that can send concurrent Ajax requests?

Thanks in advance, M.A

dda
  • 6,030
  • 2
  • 25
  • 34
Mitra Alidoosti
  • 67
  • 1
  • 1
  • 8

4 Answers4

7

By default, Ajax requests can't be simulated by JMeter as it does not process .js files (As of 3.1 version). You have to explicitly add the requests (HTTP Samplers) for AJAX requests.

Use Network tab of a browser (F12 option) and filter the traffic by xhr, which shows only AJAX requests. Add those requests as HTTP Samplers in the script at the point you needed in the Test Plan.


Browser can process .js files, hence AJAX requests are sent from the browser. When you record the script using HTTP Test Script Recorder, as you are using the browser to navigate, even AJAX requests (originated from js files) will also be added/recorded to the Test Plan.

Since JMeter can't process .js files due to limitation, manually add the AJAX requests (nothing but HTTP Get or Post requests) using HTTP Sampler, if you are building the Test Plan without HTTP Test Script Recorder.

Note: If you are using HTTP Test Script Recorder, no need to add them manually as they are already recorded in the Test Plan.

Naveen Kumar R B
  • 6,248
  • 5
  • 32
  • 65
  • JMeter can record all of JS requests that shows on Network tab of browser in Js and xhr section but when I play the test plan, the JS requests won't take effect and nothing will be down by Js requests. – Mitra Alidoosti Jan 02 '17 at 12:21
  • 1
    Yes, That is an expected behaviour for now. JMeter don't process `js` files, so requests originated from `js` files, will never be sent. That's a known limitation of JMeter. you have to add the requests explicitly for them using HTTP Samplers. – Naveen Kumar R B Jan 02 '17 at 12:27
  • I use sampler/HTTP request for my JS requests. in other words I right click on sampler and select HTTP request then add the path to JS file in path field. but it does not process JS file. it does not HTTP sampler? am I in the wrong way with HTTP sampler? – Mitra Alidoosti Jan 02 '17 at 12:39
  • 1
    As I said, JMeter does not have the capability of processing JS files. Browser can process JS files once downloaded/received, so HTTP requests (called AJAX requests) originated from them are sent. But, JMeter can't process JS files due to the limitation. So, compare the requests which are originated from JS files (using browser -> F12 tab), add the HTTP requests for them manually using HTTP Sampler. The process you followed is correct, but that is not to process JS file, except simulate an HTTP request. When you record the script using HTTP Script Recorder, even AJAX requests will be captured. – Naveen Kumar R B Jan 02 '17 at 12:57
  • Excuse me :(, but I still have problem with JS requests. I record HTTP request by HTTP Test Script Recorder so I don't need to add JS request manually according to your answer. what should I do now? – Mitra Alidoosti Jan 02 '17 at 13:44
  • 1
    please add screenshots of your test plan. what you are expecting? and what is the actual behaviour? (add screenshots of View Results Tree, to the question). – Naveen Kumar R B Jan 02 '17 at 14:02
  • the actual behaviour is that I should generate products in wordpress woocommerce by product generator plugin, when I record all actions to generate products, all of HTTP request and also JS requests are recorded by JMetre. but when I play the recorded test plan. it does not generate any product. in view results tree there is no error! and all request send correctly. the test plan and view results tree are shown blow:https://www.dropbox.com/sh/q1hyeml1fxbpdey/AAASjPTu0ifl4KmDRbGdmQpGa?dl=0 – Mitra Alidoosti Jan 02 '17 at 14:46
  • 1
    I remember that WordPress has `wpnonce` (check check online for the behavior) for each type of action, which has short life span. did you correlate this value? Compare the requests, send by browser & send by Jmeter. find out the differences. probably, the differences (change in values), you may need to correlate. – Naveen Kumar R B Jan 02 '17 at 15:00
  • 1
    Thanks a lot, your hint about wpnonce, rings a bell to solve my problem. – Mitra Alidoosti Jan 03 '17 at 13:51
0

you can do that by recording http traffic with JMeter Proxy

http://jmeter.apache.org/usermanual/jmeter_proxy_step_by_step.pdf

but it is only possible to replay the same traffic, can be problematic for sessions, cookies... Better solution is to use WebDriver

https://jmeter-plugins.org/wiki/WebDriverTutorial/

https://jmeter-plugins.org/wiki/PluginInstall/

the needed package is https://jmeter-plugins.org/files/packages/jpgc-webdriver-1.4.0.zip

Hassen Bennour
  • 3,885
  • 2
  • 12
  • 20
  • I tried this solution step by step, but WebDriver Sampler gave me error. in log viewer it gives this error: Test failed! java.lang.NoClassDefFoundError: org/openqa/selenium/os/Kernel32 – Mitra Alidoosti Jan 02 '17 at 12:50
  • have you added needed libs ?? i've updated with link – Hassen Bennour Jan 02 '17 at 13:06
  • by installing **"Selenium/WebDriver Support"** plugin this must copy selenium-remote-driver-x.xx.x.jar and other jar's to JMETER_HOME/lib folder – Hassen Bennour Jan 02 '17 at 13:31
  • JMeter handles cookies (and sessions) with the HTTP Cookie Manager config element. You don't need to use a WebDriver for that. – DavidS Mar 11 '19 at 20:02
  • @DavidS i'm not sure that the HTTP Cookie Manager affects selenium HtmlUnit implementations... but certainly the HTTP REQUEST elements... – Hassen Bennour Mar 12 '19 at 08:00
0

It is not possible as kicking off more threads than originally defined in the Thread Group is not currently supported, the feature is being tracked as Bug 53159. AJAX requests are "normal" HTTP Requests so JMeter can record and replay them, but when it comes to asynchronous execution - you cannot do this as of now. The workaround options are in:

  1. Use WebDriver Sampler plugin so each JMeter virtual user will kick off a real browser. Warning: this way is very resource intensive.
  2. Use scripting, i.e. JSR223 PostProcessor to kick off AJAX-driven requests via Apache HttpComponents
  3. Develop your custom sampler. You can use the current way of handling embedded resources as a reference.

Learn more: How to Load Test AJAX/XHR Enabled Sites With JMeter

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
0

I had this same problem after recording samplers using the JMeter Proxy. The answer was on this blog post: https://lincolnloop.com/blog/load-testing-jmeter-part-1-getting-started/ - see the Ajax Requests section.

Add an HTTP Header Manager for the ajax request and make sure you are sending the X-Requested-With:XMLHttpRequest header.

montag451
  • 21
  • 3