Class: RWebUnit::WebBrowser

Inherits:
Object
  • Object
show all
Defined in:
lib/rwebunit/web_browser.rb

Overview

Wrapping WATIR IE and FireWatir Firefox

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url = nil, existing_browser = nil, options = {}) ⇒ WebBrowser

Returns a new instance of WebBrowser.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rwebunit/web_browser.rb', line 36

def initialize(base_url = nil, existing_browser = nil, options = {})
  default_options = {:speed => "zippy", :visible => true,
  :highlight_colour => 'yellow',  :close_others => true}
  options = default_options.merge options
  @context = Context.new base_url if base_url

  if (existing_browser) then
    @browser = existing_browser
  else
    if (options[:firefox] &&  $firewatir_loaded) || ($firewatir_loaded and !$watir_loaded)
      # JSSH is running, 9997
      begin
        require 'net/telnet'
        firefox_jssh = Net::Telnet::new("Host" => "127.0.0.1", "Port" => 9997)
        FireWatir::Firefox.firefox_started = true
      rescue => e
        # puts "debug: XXX #{e}"
        sleep 1
      end
      @browser = FireWatir::Firefox.start(base_url)
    elsif $watir_loaded
      @browser = Watir::IE.new
    end
  end

  raise "rWebUnit initialiazation error, most likely Watir or Firewatir not present" if @browser.nil?
  if  $watir_loaded && @browser.class == Watir::IE
    if $ITEST2_EMULATE_TYPING  &&  $ITEST2_TYPING_SPEED then
      @browser.set_slow_speed if $ITEST2_TYPING_SPEED == 'slow'
      @browser.set_fast_speed if $ITEST2_TYPING_SPEED == 'fast'
    else
      @browser.speed = :zippy
    end
    @browser.activeObjectHighLightColor = options[:highlight_colour]
    @browser.visible = options[:visible] unless $HIDE_IE
    @browser.close_others if options[:close_others]
  end

end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



34
35
36
# File 'lib/rwebunit/web_browser.rb', line 34

def context
  @context
end

Class Method Details

.attach_browser(how, what, options = {}) ⇒ Object

Attach to existing browser

Usage:

WebBrowser.attach_browser(:title, "iTest2")
WebBrowser.attach_browser(:url, "http://www.itest2.com")
WebBrowser.attach_browser(:url, "http://www.itest2.com", {:browser => "Firefox", :base_url => "http://www.itest2.com"})
WebBrowser.attach_browser(:title, /agileway\.com\.au\/attachment/)  # regular expression


374
375
376
377
378
379
380
381
382
383
# File 'lib/rwebunit/web_browser.rb', line 374

def self.attach_browser(how, what, options={})
  default_options = {:browser => "IE"}
  options = default_options.merge(options)
  site_context = Context.new(options[:base_url]) if options[:base_url]
  if (options[:browser] == "Firefox")
    return WebBrowser.new_from_existing(FireWatir::Firefox.new.attach(how, what), site_context)
  else
    return WebBrowser.new_from_existing(Watir::IE.attach(how, what), site_context)
  end
end

.close_all_browsersObject

TODO determine browser type, check FireWatir support or not



181
182
183
184
185
186
187
# File 'lib/rwebunit/web_browser.rb', line 181

def self.close_all_browsers
  if RUBY_PLATFORM.downcase.include?("mswin")
    Watir::IE.close_all
  else
    # raise "not supported in FireFox yet."
  end
end

.is_windows?Boolean

Returns:

  • (Boolean)


450
451
452
# File 'lib/rwebunit/web_browser.rb', line 450

def self.is_windows?
  RUBY_PLATFORM.downcase.include?("mswin")
end

.new_from_existing(underlying_browser, web_context = nil) ⇒ Object

for popup windows



89
90
91
# File 'lib/rwebunit/web_browser.rb', line 89

def self.new_from_existing(underlying_browser, web_context = nil)
  return WebBrowser.new(web_context ? web_context.base_url : nil, underlying_browser, {:close_others => false})
end

.reuse(base_url, options) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rwebunit/web_browser.rb', line 76

