1
votes

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

3

3 Answers

1
votes

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">
1
votes

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!!!

0
votes

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.