0

I'm new in JMeter, I'm working on one project on JMeter. where i have to extract value from HTML response depend on condition.

Actual data:

<li class="size size-item 159103">
<button id="productSizesAndGrid-btn-size-1" class="btn selection-button lowStock" data-skucode="159103" data-size="XS 8/10 " data-in-wishlist="false" data-stock-status-name="Low stock"> XS 8/10 </button>
</li>

I want to extract data-code but if data-stock="In stock". data-stock can be out of stock as well so if data In Stock then and then only i want to extract that value.

I understand simple value extraction using regular expression extractor but couldn't understand how to do this.

when i working with this i realised there are disable data with same attribute and class

<li class="size size-item 159108">
<button id="productSizesAndGrid-btn-size-1" class="btn selection-button outOfStock disabled" data-skucode="159108" data-size="XS 8/10 " data-in-wishlist="false" data-stock-status-name="Out of stock"> XS 8/10 </button> 
</li>

Edit:

I tried using below answer : it's not working in my case

Regular Expression : <button id="btnid" class="btn inStock" data-code="(.*?)"(.*?)data-stock="In stock">
Select the first template $1$

Using css selector:

#btnid

this works in my case now problem is i got two elements because both ids are same how to get specific element; i want second one as first one is disabled.

<button id="product-1" class="btn outOfStock disabled" data-code="123456"  wishlist="false" data-stock="Out of stock" style="" css="1"></button>

<button id="product-1" class="btn inStock" data-code="123123" data-size="XS 8/10 " wishlist="false" data-stock="Low stock" style="" css="2"></button>

It works if you add 2 in Match No: 2

but i don't know it's correct way or not!

by above solution i still didn't get actual elements which are in stock if i get that then my problem solved but i have only temporary solution; i will appreciate if anyone can show me right way to do it.

Answer: below accepted answer work for me:

Match count: 13
Match[1]=
Match[2]=159109
Match[3]=159110
Match[4]=159111
Match[5]=159112
Match[6]=159103
Match[7]=159104
Match[8]=159105
Match[9]=159106
Match[10]=159107
Match[11]=
Match[12]=
Match[13]=
Rucha Bhatt Joshi
  • 822
  • 1
  • 15
  • 38

2 Answers2

1

To achieve this follow below steps:

  1. Add Regular Expression Extractor as a child of the Sampler returning the Response

enter image description here

  1. Regular Expression extractor setting as below:

    Regular Expression : <button id="btnid" class="btn inStock" data-code="(.*?)"(.*?)data-stock="In stock">
    Select the first template $1$
    

enter image description here

  1. Proof of it working

enter image description here

  1. Afterwards you can use ${dataCode} anywhere in the script.
SAIR
  • 479
  • 3
  • 9
  • it make sense for sure but still not working in my case i don't know what i'm doing wrong! – Rucha Bhatt Joshi Sep 13 '19 at 15:09
  • i also tried using css selector- select – Rucha Bhatt Joshi Sep 13 '19 at 15:16
  • I am not sure what you are doing. You need to run HTTP Sampler with proper HTTP URL that give you the Response which you are interested. Then you need to place Regular Expression Extractor as a child of that HTTP Request Sampler with the setting shown in the answer. – SAIR Sep 13 '19 at 16:29
  • can you explain me this data-code="(.*?)"(.*?) from your code? may be this part is not working – Rucha Bhatt Joshi Sep 16 '19 at 09:15
  • https://stackoverflow.com/questions/3075130/what-is-the-difference-between-and-regular-expressions – SAIR Sep 16 '19 at 09:35
  • Can you paste the Response? Because as per what you have provided in the question, it will work with this Regex working. You can also try this: – SAIR Sep 16 '19 at 09:37
  • yes, after trying a lot i got response but i got two – Rucha Bhatt Joshi Sep 17 '19 at 09:14
  • can you please check @SAIR – Rucha Bhatt Joshi Sep 17 '19 at 09:57
  • It is not at all clear what you want exactly, first you provided a Response, based on that we provided a solution. Then you paste some other response and say that it does not work. Provide a clear Response and what do you want. Because Regex will change depending upon what you want. – SAIR Sep 17 '19 at 11:08
  • sorry but both are same it doesn't matter what are the ids or class values!! does it? if you check there are four elements all we need to work on id, class ,code and data-stock -both are same data i just change values!!!! – Rucha Bhatt Joshi Sep 17 '19 at 11:32
  • Yes it does make the difference, if you see the right boundary of my regex it stops on data-stock="In stock">, now if you see the other 2 lines it does not end on the right boundary. So if you want to extract data-code value, for data-stock="In stock" use the following regex: ` – SAIR Sep 17 '19 at 12:33
  • ok make sense i tried with that also not working it only works when i write - data-code="(.*?)" this works but fetch all the code instead of only in stock!! :( – Rucha Bhatt Joshi Sep 17 '19 at 13:32
  • This simply means you do not have any value with `data-stock="In stock"`. Are you really sure, you are asking the right question. Because it seems that you do not have any Response with `data-stock="In stock"`. You need to tell the correct Response. Because 2nd and 3rd line as well does not have, instead it has class="btn inStock" and class="btn outOfStock disabled". I think you might be interested in class. – SAIR Sep 17 '19 at 13:40
  • i update with actual data and disable data which affect my fetch, can you please check now? – Rucha Bhatt Joshi Sep 18 '19 at 08:26
  • thank you for helping me for this question i really appreciate by voting up this; it doesn't solve my problem but i learn something new as i'm new in jmeter. thank you again :) – Rucha Bhatt Joshi Sep 18 '19 at 10:08
1

Don't use regular expressions for parsing HTML, they're hard to develop and maintain, moreover they're fragile and sensitive to markup changes.

I would recommend going for CSS Selector Extractor, the relevant CSS selector to match <button> tags where data-stock attribute is not Out of stock would be something like:

button:not([data-stock=Out of stock])

You need the data-code attribute

Demo:

enter image description here

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • quick question: it's working but i get all the values and those which are out of stock is blank as you can see in question: i also want to rid of blank value from this array how to do that? – Rucha Bhatt Joshi Sep 18 '19 at 11:14