Module Watir::Browser

  1. commonwatir/lib/watir/browser.rb

This Browser module provides a generic interface that tests can use to access any browser.

The actual browser (and thus the actual Watir driver) is determined at runtime based on configuration settings.

require 'watir'
browser = Watir::Browser.new
browser.goto 'http://google.com'
browser.text_field(:name, 'q').set 'pickaxe'
browser.button(:name, 'btnG').click
if browser.text.include? 'Programming Ruby'
  puts 'Text was found'
else
  puts 'Text was not found'
end

There are two ways to configure the browser that will be used by your tests.

One is to set the watir_browser environment variable to ie or firefox. (How you do this depends on your platform.)

The other is to create a file that looks like this.

browser: ie

And then to add this line to your script, after the require statement and before you invoke Browser.new.

Watir.options_file = 'path/to/the/file/you/just/created'

Public class methods

attach (how, what)

Attach to an existing browser.

[show source]
# File commonwatir/lib/watir/browser.rb, line 53
      def attach(how, what)
        set_sub_options
        klass.attach(how, what)
      end
browser_names ()

Returns the names of the browsers that are supported by this module. These are the options for ‘watir_browser’ (env var) or ‘browser:’ (yaml).

[show source]
# File commonwatir/lib/watir/browser.rb, line 99
      def browser_names
        @@browser_classes.keys
      end
default ()
[show source]
# File commonwatir/lib/watir/browser.rb, line 90
      def default
        @@default
      end
default= (option)

Specifies a default browser. Must be specified before options are parsed.

[show source]
# File commonwatir/lib/watir/browser.rb, line 94
      def default= option
        @@default = option
      end
new ()

Create a new instance of a browser driver, as determined by the configuration settings. (Don’t be fooled: this is not actually an instance of Browser class.)

[show source]
# File commonwatir/lib/watir/browser.rb, line 42
      def new
        set_sub_options
        klass.new
      end
options ()
[show source]
# File commonwatir/lib/watir/browser.rb, line 61
      def options
        return {} unless defined?(klass.options)
        klass.options
      end
set_options (options)
[show source]
# File commonwatir/lib/watir/browser.rb, line 57
      def set_options options
        return unless defined?(klass.set_options)
        klass.set_options options
      end
start (url)

Create a new instance as with new and start the browser on the specified url.

[show source]
# File commonwatir/lib/watir/browser.rb, line 48
      def start url
        set_sub_options
        klass.start url
      end
support (hash_args)

Add support for the browser option, using the specified class, provided as a string. Optionally, additional options supported by the class can be specified as an array of symbols. Options specified by the user and included in this list will be passed (as a hash) to the set_options class method (if defined) before creating an instance.

[show source]
# File commonwatir/lib/watir/browser.rb, line 76
      def support hash_args
        option = hash_args[:name]
        class_string = hash_args[:class]
        additional_options = hash_args[:options]
        library = hash_args[:library]
        gem = hash_args[:gem] || library

        @@browser_classes[option] = class_string        
        @@sub_options[option] = additional_options

        autoload class_string, library
        activate_gem gem, option
      end