While scripting E-Commerce sites, situations, where cart service is not called after cart delete service, can be faced. It forces us to make only one request to cart and delete every item on cart one by one, without calling the cart request again. Let’s check how to use “-1”/all on JMeter selector.

Since the cart request is called only once, we have to get IDs of every item in the cart in only one selector.
In this blog, you will see step by step how to overcome such situations. 

You can also apply this solution to JSON and REGEX Extractors. 

how-use-1-all-on-jmeter-selector-1

I want to find the right testing type for my product

I want QA experts to test my application

I’m looking for a long-term testing partner   

I want to consult a QA Specialist   

First, we login and add a random number of products to our cart. When we test our CSS Selector in response data of cart request, we can see we have 12 products in cart. 

Here is our CSS Selector Extractor. 

how-use-1-all-on-jmeter-selector-2

Let ‘s use a Debug Sampler and see what using –1 on Match Number brings us.

how-use-1-all-on-jmeter-selector-3

As we can see, when we select all with –1 on CSS Selector Extractor, it numbers our variables as “variable_1-12″ and shows us number of results found as “variable_matchNr”. 

To be able to use all these variables and make them dynamic, we are going to put all of them in an array. We will be using JSR223 PostProcessor. 

how-use-1-all-on-jmeter-selector-4

I created a “productIds ArrayList” and added each ID to this array. I reached these variables using “vars.entrySet()“. Finally, I used “vars.putObject()” because I want my array to be reachable by the following requests as well. 

Now we have to get our service for deleting product in my cart into a Loop Controller. Since I want to call it for every product, I set my Loop Count as “${productsInCart_matchNr}” which was created automatically depending on product number in cart as we saw in previous steps. It is 12 in our scenario. 

how-use-1-all-on-jmeter-selector-5

In our delete service, we set our product ID as “${productToRemove} so that we can change it dynamically in every iteration of our loop. 

how-use-1-all-on-jmeter-selector-6

Now I am adding a JSR223 PreProcessor to my deleting service, just to manipulate my “${productToRemove} variable on each iteration before calling the service. 

how-use-1-all-on-jmeter-selector-7

This part is a little tricky. Now I am reaching “productInCartArray” with “vars.getObject()” and setting it to a new array which I defined as “getArray“. After that, I am setting my “${productToRemove}” variable which I mentioned in previous step to first index of my “getArray“. Now we are able to delete our first product.  

But don’t forget that this PreProcessor will be called on each iteration, so if I don’t remove the element I have already used from the array, my “${productToRemove}” will never change again. So, I am removing the index I already used with “getArray.remove(0)” and putting my new array without the already used ID to “productsInCartArray“. So, on 12 loop iterations, I will be getting a different index every time my PreProcessor runs.  

I am getting my array, getting the first index of array and setting it to a variable. I am removing the first index and replacing it with my base array. 

At the end of all iterations, I will end up with no IDs in my array and an empty cart.
Let’s run the script and see the results. 

how-use-1-all-on-jmeter-selector-8

The number of products in my cart was 12. As you can see, my delete service was called 12 times with 12 different IDs, and I ended up with an empty “productsInCartArray“.  

Let’s double check it on response data of cart service as well. 

how-use-1-all-on-jmeter-selector-9
how-use-1-all-on-jmeter-selector-10

There is no product left in my cart 🙂  Thank you for your time! 

Run Your Existing JMeter Tests With Any Parameters From Any Location

More Tests.  More Thread.  More Minutes

SWITCH TO LOADIUM TODAY