def self.reuse(base_url, options)
  if RUBY_PLATFORM.downcase.include?("mswin") && $ITEST2_BROWSER != "Firefox"
    Watir::IE.each do |browser_window|
      return WebBrowser.new(base_url, browser_window, options)
    end
    puts "no browser instance found"
    WebBrowser.new(base_url, nil, options)
  else
    WebBrowser.new(base_url, nil, options)
  end
end

Instance Method Details

#area(*args) ⇒ Object

FireWatir does not support area directly, treat it as text_field



107
108
109
110
111
112
113
# File 'lib/rwebunit/web_browser.rb', line 107

def area(*args)
  if is_firefox?
    text_field(*args)
  else
    @browser.send("area", *args)
  end
end

#base_url=(new_base_url) ⇒ Object



152
153
154
155
156
157
158
# File 'lib/rwebunit/web_browser.rb', line 152

def base_url=(new_base_url)
  if @context
    @conext.base_url = new_base_url
    return
  end
  @context = Context.new base_url
end

#begin_at(relative_url) ⇒ Object



197
198
199
# File 'lib/rwebunit/web_browser.rb', line 197

def begin_at(relative_url)
  @browser.goto full_url(relative_url)
end

#browser_opened?Boolean

Returns:

  • (Boolean)


201
202
203
204
205
206
207
# File 'lib/rwebunit/web_browser.rb', line 201

def browser_opened?
  begin
    @browser != nil
  rescue => e
    return false
  end
end

#check_checkbox(checkBoxName, values = nil) ⇒ Object

checkbox



301
302
303
304
305
306
307
308
309
310
# File 'lib/rwebunit/web_browser.rb', line 301

def check_checkbox(checkBoxName, values=nil)
  if values
    values.class == Array ? arys = values : arys = [values]
    arys.each {|cbx_value|
      checkbox(:name, checkBoxName, cbx_value).set
    }
  else
    checkbox(:name, checkBoxName).set
  end
end

#clear_radio_option(radio_group, radio_option) ⇒ Object



329
330
331
# File 'lib/rwebunit/web_browser.rb', line 329

def clear_radio_option(radio_group, radio_option)
  radio(:name, radio_group, radio_option).clear
end

#click_button_with_caption(caption) ⇒ Object



275
276
277
# File 'lib/rwebunit/web_browser.rb', line 275

def click_button_with_caption(caption)
  wait_before_and_after { button(:caption, caption).click }
end

#click_button_with_id(id) ⇒ Object

buttons



267
268
269
# File 'lib/rwebunit/web_browser.rb', line 267

def click_button_with_id(id)
  wait_before_and_after { button(:id, id).click }
end

#click_button_with_name(name) ⇒ Object



271
272
273
# File 'lib/rwebunit/web_browser.rb', line 271

def click_button_with_name(name)
  wait_before_and_after { button(:name, name).click }
end

#click_button_with_value(value) ⇒ Object



279
280
281
# File 'lib/rwebunit/web_browser.rb', line 279

def click_button_with_value(value)
  wait_before_and_after { button(:value, value).click }
end

links



256
257
258
# File 'lib/rwebunit/web_browser.rb', line 256

def click_link_with_id(link_id)
  wait_before_and_after { link(:id, link_id).click }
end


260
261
262
# File 'lib/rwebunit/web_browser.rb', line 260

def click_link_with_text(text)
  wait_before_and_after { link(:text, text).click }
end

#click_radio_option(radio_group, radio_option) ⇒ Object

the method is protected in JWebUnit



325
326
327
# File 'lib/rwebunit/web_browser.rb', line 325

def click_radio_option(radio_group, radio_option)
  radio(:name, radio_group, radio_option).set
end

#close_browserObject Also known as: close

Close the browser window. Useful for automated test suites to reduce test interaction.



170
171
172
173
174
175
176
177
# File 'lib/rwebunit/web_browser.rb', line 170

def close_browser
  if is_firefox? then
    @browser.close
  else
    @browser.getIE.quit
  end
  sleep 2
end

#contains_text(text) ⇒ Object



115
116
117
# File 'lib/rwebunit/web_browser.rb', line 115

def contains_text(text)
  @browser.contains_text(text);
end

#dump_response(stream = nil) ⇒ Object


For deubgging




406
407
408
# File 'lib/rwebunit/web_browser.rb', line 406

