Included Modules

RWebSpec::Driver

Public Instance Methods

absolutify_url(src, base_url, parent_url) click to toggle source

change

  <script type="text/javascript" src="/javascripts/prototype.js"></script>

to

  <script type="text/javascript" src="http://itest2.com/javascripts/prototype.js"></script>
     # File lib/rwebspec/driver.rb, line 423
423:     def absolutify_url(src, base_url, parent_url)
424:       if src.nil? || src.empty? || src == "//:" || src =~ /\s*http:\/\//i
425:         return src
426:       end
427: 
428:       return "#{base_url}#{src}" if src =~ /^\s*\//
429:       return "#{parent_url}#{src}" if parent_url
430:       return src
431:     end
absolutize_page(content, base_url, current_url_parent) click to toggle source

     # File lib/rwebspec/driver.rb, line 382
382:     def absolutize_page(content, base_url, current_url_parent)
383:       modified_content = ""
384:       content.each_line do |line|
385:         if line =~ /<script\s+.*src=["'']?(.*)["'].*/i then
386:           script_src = $1
387:           substitute_relative_path_in_src_line(line, script_src, base_url, current_url_parent)
388:         elsif line =~ /<link\s+.*href=["'']?(.*)["'].*/i then
389:           link_href = $1
390:           substitute_relative_path_in_src_line(line, link_href, base_url, current_url_parent)
391:         elsif line =~ /<img\s+.*src=["'']?(.*)["'].*/i then
392:           img_src = $1
393:           substitute_relative_path_in_src_line(line, img_src, base_url, current_url_parent)
394:         end
395: 
396:         modified_content += line
397:       end
398:       return modified_content
399:     end
absolutize_page_hpricot(content, base_url, parent_url) click to toggle source

absolutize_page referencs using hpricot

     # File lib/rwebspec/driver.rb, line 403
403:     def absolutize_page_hpricot(content, base_url, parent_url)
404:       return  absolutize_page(content, base_url, parent_url) if RUBY_PLATFORM == 'java'
405:       begin
406:         require 'hpricot'
407:         doc = Hpricot(content)
408:         base_url.slice!(-1) if ends_with?(base_url, "/")
409:         (doc/'link').each { |e| e['href'] = absolutify_url(e['href'], base_url, parent_url) || ""}
410:         (doc/'img').each { |e| e['src'] = absolutify_url(e['src'], base_url, parent_url) || ""}
411:         (doc/'script').each { |e| e['src'] = absolutify_url(e['src'], base_url, parent_url) || ""}
412:         return doc.to_html
413:       rescue => e
414:         absolutize_page(content, base_url, parent_url)
415:       end
416:     end
ajax_wait_for_element(element_id, seconds, status='show', check_interval = @@default_polling_interval) click to toggle source

Wait for specific seconds for an Ajax update finish. Trick: In your Rails application,

    :loading      => "Element.show('search_indicator');
    :complete     => "Element.hide('search_indicator');

  <%= image_tag("indicator.gif", :id => 'search_indicator', :style => 'display:none') %>

Typical usage:

   ajax_wait_for_element("search_indicator", 30)
   ajax_wait_for_element("search_indicator", 30, "show")
   ajax_wait_for_element("search_indicator", 30, "hide")
   ajax_wait_for_element("search_indicator", 30, "show", 5) # check every 5 seconds

Warning: this method has not been fully tested, if you are not using Rails, change your parameter accordingly.

     # File lib/rwebspec/driver.rb, line 598
598:     def ajax_wait_for_element(element_id, seconds, status='show', check_interval = @@default_polling_interval)
599:       count = 0
600:       check_interval = 1 if check_interval < 1 or check_interval > seconds
601:       while count < (seconds / check_interval) do
602:         search_indicator = @web_browser.element_by_id(element_id)
603:         search_indicator_outer_html = search_indicator.outerHtml if search_indicator
604:         if status == 'hide'
605:           return true if search_indicator_outer_html and !search_indicator_outer_html.include?('style="DISPLAY: none"')
606:         else
607:           return true if search_indicator_outer_html and search_indicator_outer_html.include?('style="DISPLAY: none"')
608:         end
609:         sleep check_interval if check_interval > 0 and check_interval < 5 * 60 # wait max 5 minutes
610:         count += 1
611:       end
612:       return false
613:     end
allow(&block) click to toggle source

Does not provide real function, other than make enhancing test syntax

Example:

  allow { click_button('Register') }
     # File lib/rwebspec/driver.rb, line 549
549:     def allow(&block)
550:       yield
551:     end
Also aliased as: shall_allow, allowing
allowing(&block) click to toggle source

Alias for #allow

attach_browser(how, what, options = {}) click to toggle source

Attach to existinb browser window

 attach_browser(:title,    )
     # File lib/rwebspec/driver.rb, line 166
166:     def attach_browser(how, what, options = {})
167:       options.merge!(:browser => is_firefox? ? "Firefox" : "IE")
168:       begin
169:         options.merge!(:base_url => browser.context.base_url)
170:       rescue => e
171:         puts "error to attach to browser: #{e}"
172:       end
173:       WebBrowser.attach_browser(how, what, options)
174:     end
begin_at(url) click to toggle source

Starting browser with a URL

Example:

   begin_at("http://www.itest2.com")
     # File lib/rwebspec/driver.rb, line 121
121:     def begin_at(url)
122:       dump_caller_stack
123:       @web_browser.begin_at(url)
124:     end
browser() click to toggle source

return the underlying RWebSpec::Browser object

    # File lib/rwebspec/driver.rb, line 70
70:     def browser
71:       @web_browser
72:     end
cell_with_id(cell_id) click to toggle source

return the text of specific (identified by attribute “id”) ta tag For page containing

    <td id="preferred_recorder">iTest2/Watir Recorder</span>

td_with_id(“preferred_recorder“) # => iTest2/Watir Recorder

     # File lib/rwebspec/driver.rb, line 485
485:     def cell_with_id(cell_id)
486:       cell(:id, cell_id).text
487:     end
Also aliased as: table_data_with_id
click_button_with_image(image_filename) click to toggle source
click_button_with_image_src_contains(image_filename) click to toggle source

Click image buttion with image source name

For an image submit button

 click_button_with_image("search_button.gif")
     # File lib/rwebspec/driver.rb, line 292
292:     def click_button_with_image_src_contains(image_filename)
293:       dump_caller_stack
294:       operation_delay
295:       found = nil
296:       raise "no buttons in this page" if buttons.length <= 0
297:       buttons.each { |btn|
298:         if btn && btn.src  && btn.src.include?(image_filename) then
299:           found = btn
300:           break
301:         end
302:       }
303:       raise "not image button with src: #{image_filename} found" if found.nil?
304:       found.click
305:     end
Also aliased as: click_button_with_image
close_all_browsers() click to toggle source

Close all opening browser windows

    # File lib/rwebspec/driver.rb, line 92
92:     def close_all_browsers
93:       if is_firefox?
94:         FireWatir::Firefox.close_all
95:       else
96:         Watir::IE.close_all
97:       end
98:     end
close_browser() click to toggle source

Close the current browser window (started by the script). If no browser started, then close all browser windows.

    # File lib/rwebspec/driver.rb, line 78
78:     def close_browser
79:       if @web_browser
80:         # Old iTest2 version
81:         # @web_browser.close_browser unless $ITEST2_LEAVE_BROWSER_OPEN_AFTER_RUN
82:         @web_browser.close_browser
83:       else
84:         WebBrowser.close_all_browsers
85:       end
86:     end
Also aliased as: close_ie
close_ie() click to toggle source

Alias for #close_browser

contains_text(text) click to toggle source

(Not documented)

     # File lib/rwebspec/driver.rb, line 284
284:     def contains_text(text)
285:       @web_browser.contains_text(text)
286:     end
context() click to toggle source

(Not documented)

     # File lib/rwebspec/driver.rb, line 113
113:     def context
114:       @web_browser.context
115:     end
do_not_allow(&block) click to toggle source

Alias for #shall_not_allow

dump_response(stream = nil) click to toggle source

For debugging


     # File lib/rwebspec/driver.rb, line 327
327:     def dump_response(stream = nil)
328:       @web_browser.dump_response(stream)
329:     end
element_by_id(elem_id) click to toggle source

Identify DOM element by ID Warning: it is only supported on IE

     # File lib/rwebspec/driver.rb, line 320
320:     def element_by_id(elem_id)
321:       @web_browser.element_by_id(elem_id)
322:     end
element_text(elem_id) click to toggle source

Warning: this does not work well with Firefox yet.

     # File lib/rwebspec/driver.rb, line 314
314:     def element_text(elem_id)
315:       @web_browser.element_value(elem_id)
316:     end
ends_with?(str, suffix) click to toggle source

(Not documented)

     # File lib/rwebspec/driver.rb, line 445
445:     def ends_with?(str, suffix)
446:       suffix = suffix.to_s
447:       str[-suffix.length, suffix.length] == suffix
448:     end
enter_text_with_id(textfield_id, value) click to toggle source

for text field can be easier to be identified by attribute “id” instead of “name”, not recommended though

     # File lib/rwebspec/driver.rb, line 278
278:     def enter_text_with_id(textfield_id, value)
279:       dump_caller_stack
280:       operation_delay
281:       text_field(:id, textfield_id).set(value)
282:     end
expect_page(page_clazz, argument = nil) click to toggle source

Verify the next page following an operation.

Typical usage:

  login_page.click_login
  expect_page HomePage
     # File lib/rwebspec/driver.rb, line 105
105:     def expect_page(page_clazz, argument = nil)
106:       if argument
107:         page_clazz.new(@web_browser, argument)
108:       else
109:         page_clazz.new(@web_browser)
110:       end
111:     end
fail_safe(&block) click to toggle source

Alias for #failsafe

failsafe(&block) click to toggle source

try operation, ignore if errors occur

Example:

  failsafe { click_link("Logout") }  # try logout, but it still OK if not being able to (already logout))
     # File lib/rwebspec/driver.rb, line 559
559:     def failsafe(&block)
560:       begin
561:         yield
562:       rescue =>e
563:       end
564:     end
Also aliased as: fail_safe
firefox() click to toggle source

Return the FireWatir::Firefox instance

     # File lib/rwebspec/driver.rb, line 134
134:     def firefox
135:       @web_browser.firefox
136:     end
goto_page(page) click to toggle source

Go to another page on the testing site.

 open_browser("http://www.itest2.com")
 goto_page("/demo")  # visit page http://www.itest2.com/demo
     # File lib/rwebspec/driver.rb, line 148
148:     def goto_page(page)
149:       operation_delay
150:       dump_caller_stack
151:       @web_browser.goto_page(page) if @web_browser
152:     end
Also aliased as: visit
goto_url(url) click to toggle source

Go to another web site, normally different site being tested on

 open_browser("http://www.itest2.com")
 goto_url("http://myorganized.info")
     # File lib/rwebspec/driver.rb, line 159
159:     def goto_url(url)
160:       @web_browser.goto_url url
161:     end
ie() click to toggle source

Return the Watir::IE instance

     # File lib/rwebspec/driver.rb, line 128
128:     def ie
129:       @web_browser.ie
130:     end
is_firefox?() click to toggle source

(Not documented)

     # File lib/rwebspec/driver.rb, line 138
138:     def is_firefox?
139:       @web_browser.is_firefox? if @web_browser
140:     end
is_linux?() click to toggle source

(Not documented)

     # File lib/rwebspec/driver.rb, line 499
499:     def is_linux?
500:       RUBY_PLATFORM.downcase.include?("linux")
501:     end
is_mac?() click to toggle source

(Not documented)

     # File lib/rwebspec/driver.rb, line 491
491:     def is_mac?
492:       RUBY_PLATFORM.downcase.include?("darwin")
493:     end
is_windows?() click to toggle source

(Not documented)

     # File lib/rwebspec/driver.rb, line 495
495:     def is_windows?
496:       RUBY_PLATFORM.downcase.include?("mswin") or RUBY_PLATFORM.downcase.include?("mingw32")
497:     end
label_with_id(label_id) click to toggle source

return the text of specific (identified by attribute “id”) label tag For page containing

    <label id="preferred_ide">iTest2</label>

label_with_id(“preferred_ids“) # => iTest2

     # File lib/rwebspec/driver.rb, line 469
469:     def label_with_id(label_id)
470:       label(:id, label_id.to_s).text
471:     end
new_popup_window(options) click to toggle source

(Not documented)

     # File lib/rwebspec/driver.rb, line 308
308:     def new_popup_window(options)
309:       @web_browser.new_popup_window(options)
310:     end
on(page, &block) click to toggle source

Example:

 on @page do |i|
   i.enter_text('btn1')
   i.click_button('btn1')
 end
     # File lib/rwebspec/driver.rb, line 526
526:     def on(page, &block)
527:       yield page
528:     end
open_browser(base_url = nil, options = {}) click to toggle source

open a browser, and set base_url via hash, but does not acually

example:

  open_browser :base_url => http://localhost:8080

There are 3 ways to set base url

  1. pass as first argument
  2. If running using iTest2, used as confiured
  3. Use default value set
    # File lib/rwebspec/driver.rb, line 30
30:     def open_browser(base_url = nil, options = {})
31:       base_url ||= $ITEST2_PROJECT_BASE_URL
32:       base_url ||= $BASE_URL
33:       raise "base_url must be set" if base_url.nil?
34: 
35:       default_options = {:speed => "fast",
36:           :visible => true,
37:           :highlight_colour => 'yellow',
38:           :close_others => true,
39:           :start_new => false,   # start a new browser always
40:       :go => true}
41: 
42:       options = default_options.merge options
43:       options[:firefox] = true if "Firefox" == $ITEST2_BROWSER || "Firefox" == $BROWSER
44:       ($ITEST2_HIDE_BROWSER) ? $HIDE_IE = true : $HIDE_IE = false
45: 
46:       if base_url =~ /^file:/
47:         uri_base = base_url
48:       else
49:         uri = URI.parse(base_url)
50:         uri_base = "#{uri.scheme}://#{uri.host}:#{uri.port}"
51:       end
52: 
53:       if options[:start_new] || $celerity_loaded
54:         @web_browser = WebBrowser.new(uri_base, nil, options)
55:       else
56:         @web_browser = WebBrowser.reuse(uri_base, options) # Reuse existing browser
57:       end
58: 
59:       if base_url =~ /^file:/
60:         goto_url(base_url) # for files, no base url
61:       else
62:         (uri.path.length == 0) ?  begin_at("/") :  begin_at(uri.path) if options[:go]
63:       end
64: 
65:       return @web_browser
66:     end
Also aliased as: open_browser_with
open_browser_with(base_url = nil, options = {}) click to toggle source

Alias for #open_browser

page_source() click to toggle source

current page source (in HTML)

     # File lib/rwebspec/driver.rb, line 456
456:     def page_source
457:       @web_browser.page_source
458:     end
page_text() click to toggle source

return plain text view of page

     # File lib/rwebspec/driver.rb, line 461
461:     def page_text
462:       @web_browser.text
463:     end
page_title() click to toggle source

current web page title

     # File lib/rwebspec/driver.rb, line 451
451:     def page_title
452:       @web_browser.page_title
453:     end
repeat_try(num_tries = @@default_timeout || 30, interval = @@default_polling_interval || 1, &block) click to toggle source

Try the operation up to specified times, and sleep given interval (in seconds) Error will be ignored until timeout Example

   repeat_try(3, 2) { click_button('Search' } # 3 times, 6 seconds in total
   repeat_try { click_button('Search' } # using default 5 tries, 2 second interval
     # File lib/rwebspec/driver.rb, line 667
667:     def repeat_try(num_tries = @@default_timeout || 30, interval = @@default_polling_interval || 1, &block)
668:       num_tries ||= 1
669:       (num_tries - 1).times do |num|
670:         begin
671:           yield
672:           return
673:         rescue => e
674:           # puts "debug: #{num} failed: #{e}"
675:           sleep interval
676:         end
677:       end
678: 
679:       # last try, throw error if still fails
680:       begin
681:         yield
682:       rescue => e
683:         raise e.to_s + " after trying #{num_tries} times every #{interval} seconds"
684:       end
685:       yield
686:     end
save_current_page(options = {}) click to toggle source

For current page souce to a file in specified folder for inspection

  save_current_page(:dir => "C:\\mysite", filename => "abc", :replacement => true)
     # File lib/rwebspec/driver.rb, line 334
334:     def save_current_page(options = {})
335:       default_options = {:replacement => true}
336:       options = default_options.merge(options)
337:       if options[:dir]
338:         # already defined the dir
339:         to_dir = options[:dir]
340:       elsif $ITEST2_RUNNING_SPEC_ID && $ITEST2_WORKING_DIR
341: 
342:         $ITEST2_DUMP_DIR = File.join($ITEST2_WORKING_DIR, "dump")
343:         FileUtils.mkdir($ITEST2_DUMP_DIR) unless File.exists?($ITEST2_DUMP_DIR)
344: 
345:         spec_run_id = $ITEST2_RUNNING_SPEC_ID
346:         spec_run_dir_name = spec_run_id.to_s.rjust(4, "0") unless spec_run_id == "unknown"
347:         to_dir = File.join($ITEST2_DUMP_DIR, spec_run_dir_name)
348:       else
349:         to_dir = ENV['TEMP_DIR'] || (is_windows? ? "C:\\temp" : "/tmp") 
350:       end
351: 
352:       if options[:filename]
353:         file_name = options[:filename]
354:       else
355:         file_name = Time.now.strftime("%m%d%H%M%S") + ".html"
356:       end
357: 
358:       Dir.mkdir(to_dir) unless File.exists?(to_dir)
359:       file = File.join(to_dir, file_name)
360: 
361:       content = page_source
362:       base_url = @web_browser.context.base_url
363:       current_url = @web_browser.url
364:       current_url =~ /(.*\/).*$/
365:       current_url_parent = $1
366:       if options[:replacement] && base_url =~ /^http:/
367:         File.new(file, "w").puts absolutize_page_hpricot(content, base_url, current_url_parent)
368:       else
369:         File.new(file, "w").puts content
370:       end
371: 
372:     end
shall_allow(&block) click to toggle source

Alias for #allow

shall_not_allow(&block) click to toggle source

fail the test if user can perform the operation

Example:

 shall_not_allow { 1/0 }
     # File lib/rwebspec/driver.rb, line 534
534:     def shall_not_allow(&block)
535:       operation_performed_ok = false
536:       begin
537:         yield
538:         operation_performed_ok  = true
539:       rescue
540:       end
541:       raise "Operation shall not be allowed" if operation_performed_ok
542:     end
Also aliased as: do_not_allow
span_with_id(span_id) click to toggle source

return the text of specific (identified by attribute “id”) span tag For page containing

    <span id="preferred_recorder">iTest2/Watir Recorder</span>

span_with_id(“preferred_recorder“) # => iTest2/Watir Recorder

     # File lib/rwebspec/driver.rb, line 477
477:     def span_with_id(span_id)
478:       span(:id, span_id).text
479:     end
substitute_relative_path_in_src_line(line, script_src, host_url, page_parent_url) click to toggle source

substut

     # File lib/rwebspec/driver.rb, line 434
434:     def substitute_relative_path_in_src_line(line, script_src, host_url, page_parent_url)
435:       unless script_src =~ /^["']?http:/
436:         host_url.slice!(-1) if ends_with?(host_url, "/")
437:         if script_src =~ /^\s*\// # absolute_path
438:           line.gsub!(script_src, "#{host_url}#{script_src}")
439:         else  #relative_path
440:           line.gsub!(script_src, "#{page_parent_url}#{script_src}")
441:         end
442:       end
443:     end
support_unicode() click to toggle source

Alias for #support_utf8

support_utf8() click to toggle source

Support browser (IE) operations using unicode

 Example:
  click_button("Google 搜索")

Reference: jira.openqa.org/browse/WTR-219

     # File lib/rwebspec/driver.rb, line 507
507:     def support_utf8
508:       if is_windows?
509:         require 'win32ole'
510:         WIN32OLE.codepage = WIN32OLE::CP_UTF8
511:       end
512:     end
Also aliased as: support_unicode
symbol_to_sequence(symb) click to toggle source

Convert :first to 1, :second to 2, and so on...

     # File lib/rwebspec/driver.rb, line 717
717:     def symbol_to_sequence(symb)
718:       value = { :zero => 0,
719:           :first => 1,
720:           :second => 2,
721:           :third => 3,
722:           :fourth => 4,
723:           :fifth => 5,
724:           :sixth => 6,
725:           :seventh => 7,
726:           :eighth => 8,
727:           :ninth => 9,
728:           :tenth => 10 }[symb]
729:       return value || symb.to_i
730:     end
table_data_with_id(cell_id) click to toggle source

Alias for #cell_with_id

try(timeout = @@default_timeout, polling_interval = @@default_polling_interval || 1, &block) click to toggle source

Try the operation up to specified timeout (in seconds), and sleep given interval (in seconds). Error will be ignored until timeout Example

   try { click_link('waiting')}
   try(10, 2) { click_button('Search' } # try to click the 'Search' button upto 10 seconds, try every 2 seconds
   try { click_button('Search' }
     # File lib/rwebspec/driver.rb, line 696
696:     def try(timeout = @@default_timeout, polling_interval = @@default_polling_interval || 1, &block)
697:       start_time = Time.now
698: 
699:       last_error = nil
700:       until (duration = Time.now - start_time) > timeout
701:         begin
702:           return if yield
703:           last_error = nil
704:         rescue => e
705:           last_error = e
706:         end
707:         sleep polling_interval
708:       end
709: 
710:       raise "Timeout after #{duration.to_i} seconds with error: #{last_error}." if last_error
711:       raise "Timeout after #{duration.to_i} seconds."
712:     end
Also aliased as: try_upto
try_upto(timeout = @@default_timeout, polling_interval = @@default_polling_interval || 1, &block) click to toggle source

Alias for #try

use_current_browser(how = :title, what = /.*/) click to toggle source

Reuse current an opened browser window instead of opening a new one

  example:
    use_current_browser(:title, /.*/) # use what ever browser window
    use_current_browser(:title, "iTest2") # use browser window with title "iTest2"
     # File lib/rwebspec/driver.rb, line 180
180:     def use_current_browser(how = :title, what = /.*/)
181:       @web_browser = WebBrowser.attach_browser(how, what)
182:     end
visit(page) click to toggle source

Alias for #goto_page

wait_for_element(element_id, timeout = @@default_timeout, interval = @@default_polling_interval) click to toggle source

Wait the element with given id to be present in web page

 Warning: this not working in Firefox, try use wait_util or try instead
     # File lib/rwebspec/driver.rb, line 618
618:     def wait_for_element(element_id, timeout = @@default_timeout, interval = @@default_polling_interval)
619:       start_time = Time.now
620:       #TODO might not work with Firefox
621:       until @web_browser.element_by_id(element_id) do
622:         sleep(interval)
623:         if (Time.now - start_time) > timeout
624:           raise RuntimeError, "failed to find element: #{element_id} for max #{timeout}"
625:         end
626:       end
627:     end
wait_until(timeout = @@default_timeout || 30, polling_interval = @@default_polling_interval || 1, &block) click to toggle source

Execute the provided block until either (1) it returns true, or (2) the timeout (in seconds) has been reached. If the timeout is reached, a TimeOutException will be raised. The block will always execute at least once.

This does not handle error, if the given block raise error, the statement finish with error Examples:

  wait_until {puts 'hello'}
  wait_until { div(:id, :receipt_date).exists? }
     # File lib/rwebspec/driver.rb, line 578
578:     def wait_until(timeout = @@default_timeout || 30, polling_interval = @@default_polling_interval || 1, &block)
579:       waiter = Watir::Waiter.new(timeout, polling_interval)
580:       waiter.wait_until { yield }
581:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.