How to get WIN32OLE handle for IE via watir-webdriver?

1.3k views Asked by At

In Watir you can get WIN32OLE handle of IE window using next method.

irb(main):059:0> browser.ie
=> #<WIN32OLE:0x28d12b8>

I need somehow to get same return for IE that was created by watir-webdriver.

Is there some way? Or at least someone can point me the direction to dig.

I need this stuff to attach HTTPwatch plugin to my browser instance. Here is example of HTTPWatch code.

require 'watir'
require 'win32ole'
browser = Watir::Browser.new
controller = WIN32OLE.new('HttpWatch.Controller')
plugin = controller.IE.Attach(browser.ie)

UPD: Thanks to Justin Ko I have working code

require 'win32ole'
require 'watir-webdriver'

browser = Watir::Browser.new :ie
title = browser.title
browser.goto "google.com"

length = WIN32OLE.new('Shell.Application').Windows.count - 1

(0..length).each do |i|
begin
       WIN32OLE.new('Shell.Application').Windows(i).Document.Title
       $ie = WIN32OLE.new('Shell.Application').Windows(i)
    rescue
    end
end

controller = WIN32OLE.new('HttpWatch.Controller')
plugin = controller.IE.Attach($ie)
1

There are 1 answers

1
Justin Ko On BEST ANSWER

You could try using the WIN32OLE to attach to the running instance of IE. This was discussed on the Ruby On Windows blog - see here.

I think the code you would need is:

require 'win32ole'
require 'watir-webdriver'

browser = Watir::Browser.new :ie
title = browser.title

for window in WIN32OLE.new('Shell.Application').Windows
    begin
        if window.Document.Title == title
            ie = window
        end
    rescue
    end
end

controller = WIN32OLE.new('HttpWatch.Controller')
plugin = controller.IE.Attach(ie)

I do not have HttpWatch, so I was unable to test it. However, the win32ole type appears to be the same type as that returned by Watir's browser.ie().

Note that this solution assumes that the browser has a unique title. If this assumption is not valid, I can write up some workarounds.