def dump_response(stream = nil)
  stream.nil? ?  puts(page_source) : stream.puts(page_source)
end

#element_by_id(elem_id) ⇒ Object



333
334
335
336
337
338
339
340
# File 'lib/rwebunit/web_browser.rb', line 333

def element_by_id(elem_id)
  if is_firefox?
    # elem = @browser.document.getElementById(elem_id)
    elem = div(:id, elem_id) || label(:id, elem_id)  || button(:id, elem_id) || span(:id, elem_id) || hidden(:id, elem_id) || link(:id, elem_id) || radio(:id, elem_id)
  else
    elem = @browser.document.getElementById(elem_id)
  end
end

#element_source(elementId) ⇒ Object



352
353
354
355
356
# File 'lib/rwebunit/web_browser.rb', line 352

def element_source(elementId)
  elem = element_by_id(elementId)
  assert_not_nil(elem, "HTML element: #{elementId} not exists")
  elem.innerHTML
end

#element_value(elementId) ⇒ Object



342
343
344
345
346
347
348
349
350
# File 'lib/rwebunit/web_browser.rb', line 342

def element_value(elementId)
  if is_firefox? then
    elem = element_by_id(elementId)
    elem ? elem.invoke('innerText') : nil
  else
    elem = element_by_id(elementId)
    elem ? elem.invoke('innerText') : nil
  end
end

#enter_text_into_field_with_name(name, text) ⇒ Object Also known as: set_form_element, enter_text

text fields



244
245
246
247
248
249
250
251
# File 'lib/rwebunit/web_browser.rb', line 244

def enter_text_into_field_with_name(name, text)
  if is_firefox?
    wait_before_and_after { text_field(:name, name).value = text }
    sleep 0.3
  else
    wait_before_and_after { text_field(:name, name).set(text) }
  end
end

#firefoxObject



438
439
440
441
# File 'lib/rwebunit/web_browser.rb', line 438

def firefox
  raise "can't call this as it is configured to use IE" unless is_firefox?
  @browser
end

#full_url(relative_url) ⇒ Object



189
190
191
192
193
194
195
# File 'lib/rwebunit/web_browser.rb', line 189

def full_url(relative_url)
  if @context && @context.base_url
    @context.base_url + relative_url
  else
    relative_url
  end
end

#goto_page(page) ⇒ Object



235
236
237
# File 'lib/rwebunit/web_browser.rb', line 235

def goto_page(page)
  @browser.goto full_url(page);
end

#goto_url(url) ⇒ Object



239
240
241
# File 'lib/rwebunit/web_browser.rb', line 239

def goto_url(url)
  @browser.goto url
end

#htmlObject



125
126
127
# File 'lib/rwebunit/web_browser.rb', line 125

def html
  @browser.html
end

#ieObject

return underlying browser



433
434
435
436
# File 'lib/rwebunit/web_browser.rb', line 433

def ie
  raise "can't call this as it is configured to use Firefox" if is_firefox?
  @browser
end

#is_firefox?Boolean

Returns:

  • (Boolean)


160
161
162
163
164
165
166
# File 'lib/rwebunit/web_browser.rb', line 160

def is_firefox?
  begin
    @browser.class == FireWatir::Firefox
  rescue => e
    return false
  end
end

#methodObject

Delegate to Watir



97
98
99
100
101
# File 'lib/rwebunit/web_browser.rb', line 97

[:button, :cell, :checkbox, :div, :form, :frame, :h1, :h2, :h3, :h4, :h5, :h6, :hidden, :image, :li, :link, :map, :pre, :row, :radio, :select_list, :span, :table, :text_field, :paragraph, :file_field, :label].each do |method|
  define_method method do |*args|
    @browser.send(method, *args)
  end
end

#new_popup_window(options, browser = "ie") ⇒ Object

Attach a Watir::IE instance to a popup window.

Typical usage

new_popup_window(:url => "http://www.google.com/a.pdf")


389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/rwebunit/web_browser.rb', line 389

def new_popup_window(options, browser = "ie")
  if is_firefox?
    raise "not implemented"
  else
    if options[:url]
      Watir::IE.attach(:url, options[:url])
    elsif options[:title]
      Watir::IE.attach(:title, options[:title])
    else
      raise 'Please specify title or url of new pop up window'
    end
  end
end

#page_sourceObject Also known as: html_body



119
120
121
122
# File 'lib/rwebunit/web_browser.rb', line 119

def page_source
  @browser.html()
  #@browser.document.body
end

#page_titleObject



133
134
135
136
137
138
139
# File 'lib/rwebunit/web_browser.rb', line 133

def page_title
  if is_firefox?
    @browser.title
  else
    @browser.document.title
  end
end

#save_page(file_name = nil) ⇒ Object



443
444
445
446
447
# File 'lib/rwebunit/web_browser.rb', line 443

def save_page(file_name = nil)
  file_name ||= Time.now.strftime("%Y%m%d%H%M%S") + ".html"
  puts "about to save page: #{File.expand_path(file_name)}"
  File.open(file_name, "w").puts page_source
end

#select_file_for_upload(file_field, file_path) ⇒ Object



358
359
360
361
# File 'lib/rwebunit/web_browser.rb', line 358

def select_file_for_upload(file_field, file_path)
  normalized_file_path = RUBY_PLATFORM.downcase.include?("mswin") ? file_path.gsub("/", "\\") : file_path
  file_field(:name, file_field).set(normalized_file_path)
end

#select_option(selectName, option) ⇒ Object



283
284
285
# File 'lib/rwebunit/web_browser.rb', line 283

def select_option(selectName, option)
  select_list(:name, selectName).select(option)
end

#start_clicker(button, waitTime = 9, user_input = nil) ⇒ Object

A Better Popup Handler using the latest Watir version. Posted by [email protected]

wiki.openqa.org/display/WTR/FAQ#FAQ-HowdoIattachtoapopupwindow%3F



414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/rwebunit/web_browser.rb', line 414

def start_clicker( button, waitTime= 9, user_input=nil)
  # get a handle if one exists
  hwnd = @browser.enabled_popup(waitTime)
  if (hwnd)  # yes there is a popup
    w = WinClicker.new
    if ( user_input )
      w.setTextValueForFileNameField( hwnd, "#{user_input}" )
    end
    # I put this in to see the text being input it is not necessary to work
    sleep 3
    # "OK" or whatever the name on the button is
    w.clickWindowsButton_hwnd( hwnd, "#{button}" )
    #
    # this is just cleanup
    w = nil
  end
end

#start_window(url = nil) ⇒ Object



363
364
365
# File 'lib/rwebunit/web_browser.rb', line 363

def start_window(url = nil)
  @browser.start_window(url);
end

#submit(buttonName = nil) ⇒ Object

submit first submit button



288
289
290
291
292
293
294
295
296
297
298
# File 'lib/rwebunit/web_browser.rb', line 288

def submit(buttonName = nil)
  if (buttonName.nil?) then
    buttons.each { |button|
      next if button.type != 'submit'
      button.click
      return
    }
  else
    click_button_with_name(buttonName)
  end
end

#textObject



129
130
131
# File 'lib/rwebunit/web_browser.rb', line 129

def text
  @browser.text
end

#uncheck_checkbox(checkBoxName, values = nil) ⇒ Object



312
313
314
315
316
317
318
319
320
321
# File 'lib/rwebunit/web_browser.rb', line 312

def uncheck_checkbox(checkBoxName, values = nil)
  if values
    values.class == Array ? arys = values : arys = [values]
    arys.each {|cbx_value|
      checkbox(:name, checkBoxName, cbx_value).clear
    }
  else
    checkbox(:name, checkBoxName).clear
  end
end

#urlObject

current url



148
149
150
# File 'lib/rwebunit/web_browser.rb', line 148

def url
  @browser.url
end

#wait_before_and_afterObject

A convenience method to wait at both ends of an operation for the browser to catch up.



219
220
221
222
223
# File 'lib/rwebunit/web_browser.rb', line 219

def wait_before_and_after
  wait_for_browser
  yield
  wait_for_browser
end

#wait_for_browserObject

Some browsers (i.e. IE) need to be waited on before more actions can be performed. Most action methods in Watir::Simple already call this before and after.



212
213
214
# File 'lib/rwebunit/web_browser.rb', line 212

def wait_for_browser
  @browser.waitForIE unless is_firefox?
end