Watir: Access an Object

M

Mario Ruiz

I need to access an object with Watir through the name without knowing
what kind of object is.
For example the control could be a button or a link and
I would like to do something like:

require 'watir'
include Watir
ie=IE.new
ie.goto("file://c:/temp2.htm")

ie.object:)name,"imp1").click()

Is there any way????
Thank you in advance.
 
J

Jarmo Pertman

Okay. I did something like this. Downside of this implementation is that
it iterates through all elements before finding the correct one. So if
you'd search for text_field, then first it would search for a button,
then for a link and just after that text_field. You can of course change
the sequence to be something else and to include some other elements.
Also, please bear in mind, that first element, which exist with these
attributes will be returned.

I'd suggest to use b.text_field:)blah, blah) instead of .element in the
future anyway, but I'll provide here one working example:

require 'watir'

class Watir::IE # monkey-patch Watir's IE class
def element *args
arg1, arg2 = args # this is needed so hashes would work as input
paremeters as with regular Watir
elements = [:button, :link, :text_field] # Watir elements which will
be searched

elements.each do |el| # iterate over each element
element = self.send(el, arg1, arg2)

return element if element.exists? # return element if it exists on
page
end
nil # return nil if element was not found
end
end

b = Watir::IE.new
b.goto "http://www.google.com"

google_text_field = b.element:)name, "q") # access text_field with two
parameters

google_button = b.element:)name, "btnG") # access it with two parameters

p google_text_field.class # outputs Watir::TextField
p google_button.class # outputs Watir::Button

google_text_field.value = "ruby" # type "ruby" into search string text
field
google_button.click # and click search

puts b.text.include?("Ruby Programming Language") # outputs true

# this only works with Watir 1.6.x+ versions
puts b.element:)text => "Ruby Programming Language", :index => 1).href #
multiple attributes, e.g. first link with specified text

Best regards,
Jarmo
 
M

Michael Michael

I don't mean to be too intrusive as I am a dreadful IT Recruiter that
you probably all despise. I am working on filling a number of RoR
Developer positions for an exciting young company in NYC. If you are
interested in learning more about the position please e-mail me,
(e-mail address removed) or call me at 212.991.1835. No one has to
know that you contacted me regarding and you can still bash me here in
the blog.

Have a great day! Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top