1

This is how I have Test Plan set up:

HTTP Request -> Regular Expression Extractor to extract multiple links - This is extracting correctly -- But some of the links are Blank RegularExpressionExtractor --- <a href="(.*)" class="product-link">

BeanShell Sampler - to filter blank or null values -- This works fine

BeanShell Sampler

log.info("Enter Beanshell Sampler");
matches = vars.get("url_matchNr");
log.info(matches);

for (Integer i=1; i < Integer.parseInt(matches); i++) 
{
    String url = vars.get("url_"+i);
    //log.info(url1);
    if(url != null @and url.length() > 0)
    {
        log.info(i+"->" + url);
        //return url;
        //vars.put("url2", url);
        vars.put("url2", url);
        //props.put("url2", url);
        log.info("URL2:" + vars.get("url2"));
    }
}

ForEach Controller ForEach Controller

Test Plan

The problem I am facing is ForEach Controller runs through all the values including Blank or NULL -- How can I run the loop only for the non null blank values

Allan
  • 12,117
  • 3
  • 27
  • 51
Sandeep Sharma
  • 109
  • 2
  • 15

3 Answers3

1

You should change your regular expression to exclude empty value

Instead of using any value including empty using * sign

 <a href="(.*)" class="product-link">

Find only not empty strings using + sign:

 <a href="(.+)" class="product-link">
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
1

As mentioned earlier, you should change your regex!

you can replace it directly by

<a href="(.+)" class="product-link">

or by something more constraining like this:

<a href="^((https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?)$" class="product-link">

which is a regex to match only URLs.

https://code.tutsplus.com/tutorials/8-regular-expressions-you-should-know--net-6149

The first capturing group is all option. It allows the URL to begin with "http://", "https://", or neither of them. I have a question mark after the s to allow URL's that have http or https. In order to make this entire group optional, I just added a question mark to the end of it.

Next is the domain name: one or more numbers, letters, dots, or hypens followed by another dot then two to six letters or dots. The following section is the optional files and directories. Inside the group, we want to match any number of forward slashes, letters, numbers, underscores, spaces, dots, or hyphens. Then we say that this group can be matched as many times as we want. Pretty much this allows multiple directories to be matched along with a file at the end. I have used the star instead of the question mark because the star says zero or more, not zero or one. If a question mark was to be used there, only one file/directory would be able to be matched.

Then a trailing slash is matched, but it can be optional. Finally we end with the end of the line.

String that matches:

http://net.tutsplus.com/about

String that doesn't match:

http://google.com/some/file!.html (contains an exclamation point)

Good luck!!!

Allan
  • 12,117
  • 3
  • 27
  • 51
0

ForEach controller doesn't work with JMeter Properties, you need to change the "Input Variable Prefix" to url_2 and your test should start working as expected.

Also be aware that since JMeter 3.1 it is recommended to use Groovy language for any form of scripting so consider migrating to JSR223 Sampler and Groovy language on next available opportunity.

Groovy has much better performance while Beanshell might become a bottleneck when it comes to immense loads.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • In Beanshell Sampler I have this for (Integer i=1; i < Integer.parseInt(matches); i++) { String url = vars.get("url_"+i); //log.info(url1); if(url != null @and url.length() > 0) { log.info(i+"->" + url); //l1=__javaScript(url.length); //log.info("Length:"+l1); //return url; vars.put("url2", url); //props.put("url2", url); log.info("URL2:" + vars.get("url2")); } } In ForEachController Input Variable I am using ${url2} and Output Variable url_out but its not working – Sandeep Sharma Dec 07 '17 at 04:36
  • Of course it will not be working, I would suggest re-read my answer as many times as it will be required to see what you should put at the "Input Variable Prefix" – Dmitri T Dec 07 '17 at 06:46
  • Using url_2 works great. Now I want to extend this further and use the url into another thread group and use more than 1 threads. I used this option https://www.blazemeter.com/blog/knit-one-pearl-two-how-use-variables-different-thread-groups and it passes the value to the HTTP sampler successfully but I need to use the property value into the for each controller which I understand not possible then I tried this option https://stackoverflow.com/questions/707832/how-do-i-pass-a-variable-from-one-thread-group-to-another-in-jmeter some how this didn't work either do you have any simpler way? – Sandeep Sharma Dec 11 '17 at 22:00