Class: Vapir::Browser

Inherits:
Object
  • Object
show all
Defined in:
lib/vapir-common/browser.rb

Overview

Watir is a family of open-source drivers for automating web browsers. You can use it to write tests that are easy to read and maintain.

Watir drives browsers the same way people do. It clicks links, fills in forms, presses buttons. Watir also checks results, such as whether expected text appears on a page.

The Watir family currently includes support for Internet Explorer (on Windows), Firefox (on Windows, Mac and Linux) and Safari (on Mac).

Project Homepage: wtr.rubyforge.org

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 'vapir'
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

A comprehensive summary of the Watir API can be found here wiki.openqa.org/display/WTR/Methods+supported+by+Element

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'

Constant Summary collapse

@@browser_classes =
{}
@@sub_options =
{}
@@default =
nil

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.__new__Object



59
# File 'lib/vapir-common/browser.rb', line 59

alias __new__ new

.attach(how, what) ⇒ Object

Attach to an existing browser.



92
93
94
95
96
# File 'lib/vapir-common/browser.rb', line 92

def attach(how, what)
  ensure_overridden
  set_sub_options
  klass.attach(how, what)
end

.browser_namesObject

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



144
145
146
# File 'lib/vapir-common/browser.rb', line 144

def browser_names
  @@browser_classes.keys
end

.defaultObject



135
136
137
# File 'lib/vapir-common/browser.rb', line 135

def default
  @@default
end

.default=(option) ⇒ Object

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



139
140
141
# File 'lib/vapir-common/browser.rb', line 139

def default= option
  @@default = option
end

.ensure_overriddenObject

makes sure that the class methods of Browser that call to the class methods of klass are overridden so that Browser class methods aren’t inherited causing infinite loop.



78
79
80
81
82
# File 'lib/vapir-common/browser.rb', line 78

def ensure_overridden
  if self==klass
    raise NotImplementedError, "This method must be overridden by #{self}!"
  end
end

.inherited(subclass) ⇒ Object



60
61
62
63
64
# File 'lib/vapir-common/browser.rb', line 60

def inherited(subclass)
  class << subclass
    alias new __new__
  end
end

.klassObject



107
108
109
110
111
112
113
114
# File 'lib/vapir-common/browser.rb', line 107

def klass
  key = Vapir.options[:browser]
  #eval(@@browser_classes[key]) # this triggers the autoload
  browser_class_name=@@browser_classes[key]
  klass=browser_class_name.split('::').inject(Object) do |namespace, name_part|
    namespace.const_get(name_part)
  end
end

.new(*args, &block) ⇒ Object

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



69
70
71
72
73
74
75
# File 'lib/vapir-common/browser.rb', line 69

def new *args, &block
  #set_sub_options
  browser=klass.new *args, &block
  #browser=klass.allocate
  #browser.send :initialize, *args, &block
  browser
end

.optionsObject



103
104
105
# File 'lib/vapir-common/browser.rb', line 103

def options
  self==klass ? {} : klass.options
end

.set_options(options) ⇒ Object



97
98
99
100
101
102
# File 'lib/vapir-common/browser.rb', line 97

def set_options options
  #ensure_overridden
  unless self==klass
    klass.set_options options
  end
end

.start(url) ⇒ Object

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



86
87
88
89
90
# File 'lib/vapir-common/browser.rb', line 86

def start url
  ensure_overridden
  set_sub_options
  klass.start url
end

.support(hash_args) ⇒ Object

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.



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/vapir-common/browser.rb', line 121

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

Instance Method Details

#inspectObject



177
178
179
# File 'lib/vapir-common/browser.rb', line 177

def inspect
  "#<#{self.class}:0x#{(self.hash*2).to_s(16)} " + (exists? ? "url=#{url.inspect} title=#{title.inspect}" : "exists?=false") + '>'
end

#locate(options = {}) ⇒ Object

locate is used by stuff that uses container. this doesn’t actually locate the browser but checks if it (still) exists.



171
172
173
# File 'lib/vapir-common/browser.rb', line 171

def locate(options={})
  exists?
end

#locate!(options = {}) ⇒ Object



174
175
176
# File 'lib/vapir-common/browser.rb', line 174

def locate!(options={})
  locate(options) || raise(Vapir::Exception::WindowGoneException, "The browser window seems to be gone")
end