Today I created a handy tool for Watir script developers: iehook.rb
The general idea is:
- user loads iehook.rb
- a list of IE sessions is returned
- user enters session number required
- $ie global variable is now available
The Watir developer can now use the IRB session to interact with page elements in real-time, via watir commands applied to the $ie object. Here’s a IRB session using iehook:
irb(main):001:0> load 'iehook.rb'
0 http://giantrobots.thoughtbot.com/2008/12/23/script-console-tips
1 http://www.live.com/
2 http://www.google.com/search?hl=&cat=&meta=&num=&ie=utf-8&q=windows+powershell
3 file:///C:/Users/mememe/Documents
enter session # 1
$ie attached to >> 1 http://www.live.com/
=> true
irb(main):002:0> $ie.send_keys("abc")
=>nil
irb(main):003:0>
Here is the actual Ruby source code.
# iehook.rb :
# Interactive Ruby script
# Finds all existing IE sessions, attaches to selected
#
# Usage : from command prompt
# irb -r iehook.rb
#
# Usage : from within IRB
# load 'iehook.rb'
#
# Version Date Author Comment
# 1.0 2009-02-26 R. Papesch huzzah!
#
require 'watir'
require 'win32ole'
shell = WIN32OLE.new('Shell.Application')
windows = shell.Windows
i = 0
windows.each {|w| puts i.to_s + " " + w.LocationURL; i+=1 }
print "enter session # "
x = gets.chomp.to_i
w_url = windows.Item(x).LocationURL
$ie = Watir::IE.attach(:url, w_url)
puts "$ie attached to >> " + x.to_s + " " + $ie.url