Did you know that you are not limited by :id, :name, and :text in order to locate an element using Watir. Instead of these standard elements you are also capable of finding an element using the CSS class that the element references. For example, if you have a div that has a stylesheet class defined as foo then you can locate this div using the foo class.
To demonstrate how to do this take the following Watir script as your go by. This script was derived from the css_test script already available in the unittests folder of the 1.5.1 Watir gem folder.
C:\ruby\lib\ruby\gems\1.8\gems\watir-1.5.1.1081\unittests
There are also many tests in this folder that you should look at. To get the latest gem just download the developer watir gem and open up your cmd prompt and type gem install watir
require 'watir'
include Watir
require 'test/unit'
class CSSTest < Test::Unit::TestCase
def startup
@@ie = Watir::IE.start('http://www.csszengarden.com')
end
def ie
if defined? @@ie
@@ie
else
startup
end
end
def test_01_checkClassContents
pars = ie.getIE.document.getElementsByTagName("p")
innertext = ''
pars.each do |p|
if p.invoke("className").to_s.downcase.match(/p1/i)
innertext = p.innerText.to_s.downcase
end
end
assert(innertext.match(/^.*?we\ would\ like\ to\ see\ as\ much\ css1.*?$/))
end
end
So the above code goes out to the csszengarden site and looks for the element <p class="p1">. Once it finds this tag then it asserts that the text it contains matches the regex provided. This may not be the most practical way that you can use this ability, but it at least is a good standard template. Also, you should note the to_s.downcase that will lower the case of the text it is assigned to lower. This can be especially helpful when looking for text on a page.