Friday, November 20, 2009

Randomly Clicking links in JMeter (sometimes known as spidering)

A follow up to Spidering a site with JMeter
A user on the JMeter mailing list posted his solution using the HTML link parser[1] to spider a site. The spidering consists of clicking a link at random from the links parsed from the last accessed page.
The test looks like
Script available at Spider.jmx
The Initial Request is used by the HTML Link Parser to get the initial set of urls from which one will be chosen.
The While controllers condition is simply true , since we want it to loop forever.
The Spider HTTP Sampler has a path of .*.
The If Controller has a condition ${__javaScript(!${JMeterThread.last_sample_ok})}
This simply checks if the last sample fetched failed (by .* or because it fetched a CGI/PDF which cant be parsed for links and if so reexecutes the Initial Request.)
There are numerous tweaks you can implement , you might not execute the initial request, it might be one at random that you pick , or the last successful request. You might choose to check the request being made to restrict the paths.

Note that this clicks a link at random from a set of links acquired from the previously clicked page. This cannot ensure that a link is not repeated and cannot ensure that all links are fetched.

[1] http://jakarta.apache.org/jmeter/usermanual/component_reference.html#HTML_Link_Parser

Wednesday, November 11, 2009

Dependent tests in JMeter (kind of)

A common use case in testing is the concept of dependent tests(except for the unit test fanatics who love JUnit and didn't realise they needed this functionality till TestNG came along). One of the requirements then becomes that these dependent test should not execute if the test that it depends on fails. To implement this in JMeter we need to use the following two pieces of information
The variable JMeterThread.last_sample_ok is set to "true" or "false" after all assertions for a sampler have been run. [1]
If Controller -Evaluate for all children - Should condition be evaluated for all children? If not checked, then the condition is only evaluated on entry.[2]

Combining the two bits of information we have
Thread Group
If Controller((${JMeterThread.last_sample_
ok}) with Evaluate for all children = checked
Req 1 --> if this errors req 1 and req 2 wont be executed
Req 2 --> if this errors , req 3 wont be executed
Req 3
Note that any assertion failing would also mark the request as failed.
Note also that you cannot have nested dependent sets but you could flatten them out as separate IF controllers.

[1] http://jakarta.apache.org/jmeter/usermanual/component_reference.html#assertions
[2] http://jakarta.apache.org/jmeter/usermanual/component_reference.html#If_Controller