Friday, April 9, 2010

Watir: How to identify Selection Boxes?

Watir sets or clears an item in a selection box (or dropdown box) by looking at the attributes available in the

id Attribute

Watir code to set a select box item using the id attribute:
ie.select_list(:id, "one").set("is fun")

name Attribute

Watir code to set a select box item using the name attribute:
ie.select_list(:name, "selectme").set("is fun")

Selection Box Methods

Watir code to clear a select box item using the id attribute:
ie.select_list(:id, "one").clearSelection
Watir code to get the contents of a select list:
contents = ie.select_list(:id, "one").getAllContents
NOTE: contents will be an array

Select Multiple

Some select lists can have multiple selections instead of just one. If multiple items can be selected in select box, Watir can set or clear an item.
What you see in the web browser:
This is the tag in the HTML source:

You can set individual options using successive _set_s and you can clear everything that is selected with the clearSelection method. The following code would select every option in the select list and then clear everything.
ie.select_list(:id, 'one').set('Web Testing')
ie.select_list(:id, 'one').set('in Ruby')
ie.select_list(:id, 'one').set('is fun')
ie.select_list(:id, 'one').clearSelection

1 comment: