Watir: Access an Object

Discussion in 'Ruby' started by Mario Ruiz, Jan 13, 2009.

  1. Mario Ruiz

    Mario Ruiz Guest

    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-forum.com/.
     
    Mario Ruiz, Jan 13, 2009
    #1
    1. Advertising

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

    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-forum.com/.
     
    Jarmo Pertman, Jan 14, 2009
    #2
    1. Advertising

  3. Ruby Opportunities

    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,
    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-forum.com/.
     
    Michael Michael, Jan 14, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vani
    Replies:
    1
    Views:
    162
    Jeff Wood
    Oct 26, 2005
  2. Sean Nakasone

    watir to access combobox

    Sean Nakasone, Sep 22, 2007, in forum: Ruby
    Replies:
    2
    Views:
    106
  3. Anukul Singhal
    Replies:
    1
    Views:
    168
    Charley Baker
    May 15, 2008
  4. Devabrat Kumar
    Replies:
    0
    Views:
    264
    Devabrat Kumar
    Jun 9, 2008
  5. Pradeepta Swain

    watir , access is denied

    Pradeepta Swain, Jun 27, 2008, in forum: Ruby
    Replies:
    5
    Views:
    190
    Pradeepta Swain
    Jul 1, 2008
Loading...

Share This Page