Using CSS Selectors in JMeter Scripts

Using CSS Selectors in JMeter Scripts

When performance tests are executed, the main focus is on how the application behaves under heavy load. To analyze it, you need to extract…

Using CSS Selectors in JMeter Scripts

When 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 your JMeter scripts.

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 us through our contact us form.

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

  • Add a CSS Extractor which will fetch the URLs.

Extracting Data from Response

ProductLink Extractor looks like the above figure.

  • Reference Name field needs to be filled out. This variable will be used in different places with this reference name.
  • CSS / JQuery expression should be detected from the console of your web browser. In case you are familiar with Selenium, you won’t have any difficulty.
  • 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 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 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.

CSS Selector Tips

1- Absolute Path

Using .list-group>a to reach an element directly.

2 — Non-Absolute Path

Using a white space between tags to locate the element. Use “.” for class and “#” for id.

.list-group .facebook-signin find the login button

3- Starting Text of an Attribute

You can use tag[attribute^=’starting text’] syntax.

Happy Load Testing!