Class: RWebUnit::WebBrowser
- Inherits:
-
Object
- Object
- RWebUnit::WebBrowser
- Defined in:
- lib/rwebunit/web_browser.rb
Overview
Wrapping WATIR IE and FireWatir Firefox
Instance Attribute Summary collapse
-
#context ⇒ Object
Returns the value of attribute context.
Class Method Summary collapse
-
.attach_browser(how, what, options = {}) ⇒ Object
Attach to existing browser.
-
.close_all_browsers ⇒ Object
TODO determine browser type, check FireWatir support or not.
- .is_windows? ⇒ Boolean
-
.new_from_existing(underlying_browser, web_context = nil) ⇒ Object
for popup windows.
- .reuse(base_url, options) ⇒ Object
Instance Method Summary collapse
-
#area(*args) ⇒ Object
FireWatir does not support area directly, treat it as text_field.
- #base_url=(new_base_url) ⇒ Object
- #begin_at(relative_url) ⇒ Object
- #browser_opened? ⇒ Boolean
-
#check_checkbox(checkBoxName, values = nil) ⇒ Object
checkbox.
- #clear_radio_option(radio_group, radio_option) ⇒ Object
- #click_button_with_caption(caption) ⇒ Object
-
#click_button_with_id(id) ⇒ Object
buttons.
- #click_button_with_name(name) ⇒ Object
- #click_button_with_value(value) ⇒ Object
-
#click_link_with_id(link_id) ⇒ Object
links.
- #click_link_with_text(text) ⇒ Object
-
#click_radio_option(radio_group, radio_option) ⇒ Object
the method is protected in JWebUnit.
-
#close_browser ⇒ Object
(also: #close)
Close the browser window.
- #contains_text(text) ⇒ Object
-
#dump_response(stream = nil) ⇒ Object
— For deubgging —.
- #element_by_id(elem_id) ⇒ Object
- #element_source(elementId) ⇒ Object
- #element_value(elementId) ⇒ Object
-
#enter_text_into_field_with_name(name, text) ⇒ Object
(also: #set_form_element, #enter_text)
text fields.
- #firefox ⇒ Object
- #full_url(relative_url) ⇒ Object
- #goto_page(page) ⇒ Object
- #goto_url(url) ⇒ Object
- #html ⇒ Object
-
#ie ⇒ Object
return underlying browser.
-
#initialize(base_url = nil, existing_browser = nil, options = {}) ⇒ WebBrowser
constructor
A new instance of WebBrowser.
- #is_firefox? ⇒ Boolean
-
#method ⇒ Object
Delegate to Watir.
-
#new_popup_window(options, browser = "ie") ⇒ Object
Attach a Watir::IE instance to a popup window.
- #page_source ⇒ Object (also: #html_body)
- #page_title ⇒ Object
- #save_page(file_name = nil) ⇒ Object
- #select_file_for_upload(file_field, file_path) ⇒ Object
- #select_option(selectName, option) ⇒ Object
-
#start_clicker(button, waitTime = 9, user_input = nil) ⇒ Object
A Better Popup Handler using the latest Watir version.
- #start_window(url = nil) ⇒ Object
-
#submit(buttonName = nil) ⇒ Object
submit first submit button.
- #text ⇒ Object
- #uncheck_checkbox(checkBoxName, values = nil) ⇒ Object
-
#url ⇒ Object
current url.
-
#wait_before_and_after ⇒ Object
A convenience method to wait at both ends of an operation for the browser to catch up.
-
#wait_for_browser ⇒ Object
Some browsers (i.e. IE) need to be waited on before more actions can be performed.
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, = {}) = {:speed => "zippy", :visible => true, :highlight_colour => 'yellow', :close_others => true} = .merge @context = Context.new base_url if base_url if (existing_browser) then @browser = existing_browser else if ([: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 = [:highlight_colour] @browser.visible = [:visible] unless $HIDE_IE @browser.close_others if [:close_others] end end |
Instance Attribute Details
#context ⇒ Object
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, ={}) = {:browser => "IE"} = .merge() site_context = Context.new([:base_url]) if [:base_url] if ([: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_browsers ⇒ Object
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
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(, web_context = nil) return WebBrowser.new(web_context ? web_context.base_url : nil, , {: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, ) if RUBY_PLATFORM.downcase.include?("mswin") && $ITEST2_BROWSER != "Firefox" Watir::IE.each do |browser_window| return WebBrowser.new(base_url, browser_window, ) end puts "no browser instance found" WebBrowser.new(base_url, nil, ) else WebBrowser.new(base_url, nil, ) 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
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 () wait_before_and_after { (:caption, ).click } end |
#click_button_with_id(id) ⇒ Object
buttons
267 268 269 |
# File 'lib/rwebunit/web_browser.rb', line 267 def (id) wait_before_and_after { (:id, id).click } end |
#click_button_with_name(name) ⇒ Object
271 272 273 |
# File 'lib/rwebunit/web_browser.rb', line 271 def (name) wait_before_and_after { (:name, name).click } end |
#click_button_with_value(value) ⇒ Object
279 280 281 |
# File 'lib/rwebunit/web_browser.rb', line 279 def (value) wait_before_and_after { (:value, value).click } end |
#click_link_with_id(link_id) ⇒ Object
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 |
#click_link_with_text(text) ⇒ Object
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_browser ⇒ Object 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) || (: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 |
#firefox ⇒ Object
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 |
#html ⇒ Object
125 126 127 |
# File 'lib/rwebunit/web_browser.rb', line 125 def html @browser.html end |
#ie ⇒ Object
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
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 |
#method ⇒ Object
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(, browser = "ie") if is_firefox? raise "not implemented" else if [:url] Watir::IE.attach(:url, [:url]) elsif [:title] Watir::IE.attach(:title, [:title]) else raise 'Please specify title or url of new pop up window' end end end |
#page_source ⇒ Object 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_title ⇒ Object
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.(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( , 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, "#{}" ) # # 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( = nil) if (.nil?) then .each { || next if .type != 'submit' .click return } else () end end |
#text ⇒ Object
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 |
#url ⇒ Object
current url
148 149 150 |
# File 'lib/rwebunit/web_browser.rb', line 148 def url @browser.url end |
#wait_before_and_after ⇒ Object
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_browser ⇒ Object
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 |