Retrieving a random Option from HTML DropDown using Grails Functional Testing

21 / Apr / 2009 by Uday Pratap Singh 1 comments

I have recently started using the marvelous plugin Grails function Testing by Marc Palmer (http://www.grails.org/Grails+Functional+Testing).I would like to share few things with you about this plugin.
The purpose of testing get defeated if we keep on doing for the same values. The method below let you to do it for the different values each time.

def countryList = byName(’country’)
int randomIndex=new Random().nextInt(
countryList.getOptionSize())
selects[’country’].select countryList.getOption(randomIndex).getValueAttribute()

byName() retrieve the element from the current page by its name attribute. Throws exception if multiple elements found with same name and null if no element found
In above example I am retrieving the select element of current page which has the name attribute ‘country’.
getOption() takes the integer as a parameter and return the option element of the current page at index 2
getValueAttribute() useful to get what the value need to be passed as params to the server. This method assures that value is passed rather than the name displyed in the list.

I hope this would help some of you.

Hope it helps
Uday Pratap Singh
uday@intelligrape.com
https://twitter.com/meudaypratap

FOUND THIS USEFUL? SHARE IT

comments (1 “Retrieving a random Option from HTML DropDown using Grails Functional Testing”)

Leave a Reply

Your email address will not be published. Required fields are marked *