[Webtest] how to check the full content of a select list?

Denis N. Antonioli Denis N. Antonioli" <denis.antonioli@canoo.com
Mon, 8 Jan 2007 09:52:49 +0100


--Apple-Mail-12-960608749
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	charset=US-ASCII;
	delsp=yes;
	format=flowed

Hi

in a previous project, I also wanted to check all select menus on a  
page.
My solution was to define the content of each menu in a file (foo.menu,
bar.menu, ...), and store all these menu definitions in the same  
directory
with a small webtest.

Each .menu file is a list of the option values, and the first line of  
the .menu
file is an xpath that select the menu on the page.
In your case, the file would look like:
//select[@name='searchLegs[0].originPoint']
none
AKL
CHC
WLG
...

The webtest called the attached groovy script.

The groovy script then verify that the number of elements in the html  
page
is the same as the number of elements in the menu definition file,  
then verify
that there is a menu entry for every line in the file.


--Apple-Mail-12-960608749
Content-Transfer-Encoding: 7bit
Content-Type: application/octet-stream;
	x-unix-mode=0755;
	name=verifyPullDowns.groovy
Content-Disposition: attachment;
	filename=verifyPullDowns.groovy

/**
 * Code fragment to compare all pull downs menus against text files.
 * To be called from the desired selection screen.
 */
import com.gargoylesoftware.htmlunit.html.xpath.HtmlUnitXPath
def ant = new AntBuilder()

def scanner = ant.fileScanner {
            fileset(dir:new File(step.project.properties.'ant.file').parentFile) {
                include(name:"*.menu")
            }
        }

def errors = new StringBuffer()
for (file in scanner) {
	step.project.log("Testing " + file)
	def menuDefinition = file.readLines();

	def baseXPath = (menuDefinition.remove(0) as String).trim()

    def size = getXpath('count(' + baseXPath + '/option)').numberValueOf(document).intValue()
	if (size != menuDefinition.size()) {
		errors.append(file.name).append(': expect ').append(menuDefinition.size())
		errors.append(', but has ').append(size).append(' options').append('\n')
	}
	menuDefinition.each { it ->
		def text = it.replace('\u00a0' as char, '\u0020' as char).trim().replaceAll("  +", " ")
		if (!getXpath(baseXPath + '/option[normalize-space(translate(text(), "\u00a0", "\u0020"))="' + text + '"]').booleanValueOf(document)) {
			errors.append(file.name).append(': missing option "').append(text).append('"\n')
		}
	}
	step.project.log(errors.toString())
}

if (errors.size() > 0) {
	throw new com.canoo.webtest.engine.StepFailedException(errors.toString(), step)
}

def getXpath(xpath) {
	step.project.log("...looking for " + xpath)
	return new HtmlUnitXPath(xpath)
}
--Apple-Mail-12-960608749
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
	charset=US-ASCII;
	delsp=yes;
	format=flowed



I assume you won't be able to use the script as is, but it could be a  
basis to
build on!

Best
	dna


On 7 janv. 07, at 12:51, John and Pip wrote:

> Hi All,
>
> What is the recommended way in WebTest to check the content of a
> select list on a page?
>
> As an example I have the list appended below (you can see it at
> https://flightbookings.airnewzealand.co.nz/isbook_en_NZ/book/ 
> initSearchForFlights.do)
> and I need to check that the correct elements appear.
>
> It will always default to the same selected value so checking for the
> literal HTML below would work, but I can not figure out how to do this
> - it seems a bit beefy for verifyText although I can make it work for
> the first line but am not sure it can handle the full list? I am
> having problems with having to escape characters.

[...]

> I realise I could use verifySelectField to check the currently
> selected option, and I could use verifyXPath probably to count the
> number of options, but neither of those is what I want.
>
> I could also perhaps use verifyElementText but I've not managed to get
> even a single line validated with this.
>
> I would appreciate any advise.
>
> regards,
>        John
>
> <select name="searchLegs[0].originPoint"
> onchange="the_fare_finder.changeInPoints();" style="width:140px;"
> class="dps"><option value="">select a city</option>
> <option value="none">----------------</option>
> <option value="AKL">Auckland</option>
>
> <option value="CHC">Christchurch</option>
> <option value="WLG">Wellington</option>
> <option value="none">----------------</option>

[...]

> </select>
> _______________________________________________
> WebTest mailing list
> WebTest@lists.canoo.com
> http://lists.canoo.com/mailman/listinfo/webtest

-- 
Any technology distinguishable from magic is insufficiently advanced.


--Apple-Mail-12-960608749--