Using CSS Selectors in JMeter Scripts
Take a look at this tutorial that will show you how to construct CSS Selector to extract data from your webpage for performance analysis.
Join the DZone community and get the full member experience.
Join For FreeWhen performance tests are executed, the main focus is on how the application behaves under heavy load. To analyze it, you need to extract data from a web page and create a new request with the retrieved value. Or you just need to parse the response and validate an assertion. To deal with these challenges efficiently, you can use CSS Selectors. In this article, we’ll go through the basic usage of CSS Selectors in JMeter scripts. If you need more on this topic, please feel free to contact me through our contact us form with the message subject as “CSS Selectors”.
Let’s See CSS Selectors in Action!
We have an HTTP Sampler which makes a GET request to a landing page e-commerce website. In the main page, products are listed. To make a randomized navigation from the main page, the URL of the new pages needs to be extracted. To do this:
Add a CSS Extractor which will fetch the URLs.
Extracting Data from Response
The ProductLink Extractor looks like the above figure.
- The "Reference Name" field needs to be filled out. This variable will be used in different places with this reference name.
- The CSS/JQuery expression should be detected from the console of your web browser. If you are familiar with Selenium, you won’t have any difficulty.
- The "Attribute" field defines the attribute of the extracted web element.
- In order to make a GET request to a random link from a list, you should enter “0” in Match No.
- Set default value to
NoLinkFound
or a valid product Link.
Making a New Request
- Fill your second HTTP sampler’s Path field with the
productLink
variable by using${variableName}
syntax. - Set the Name of the HTTP sampler with the same variable so you will be able to understand which pages you hit during the test run.
By doing so in every run, you will visit a different link.
How to Debug the CSS Selector
- Add a “View Result Tree” listener in your Thread and run your test once.
- After the execution, open “View Result Tree” listener and switch to CSS/JQuery Tester View.
There, you can debug your CSS expression in JMeter.
Do you want to run your JMeter test in the cloud? Try Loadium now!
Published at DZone with permission of Canberk Akduygu. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments