Class: RWebUnit::WebTester
- Inherits:
-
Object
- Object
- RWebUnit::WebTester
- Defined in:
- lib/rwebunit/web_tester.rb
Overview
Wrapping WATIR and provide assertions
Instance Attribute Summary collapse
-
#context ⇒ Object
Returns the value of attribute context.
Class Method Summary collapse
Instance Method Summary collapse
-
#area(*args) ⇒ Object
Delegate to Watir.
- #base_url=(new_base_url) ⇒ Object
- #begin_at(relative_url) ⇒ Object
- #browser_opened? ⇒ Boolean
- #button(*args) ⇒ Object
- #buttons ⇒ Object
- #cell(*args) ⇒ Object (also: #td)
-
#check_checkbox(checkBoxName, values = nil) ⇒ Object
checkbox.
- #checkbox(*args) ⇒ Object (also: #check_box)
- #checkboxes ⇒ Object
- #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
Close the browser window.
- #close_others ⇒ Object
- #contains_text(text) ⇒ Object
- #div(*args) ⇒ 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.
- #file_field(*args) ⇒ Object
- #firefox ⇒ Object
- #focus ⇒ Object
- #form(*args) ⇒ Object
- #frame(*args) ⇒ Object
- #full_url(relative_url) ⇒ Object
- #go_back ⇒ Object
- #go_forward ⇒ Object
- #goto_page(page) ⇒ Object
- #h1(*args) ⇒ Object
- #h2(*args) ⇒ Object
- #h3(*args) ⇒ Object
- #h4(*args) ⇒ Object
- #h5(*args) ⇒ Object
- #h6(*args) ⇒ Object
- #hidden(*args) ⇒ Object
-
#ie ⇒ Object
return underlying browser.
- #image(*args) ⇒ Object
- #images ⇒ Object
-
#initialize(base_url = nil, options = {}) ⇒ WebTester
constructor
A new instance of WebTester.
- #is_firefox? ⇒ Boolean
- #label(*args) ⇒ Object
- #li(*args) ⇒ Object
- #link(*args) ⇒ Object
- #links ⇒ Object
- #map(*args) ⇒ Object
-
#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
- #paragraph(*args) ⇒ Object
- #pre(*args) ⇒ Object
- #radio(*args) ⇒ Object
- #radios ⇒ Object
- #refresh ⇒ Object
- #row(*args) ⇒ Object (also: #tr)
- #save_page(file_name = nil) ⇒ Object
- #select_file_for_upload(file_field, file_path) ⇒ Object
- #select_list(*args) ⇒ Object
- #select_lists ⇒ Object
- #select_option(selectName, option) ⇒ Object
- #span(*args) ⇒ 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.
- #table(*args) ⇒ Object
- #text_field(*args) ⇒ Object
- #text_fields ⇒ 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, options = {}) ⇒ WebTester
Returns a new instance of WebTester.
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_tester.rb', line 48 def initialize(base_url = nil, = {}) = {:speed => "zippy", :visible => true, :highlight_colour => 'yellow', :close_others => true} = .merge @context = Context.new base_url if base_url if ([:firefox] && $firewatir_loaded) || ($firewatir_loaded and !$watir_loaded) @@browser = FireWatir::Firefox.start(base_url) elsif $watir_loaded @@browser = Watir::IE.new if ENV['ITEST_EMULATE_TYPING'] == "true" && ENV['ITEST_TYPING_SPEED'] then @@browser.set_slow_speed if ENV['ITEST_TYPING_SPEED'] == 'slow' @@browser.set_fast_speed if ENV['ITEST_TYPING_SPEED'] == 'fast' else @@browser.speed = :zippy end @@browser.activeObjectHighLightColor = [:highlight_colour] @@browser.visible = [:visible] unless $HIDE_IE @@browser.close_others if [:close_others] else raise "rWebUnit initialiazation error, most likely Watir or Firewatir not present" end end |
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
46 47 48 |
# File 'lib/rwebunit/web_tester.rb', line 46 def context @context end |
Class Method Details
Instance Method Details
#area(*args) ⇒ Object
Delegate to Watir
79 80 81 |
# File 'lib/rwebunit/web_tester.rb', line 79 def area(*args) @@browser.area(*args) end |
#base_url=(new_base_url) ⇒ Object
216 217 218 219 220 221 222 |
# File 'lib/rwebunit/web_tester.rb', line 216 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
260 261 262 |
# File 'lib/rwebunit/web_tester.rb', line 260 def begin_at(relative_url) @@browser.goto full_url(relative_url) end |
#browser_opened? ⇒ Boolean
264 265 266 267 268 269 270 |
# File 'lib/rwebunit/web_tester.rb', line 264 def browser_opened? begin @@browser != nil rescue => e return false end end |
#button(*args) ⇒ Object
82 83 84 |
# File 'lib/rwebunit/web_tester.rb', line 82 def (*args) @@browser.(*args); end |
#buttons ⇒ Object
195 196 197 |
# File 'lib/rwebunit/web_tester.rb', line 195 def @@browser.; end |
#cell(*args) ⇒ Object Also known as: td
85 86 87 |
# File 'lib/rwebunit/web_tester.rb', line 85 def cell(*args) @@browser.cell(*args); end |
#check_checkbox(checkBoxName, values = nil) ⇒ Object
checkbox
368 369 370 371 372 373 374 375 376 377 |
# File 'lib/rwebunit/web_tester.rb', line 368 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 |
#checkbox(*args) ⇒ Object Also known as: check_box
89 90 91 |
# File 'lib/rwebunit/web_tester.rb', line 89 def checkbox(*args) @@browser.checkbox(*args); end |
#checkboxes ⇒ Object
201 202 203 |
# File 'lib/rwebunit/web_tester.rb', line 201 def checkboxes @@browser.checkboxes; end |
#clear_radio_option(radio_group, radio_option) ⇒ Object
396 397 398 |
# File 'lib/rwebunit/web_tester.rb', line 396 def clear_radio_option(radio_group, radio_option) radio(:name, radio_group, radio_option).clear end |
#click_button_with_caption(caption) ⇒ Object
341 342 343 |
# File 'lib/rwebunit/web_tester.rb', line 341 def () wait_before_and_after { (:caption, ).click } end |
#click_button_with_id(id) ⇒ Object
buttons
333 334 335 |
# File 'lib/rwebunit/web_tester.rb', line 333 def (id) wait_before_and_after { (:id, id).click } end |
#click_button_with_name(name) ⇒ Object
337 338 339 |
# File 'lib/rwebunit/web_tester.rb', line 337 def (name) wait_before_and_after { (:name, name).click } end |
#click_button_with_value(value) ⇒ Object
345 346 347 |
# File 'lib/rwebunit/web_tester.rb', line 345 def (value) wait_before_and_after { (:value, value).click } end |
#click_link_with_id(link_id) ⇒ Object
links
322 323 324 |
# File 'lib/rwebunit/web_tester.rb', line 322 def click_link_with_id(link_id) wait_before_and_after { link(:id, link_id).click } end |
#click_link_with_text(text) ⇒ Object
326 327 328 |
# File 'lib/rwebunit/web_tester.rb', line 326 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
392 393 394 |
# File 'lib/rwebunit/web_tester.rb', line 392 def click_radio_option(radio_group, radio_option) radio(:name, radio_group, radio_option).set end |
#close_browser ⇒ Object
Close the browser window. Useful for automated test suites to reduce test interaction.
234 235 236 237 238 239 240 241 242 |
# File 'lib/rwebunit/web_tester.rb', line 234 def close_browser if is_firefox? then puts "[debug] about to close firefox" @@browser.close else @@browser.getIE.quit end sleep 2 end |
#close_others ⇒ Object
305 306 307 |
# File 'lib/rwebunit/web_tester.rb', line 305 def close_others; @@browser.close_others; end |
#contains_text(text) ⇒ Object
170 171 172 |
# File 'lib/rwebunit/web_tester.rb', line 170 def contains_text(text) @@browser.contains_text(text); end |
#div(*args) ⇒ Object
93 94 95 |
# File 'lib/rwebunit/web_tester.rb', line 93 def div(*args) @@browser.div(*args); end |
#dump_response(stream = nil) ⇒ Object
For deubgging
461 462 463 464 465 466 467 |
# File 'lib/rwebunit/web_tester.rb', line 461 def dump_response(stream = nil) if stream.nil? puts page_source # std out else stream.puts page_source end end |
#element_by_id(elem_id) ⇒ Object
400 401 402 403 404 405 406 |
# File 'lib/rwebunit/web_tester.rb', line 400 def element_by_id(elem_id) if is_firefox? elem = 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
418 419 420 421 422 |
# File 'lib/rwebunit/web_tester.rb', line 418 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
408 409 410 411 412 413 414 415 416 |
# File 'lib/rwebunit/web_tester.rb', line 408 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
310 311 312 313 314 315 316 317 |
# File 'lib/rwebunit/web_tester.rb', line 310 def enter_text_into_field_with_name(name, text) if is_firefox? wait_before_and_after { text_field(:name, name).value = text } sleep 0.5 else wait_before_and_after { text_field(:name, name).set(text) } end end |
#file_field(*args) ⇒ Object
163 164 165 |
# File 'lib/rwebunit/web_tester.rb', line 163 def file_field(*args) @@browser.file_field(*args); end |
#firefox ⇒ Object
497 498 499 500 |
# File 'lib/rwebunit/web_tester.rb', line 497 def firefox raise "can't call this as it is configured to use IE" unless is_firefox? @@browser end |
#focus ⇒ Object
302 303 304 |
# File 'lib/rwebunit/web_tester.rb', line 302 def focus; @@browser.focus; end |
#form(*args) ⇒ Object
96 97 98 |
# File 'lib/rwebunit/web_tester.rb', line 96 def form(*args) @@browser.form(*args); end |
#frame(*args) ⇒ Object
99 100 101 |
# File 'lib/rwebunit/web_tester.rb', line 99 def frame(*args) @@browser.frame(*args); end |
#full_url(relative_url) ⇒ Object
252 253 254 255 256 257 258 |
# File 'lib/rwebunit/web_tester.rb', line 252 def full_url(relative_url) if @context && @context.base_url @context.base_url + relative_url else relative_url end end |
#go_back ⇒ Object
289 290 291 |
# File 'lib/rwebunit/web_tester.rb', line 289 def go_back; @@browser.back; end |
#go_forward ⇒ Object
292 293 294 |
# File 'lib/rwebunit/web_tester.rb', line 292 def go_forward; @@browser.forward; end |
#goto_page(page) ⇒ Object
295 296 297 |
# File 'lib/rwebunit/web_tester.rb', line 295 def goto_page(page) @@browser.goto full_url(page); end |
#h1(*args) ⇒ Object
102 103 104 |
# File 'lib/rwebunit/web_tester.rb', line 102 def h1(*args) @@browser.h1(*args); end |
#h2(*args) ⇒ Object
105 106 107 |
# File 'lib/rwebunit/web_tester.rb', line 105 def h2(*args) @@browser.h2(*args); end |
#h3(*args) ⇒ Object
108 109 110 |
# File 'lib/rwebunit/web_tester.rb', line 108 def h3(*args) @@browser.h3(*args); end |
#h4(*args) ⇒ Object
111 112 113 |
# File 'lib/rwebunit/web_tester.rb', line 111 def h4(*args) @@browser.h4(*args); end |
#h5(*args) ⇒ Object
114 115 116 |
# File 'lib/rwebunit/web_tester.rb', line 114 def h5(*args) @@browser.h5(*args); end |
#h6(*args) ⇒ Object
117 118 119 |
# File 'lib/rwebunit/web_tester.rb', line 117 def h6(*args) @@browser.h6(*args); end |
#hidden(*args) ⇒ Object
120 121 122 |
# File 'lib/rwebunit/web_tester.rb', line 120 def hidden(*args) @@browser.hidden(*args); end |
#ie ⇒ Object
return underlying browser
492 493 494 495 |
# File 'lib/rwebunit/web_tester.rb', line 492 def ie raise "can't call this as it is configured to use Firefox" if is_firefox? @@browser end |
#image(*args) ⇒ Object
123 124 125 |
# File 'lib/rwebunit/web_tester.rb', line 123 def image(*args) @@browser.image(*args); end |
#images ⇒ Object
188 189 190 |
# File 'lib/rwebunit/web_tester.rb', line 188 def images @@browser.images end |
#is_firefox? ⇒ Boolean
224 225 226 227 228 229 230 |
# File 'lib/rwebunit/web_tester.rb', line 224 def is_firefox? begin @@browser.class == FireWatir::Firefox rescue => e return false end end |
#label(*args) ⇒ Object
166 167 168 |
# File 'lib/rwebunit/web_tester.rb', line 166 def label(*args) @@browser.label(*args); end |
#li(*args) ⇒ Object
126 127 128 |
# File 'lib/rwebunit/web_tester.rb', line 126 def li(*args) @@browser.li(*args); end |
#link(*args) ⇒ Object
129 130 131 |
# File 'lib/rwebunit/web_tester.rb', line 129 def link(*args) @@browser.link(*args); end |
#links ⇒ Object
192 193 194 |
# File 'lib/rwebunit/web_tester.rb', line 192 def links @@browser.links; end |
#map(*args) ⇒ Object
132 133 134 |
# File 'lib/rwebunit/web_tester.rb', line 132 def map(*args) @@browser.map(*args); 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")
444 445 446 447 448 449 450 451 452 453 454 455 456 |
# File 'lib/rwebunit/web_tester.rb', line 444 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
174 175 176 177 |
# File 'lib/rwebunit/web_tester.rb', line 174 def page_source @@browser.html() #@@browser.document.body end |
#page_title ⇒ Object
180 181 182 183 184 185 186 |
# File 'lib/rwebunit/web_tester.rb', line 180 def page_title if is_firefox? @@browser.title else @@browser.document.title end end |
#paragraph(*args) ⇒ Object
160 161 162 |
# File 'lib/rwebunit/web_tester.rb', line 160 def paragraph(*args) @@browser.paragraph(*args); end |
#pre(*args) ⇒ Object
135 136 137 |
# File 'lib/rwebunit/web_tester.rb', line 135 def pre(*args) @@browser.pre(*args); end |
#radio(*args) ⇒ Object
138 139 140 |
# File 'lib/rwebunit/web_tester.rb', line 138 def radio(*args) @@browser.radio(*args); end |
#radios ⇒ Object
204 205 206 |
# File 'lib/rwebunit/web_tester.rb', line 204 def radios @@browser.radios; end |
#refresh ⇒ Object
298 299 300 |
# File 'lib/rwebunit/web_tester.rb', line 298 def refresh; @@browser.refresh; end |
#row(*args) ⇒ Object Also known as: tr
141 142 143 |
# File 'lib/rwebunit/web_tester.rb', line 141 def row(*args) @@browser.row(*args); end |
#save_page(file_name = nil) ⇒ Object
502 503 504 505 506 |
# File 'lib/rwebunit/web_tester.rb', line 502 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
424 425 426 |
# File 'lib/rwebunit/web_tester.rb', line 424 def select_file_for_upload(file_field, file_path) file_field(:name, file_field).set(file_path) end |
#select_list(*args) ⇒ Object
145 146 147 |
# File 'lib/rwebunit/web_tester.rb', line 145 def select_list(*args) @@browser.select_list(*args); end |
#select_lists ⇒ Object
198 199 200 |
# File 'lib/rwebunit/web_tester.rb', line 198 def select_lists @@browser.select_lists; end |
#select_option(selectName, option) ⇒ Object
349 350 351 352 |
# File 'lib/rwebunit/web_tester.rb', line 349 def select_option(selectName, option) # @@browser.selectBox(:name, selectName).select(option) select_list(:name, selectName).select(option) end |
#span(*args) ⇒ Object
149 150 151 |
# File 'lib/rwebunit/web_tester.rb', line 149 def span(*args) @@browser.span(*args); 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
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 |
# File 'lib/rwebunit/web_tester.rb', line 473 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
428 429 430 |
# File 'lib/rwebunit/web_tester.rb', line 428 def start_window(url = nil) @@browser.start_window(url); end |
#submit(buttonName = nil) ⇒ Object
submit first submit button
355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/rwebunit/web_tester.rb', line 355 def submit( = nil) if (.nil?) then .each { || next if .type != 'submit' .click return } else () end end |
#table(*args) ⇒ Object
153 154 155 |
# File 'lib/rwebunit/web_tester.rb', line 153 def table(*args) @@browser.table(*args); end |
#text_field(*args) ⇒ Object
156 157 158 |
# File 'lib/rwebunit/web_tester.rb', line 156 def text_field(*args) @@browser.text_field(*args); end |
#text_fields ⇒ Object
207 208 209 |
# File 'lib/rwebunit/web_tester.rb', line 207 def text_fields @@browser.text_fields; end |
#uncheck_checkbox(checkBoxName, values = nil) ⇒ Object
379 380 381 382 383 384 385 386 387 388 |
# File 'lib/rwebunit/web_tester.rb', line 379 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
212 213 214 |
# File 'lib/rwebunit/web_tester.rb', line 212 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.
282 283 284 285 286 |
# File 'lib/rwebunit/web_tester.rb', line 282 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.
275 276 277 |
# File 'lib/rwebunit/web_tester.rb', line 275 def wait_for_browser @@browser.waitForIE unless is_firefox? end |