Showing posts with label ie. Show all posts
Showing posts with label ie. Show all posts

Tuesday, May 22, 2007

The Shell Windows Collection of Internet Explorer Objects

As mentioned previously, you cannot use the WIN32OLE.connect method to connect to a running instance of Internet Explorer, as you would do, for example, with Excel or Word. I'll explain now how to do this via the Windows Shell.

First, here's the code snippet, which grabs an instance of IE that has this blog displayed...


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

The Windows Shell object includes a Windows method which returns a collection of all of the open windows that belong to the Shell...

shell = WIN32OLE.new('Shell.Application')
windows = shell.Windows

This is actually a collection of Internet Explorer objects, though some of these may be Internet Explorer web browser windows and others may be Windows Explorer windows.

To get a count of the number of windows, call the Count method...

windows.Count

To reference a member of this collection by index, call the Item method, passing it the (zero-based) index...

first_window = windows.Item(0)

Internet Explorer windows will normally have a Document object, which will have a Title property, so to find the IE window that you want to work with, iterate over the Windows collection and check the Document.Title value...

for window in windows
begin
if window.Document.Title =~ /Ruby on Windows/
ie = window
end
rescue
end
end

...and now you have your IE application object to work with as previously discussed.

Note that not all Shell Window objects will have a Document object, so (in the example above) wrapping the code within a begin... rescue... end block would handle the error that occurs with non-IE windows.

Make sense? Let me know if you have questions or comments, and thanks for stopping by!

Sunday, May 20, 2007

Automating Internet Explorer with Ruby: Without Watir

In a previous post, I discussed how to use the watir library to automate Microsoft Internet Explorer. I should also mention that you can also do this directly, using the win32ole library. Let's take a look at how to do this...

First, require the win32ole library...


require 'win32ole'

Create a new instance of Internet Explorer...

ie = WIN32OLE.new('InternetExplorer.Application')

(NOTE: You cannot use the WIN32OLE.connect method to connect to a running instance of Internet Explorer, as you would do, for example, with Excel or Word. I'll explain later how to do this via the Windows Shell. Stay tuned...)

You can show or hide the IE window by setting the Visible property...

ie.Visible = true

Navigate to a URL using the Navigate method...

ie.Navigate('http://google.com')

Wait until IE has completed loading the page, by checking the ReadyState property...

sleep(1) until ie.ReadyState == 4

When a web page is loaded into IE, the contents of the page are represented by the Document object. The document object's All method returns a collection of all elements within the document, such as input textboxes, links, buttons, etc. You may reference a document element by name, which you will find in the HTML code for that page. For example, viewing the source for the google.com top page shows us, among others things, that we have a textbox named 'q' and a button named 'btnG'...

input maxlength=2048 name=q size=55 title="Google Search" value=""
input name=btnG type=submit value="Google Search"

Let's enter a value into the textbox named 'q' by setting its Value property...

ie.Document.All.q.Value = 'ruby on windows'

...and then click the button named 'btnG', by calling its Click method...

ie.Document.All.btnG.click

...and wait until IE has completed loading the page...

sleep(1) until ie.ReadyState == 4

The Document.All.Tags method returns a collection of all elements with the specified HTML tag, such as 'a', 'tr', or 'input'. So, to get a collection of all links, you could do this...

links = ie.Document.All.Tags('a')

You could then iterate over this collection...

for link in links do
puts link.InnerText # print the link's text
puts link.href # print the link's URL
end

To quit IE, call the application object's Quit method...

ie.Quit

There is much more to automating IE than I what I have presented here, but this will hopefully get you started. As always, feel free to post a comment here or email me if there is a specific task or topic that you would like to see discussed here.

This is all about helping you make better use of Ruby on Windows.

Thanks for stopping by!


Digg my article

Thursday, May 3, 2007

Automating Internet Explorer with Ruby: Watir

A reader has asked about automating Internet Explorer (IE) with Ruby. While you can do this using the win32ole library, I strongly recommend using the watir library instead.

WATIR (pronounced "water") stands for "Web Application Testing in Ruby". Watir is "a free, open-source functional testing tool for automating browser-based tests of web applications." Though it's designed for driving IE to test web applications, Watir is also very handy for automating IE in non-test scenarios. It leverages the win32ole library under the hood, but provides objects and methods that simplify the process.

To use Watir, first install the watir gem. Open up a Command Prompt and type the following...


gem install watir

...then, in your script, require the watir library...

require 'watir'

To create an instance of IE and navigate to a website...

ie = Watir::IE.new
ie.goto('http://rubyonwindows.blogspot.com')

To click on a link...

ie.link(:text, "win32ole").click
ie.link(:url, 'http://rubyonwindows.blogspot.com/search/label/win32ole').click

To input text into a text field...

ie.text_field(:name, 'login').set('MyLogin')

To select an option from a drop-down list...

ie.select_list( :name , "HallOfFame").select("Dave Concepcion")

To click a button...

ie.button(:value, 'Submit').click

To close the browser, call the close method...

ie.close

That's the basics. For further details and examples, you'll definitely want to check out the Watir User Guide.

For the complete Watir API reference, check out the rdocs here.

Various extensions for Watir have popped up, including WatirRecorder, WatirMaker, and WET Web Tester. Google for further details and updated links.

As always, post a comment here or send me an email with further questions or to request a topic for discussion.

Thanks for stopping by!

Digg my article