[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Watir: Access an Object

Mario Ruiz

1/13/2009 11:28:00 AM

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.
--
Posted via http://www.ruby-....

2 Answers

Jarmo Pertman

1/14/2009 2:44:00 PM

0

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...

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

Mario Ruiz wrote:
> 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
--
Posted via http://www.ruby-....

Michael Michael

1/14/2009 4:07:00 PM

0

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,
mmartino@apexsystemsinc.com 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!







Mario Ruiz wrote:
> 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.

--
Posted via http://www.ruby-....