JavaScript
How do I trigger JavaScript events?
If the tag contains a JavaScript call, you can use the "fire_event" method to trigger it. ex. onchange=doThis() or onmouseup=clearForm()
To trigger a text field named "my_field" with an onchange event we would do this with Watir:
ie.text_field(:name, "my_field").fire_event("onchange")
How do I interact with a JavaScript tree view?
Watir now supports JavaScript treeviews such as the Joust tree view
.
It's important to find out which frame the tree view is in, which can be done using "show_frames". Once we know that, we can use the image "index" attribute to find the plus images to expand the menu.
Here is an example using IRB (Start > Run > IRB):
irb(main):001:0> require 'watir'
=> true
irb(main):002:0> ie = Watir::IE.new
=> #<Watir::IE:0x2df00e8 @ie=#<WIN32OLE:0x2df0088>, @defaultSleepTime=0.1, @error_ch x02ab0568@c:/ruby/lib/ruby/site_ruby/1.8/watir.rb:1213>\], @logger=#<Watir::DefaultLogger=#<Logger::LogDevice:0x2deffc8 @dev=#<IO:0x2a6e980>, @shift_size=nil, @level=2, @datetime_format="%d-%b-%Y %H:%M:%S", @progname=nil>, @typingspeed jectHighLightColor="yellow", @enable_spinner=false, @url_list=\[\], @form=nil>
irb(main):003:0> ie.goto("http:)
=> 3.422
irb(main):004:0> ie.frame("menu").link(:text , /wel/i).flash
=> 10
irb(main):005:0> ie.frame("menu").image(:index, 1).flash
=> 10
irb(main):006:0> ie.frame("menu").image(:index, 2).flash
=> 10
irb(main):007:0> ie.frame("menu").image(:index, 3).flash
=> 10
irb(main):008:0> ie.frame("menu").image(:index, 3).click
=> nil
irb(main):009:0> ie.frame("menu").link(:text, "Who is using it?").flash
=> 10
We used "flash" to find the image we wanted which was the first "plus" sign. Once we found it using index (at index 3), we clicked it to expand it. Once it was expanded, we were able to interact with elements under that tree node.