just_add_watir | Advanced recipes for Watir
26Sep/08Off

Q. How do I learn all the objects on a page? [firewatir]

Sometimes you might wish to implement a QTP-like 'object repository'. I've had most success implementing this via Ruby modules.

To 'learn' all the objects on a page using firewatir you can use a simple script like this:

require 'rubygems'
require 'firewatir'
 
class NilClass
  def length
    0
  end
end
 
class DiscoverObjects
  include FireWatir
 
  def initialize(url)
    if url
      # attach to specific url
      @b = Firefox.start(url)
    else
      # attach to existing window
      @b = Firefox.new
    end
  end
 
  def all
    @objects = {}
    [:links, :text_fields, :buttons, :select_lists].each do |type|
      @b.send(type).each do |element|
        @objects[element] = {}
        @objects[element][:type] = type.to_s[0..-2]
        # omitted :href, :url, :class
        [:id, :name, :value, :text, :index, :xpath, :title, :action, :src, :for].each do |attrib|
          value = element.send(attrib)
          @objects[element][attrib] = value unless value.empty?
        end
      end
    end
  end
 
  def print_objects
    puts "module #{@b.title.to_s.gsub(/[^\d\w]/,'').capitalize}"
    puts "# Objects learned from: #{@b.url}\n# #{Time.now}"
 
    @objects.each do |key,val|
      val.each do |k,v|
          unless k.to_s =~ /type/
            method_name = "#{val[:type]}_#{k}_#{v.to_s.gsub(/[^\d\w]/,'_').downcase}"
            puts "\tdef #{method_name.gsub('__','_').gsub(/_$/,'')}"
            puts "\t\treturn @b.#{val[:type]}(:#{k}, \"#{v}\")\n\tend\n\n"
          end
      end
    end
    puts "end"
  end
 
end
 
url = ARGV[0]
discover = DiscoverObjects.new(url)
discover.all
discover.print_objects

Which will produce results like this:

module Firefoxwebbrowserfastermoresecurecustomizable
# Objects learned from: http://www.mozilla.com/en-US/firefox/
# Sat Sep 27 07:40:08 +1000 2008
	def link_text_qmo
		return @b.link(:text, "QMO")
	end
 
	def link_text_press_center
		return @b.link(:text, "Press Center")
	end
 
	def link_text_other_systems_and_languages
		return @b.link(:text, "Other Systems and Languages")
	end
end

Feel free to modify to suit (for watir or the like). I develop on a Mac, so firewatir is easiest for me. The manual way is to just use the IE Developer Toolbar or Firebug depending on your platform.

Comments (2) Trackbacks (0)
  1. Hi,
    Can you pls tell a way to handle javascript pop-up in firewatir? I did it fine with IE using enabled_popup method, but its not recognizing this method for Firewatir. Pls suggest. Ineed to click on the OK button in pop-up.
    thnaks in advance

  2. WHen i run the DiscoverObjects on firefox , error info :
    c:/ruby/lib/ruby/gems/1.8/gems/firewatir-1.6.2/lib/firewatir/firefox.rb:271:in `set_defaults’: Unable to connect to machine : 127.0.0.1 on port 9997. Make sure that JSSh is properly installed and Firefox is running with ‘-jssh’ option (Watir::Exception::UnableToStartJSShException)
    from c:/ruby/lib/ruby/gems/1.8/gems/firewatir-1.6.2/lib/firewatir/firefox.rb:161:in `initialize’
    from c:/ruby/lib/ruby/gems/1.8/gems/firewatir-1.6.2/lib/firewatir/firefox.rb:177:in `new’
    from c:/ruby/lib/ruby/gems/1.8/gems/firewatir-1.6.2/lib/firewatir/firefox.rb:177:in `start’
    from discoyobj.rb:16:in `initialize’
    from discoyobj.rb:57:in `new’

    please help me to resolve this problem,thanks!

Trackbacks are disabled.