[Webtest] filters

Paul King webtest@lists.canoo.com
Sat, 20 May 2006 22:36:38 +1000

Hi Brian, I would probably use some scripting (e.g. Groovy)
to do what you are after. You can optionally use filters
to select just a minimal part of your page - this may simplify
the scripting code slightly. Invoke your first page, do the
scripting and store the server names into a list. Invoke the
second page, do the scripting and store into a list. Then
compare the two lists.

If you use the Groovy step, the lists will stay in the binding
across your Groovy steps (since build 1276). If you are using
scriptStep, then you will need to have the attribute keep="true"
to have the binding carry over.

Your script code could look something like:

	<invoke url="${testUrl}"/>
	<selectLinesFilter startRegex=".*Servers Added.*" includeStart="no"
	    stopRegex=".*Servers Modified.*" includeStop="no"/>
	<scriptStep language="groovy"><![CDATA[
	    servers = [ ]
	    root = new XmlSlurper().parseText(document.asXml())
	    root.depthFirst().findAll{ node ->
		node.text().contains('Server') }.each{ lines ->
		    lines.toString().split('\n').findAll{ str ->
			str.trim().size() > 0}.each{ servers << it.trim() }}
	    assert servers.unique().size() == 3

This was run on the following HTML:

<h1>A Heading to ignore</h1>
<p>Servers Added:</p><br/>
<p>Servers Modified</p>

If your text is a little more structured you will be able to shorten
the script quite a lot.

Cheers, Paul.

Brian Spindler wrote:
> Guys/Gals,
> I've been looking at the filter steps and believe they are what I need however I am having a hard time finding all the logic I need so I hoped someone could quickly point me in the right direction.  This is a simplified example of the HTML that I might want to filter:
> <p> Servers Added:</p><br/>
> Server1<br/>
> Server2<br/>
> Server3<br/>
> <p> Servers Modified</p>
> I was using a selectLinesFilter to try and capture the "Server1<br/>Server2<br/>Server3<br/>" however what I am trying to accomplish is to extract the servers and compare them to the results of another HTML page.  The reason I think this is going to get difficult is because the other page the server names will be within <td></td> tags.  Does this mean I need to extract the server names individually to where I confirm there existence one by one with verifyText steps?
> Any help would be greatly appreciated.
> Thanks!
> -Brian