Reputation: 281
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
Reputation: 168082
You can do it as follows:
Add CSS/JQuery Extractor as a child of the HTTP Request and configure it as follows:
href
a
href
-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
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 VariablesSee 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