Rachit M Garg
Rachit M Garg

Reputation: 281

Extract href attribute value from response and save in separate file for each URL Jmeter

Hi I am running a test scenario where I am taking relative URL's from the csv file and hitting it with the same after appending it to domain. Now I want to extract all the href present in the body for each page hit and save in a separate file. Say if I am hitting www.abc.com/url1 and this pages has 10 href than 1st file should be named as url1 and should contain these 10 href values.Similarly for all the other URL's

Please suggest as I am not an expert in Jmeter. I tried googling but didn't found an understandable way.

Thanks in advance

Upvotes: 0

Views: 849

Answers (1)

Dmitri T
Dmitri T

Reputation: 168082

You can do it as follows:

  1. Add CSS/JQuery Extractor as a child of the HTTP Request and configure it as follows:

    • Reference Name: anything meaningful, i.e. href
    • CSS/JQuery Expression: just a
    • Attribute: href
    • Match No: -1

    This will extract all "href" attributes from all links into JMeter Variables like:

    href_1=http://foo.bar
    href_2=http://example.com
    ...
    href_matchNr=10
    
  2. Then add Beanshell PostProcessor as a child of the same HTTP Request after the CSS JQuery Extractor. Put the following code into the PostProcessor's "Script" area:

    import org.apache.commons.io.FileUtils;
    
    File csvFile = new File("/path/to/url1.csv");
    int hrefCount = Integer.parseInt(vars.get("href_matchNr"));
    for (int i = 1; i <= hrefCount; i++) {
        FileUtils.writeStringToFile(csvFile,vars.get("href_" + i) + System.getProperty("line.separator"), true);
    }
    

    where:

    • vars - an instance of JMeterVariables class which provides read/write access to JMeter Variables
    • FileUtils - is the class from Apache Commons IO package which provides simple approach of reading and writing data

See How to Use BeanShell: JMeter's Favorite Built-in Component for more information on how you can overcome JMeter limitations by scripting and some form of Beanshell cookbook

Upvotes: 1

Related Questions