[Webtest] Maintaining the variable state in repeat loop

Manish Ranade Manish Ranade <manish.ranade@gmail.com>
Sun, 11 Dec 2011 14:36:21 -0800


I have a test that tests all the links on a given page by clicking on
them. I use clickLink to click. There is one more piece of logic where
I am checking for a link url pattern and if it matches a given pattern
then I dont want to click on that link more than three times. Reason
for doing this is I have more than 500 links that have the same url
except for some different url params, so I dont want to test them all.
Below is my code and the problem I am facing.


<repeat xpath="//a" counterName="link" description="Click on each link
on the HomePage" >
	<storeXPath description="Link Url" xpath="$link/@href" property="linkUrl" />
	<scriptStep language="groovy">
		def articlePageUrlPattern = /.*ArticlePage\?id=[1-9]+.*/;
		try {
		} catch(e) {
			articlePageCount = 0;
		if( !("#{linkUrl}" ==~ articlePageUrlPattern) ) {
			step.setWebtestProperty('clickLink', 'true');
		} else {
			step.setWebtestProperty('clickLink', 'false');
			if(articlePageCount > 3) {
				step.setWebtestProperty('clickLink', 'false');
			} else {
				step.setWebtestProperty('clickLink', 'true');

	<ifStep test="#{clickLink}">
		<clickLink xpath="$link[@href]" description="#{linkUrl}"/>


The problem I am facing:

It always goes in a catch block and sets articlePageCount to zero and
then test ends up testing all the links with that pattern. How do I
solve this problem? It seems link in every iteration of <repeat>,
Groovy code block behaves like it doesnt know what happened in the
previous iternation. I have also tried setting articlePageCount
outside of <repeat> inside of a separate <ScriptStep> Groovy block.
That doesnt work.

Any thoughts? Something I am doing wrong?

Manish Ranade
Phone: 408-596-6546