Module: RWebSpec::Driver
- Includes:
- Popup, TestWisePlugin
- Included in:
- AbstractWebPage, RSpecHelper, TestScript, WebTestCase
- Defined in:
- lib/rwebspec/driver.rb
Constant Summary collapse
- @@default_polling_interval =
include Watir::ScreenCapture if RUBY_PLATFORM.downcase.include?(“mswin”) or RUBY_PLATFORM.downcase.include?(“mingw”)
1- @@default_timeout =
second
30
Instance Method Summary collapse
-
#absolutify_url(src, base_url, parent_url) ⇒ Object
change <script type=“text/javascript” src=“/javascripts/prototype.js”></script> to <script type=“text/javascript” src=“itest2.com/javascripts/prototype.js”></script>.
-
#absolutize_page(content, base_url, current_url_parent) ⇒ Object
<link rel=“stylesheet” type=“text/css” href=“/stylesheets/default.css” /> ‘<script type=“text/javascript” src=“www.jeroenwijering.com/embed/swfobject.js”></script>’ <script type=“text/javascript” src=“/javascripts/prototype.js”></script> <script type=“text/javascript” src=“/javascripts/scriptaculous.js?load=effects,builder”></script> <script type=“text/javascript” src=“/javascripts/extensions/gallery/lightbox.js”></script> <link href=“/stylesheets/extensions/gallery/lightbox.css” rel=“stylesheet” type=“text/css” /> <img src=“images/mission_48.png” />.
-
#absolutize_page_hpricot(content, base_url, parent_url) ⇒ Object
absolutize_page referencs using hpricot.
-
#ajax_wait_for_element(element_id, seconds, status = 'show', check_interval = @@default_polling_interval) ⇒ Object
Wait for specific seconds for an Ajax update finish.
-
#allow(&block) ⇒ Object
(also: #shall_allow, #allowing)
Does not provide real function, other than make enhancing test syntax.
-
#attach_browser(how, what, options = {}) ⇒ Object
Attach to existing browser window.
-
#background_visit(url, opts = {}) ⇒ Object
Go to specific url in background (i.e not via browwser, different from goto_url) This won’t share the session with what’s currenlty in browser, proxy setting.
- #basic_authentication(username, password, options = {}) ⇒ Object
- #basic_authentication_celerity(username, password) ⇒ Object
-
#basic_authentication_firefox(username, password, wait = 3) ⇒ Object
Use JSSH to pass authentication Window title “Authentication required”.
-
#basic_authentication_ie(title, username, password, options = {}) ⇒ Object
Use AutoIT3 to send password title starts with “Connect to …”.
-
#begin_at(url) ⇒ Object
Starting browser with a URL.
-
#browser ⇒ Object
return the underlying RWebSpec::Browser object.
-
#cell_with_id(cell_id, options = {}) ⇒ Object
(also: #table_data_with_id)
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.
- #check_ie_version ⇒ Object
- #choose_file_dialog(file_path) ⇒ Object
-
#clear_popup(popup_win_title, seconds = 10, yes = true) ⇒ Object
Clear popup windows such as ‘Security Alert’ or ‘Security Information’ popup window,.
-
#click_button_with_image_src_contains(image_filename) ⇒ Object
(also: #click_button_with_image)
Click image buttion with image source name.
-
#close_all_browsers ⇒ Object
Close all opening browser windows.
-
#close_browser ⇒ Object
(also: #close_ie)
Close the current browser window (started by the script).
-
#contains(str) ⇒ Object
(also: #contain)
In pages, can’t use include, text.should include(“abc”) won’t work Instead, text.should contains(“abc”.
- #contains_text(text) ⇒ Object
- #context ⇒ Object
- #default_dump_dir ⇒ Object
-
#dump_response(stream = nil) ⇒ Object
— For debugging —.
-
#element_by_id(elem_id) ⇒ Object
Identify DOM element by ID Warning: it is only supported on IE.
-
#element_text(elem_id) ⇒ Object
Warning: this does not work well with Firefox yet.
- #ends_with?(str, suffix) ⇒ Boolean
-
#enter_text_with_id(textfield_id, value) ⇒ Object
for text field can be easier to be identified by attribute “id” instead of “name”, not recommended though.
-
#expect_page(page_clazz, argument = nil) ⇒ Object
Verify the next page following an operation.
-
#failsafe(&block) ⇒ Object
(also: #fail_safe)
try operation, ignore if errors occur.
-
#firefox ⇒ Object
Return the FireWatir::Firefox instance.
-
#goto_page(page) ⇒ Object
(also: #visit)
Go to another page on the testing site.
-
#goto_url(url) ⇒ Object
Go to another web site, normally different site being tested on.
-
#ie ⇒ Object
Return the Watir::IE instance.
- #is_celerity? ⇒ Boolean
- #is_firefox? ⇒ Boolean
- #is_linux? ⇒ Boolean
- #is_mac? ⇒ Boolean
- #is_windows? ⇒ Boolean
-
#javascript_dialog ⇒ Object
Watir 1.9 way of handling javascript dialog.
-
#label_with_id(label_id, options = {}) ⇒ Object
return the text of specific (identified by attribute “id”) label tag For page containing <label id=“preferred_ide”>TestWise</label> label_with_id(“preferred_ids”) # => TestWise label_with_id(“preferred_ids”, :index => 2) # => TestWise.
- #new_popup_window(options) ⇒ Object
-
#on(page) {|page| ... } ⇒ Object
Example: on @page do |i| i.enter_text(‘btn1’) i.click_button(‘btn1’) end.
-
#open_browser(base_url = nil, options = {}) ⇒ Object
(also: #open_browser_with)
open a browser, and set base_url via hash, but does not acually.
-
#page_source ⇒ Object
current page source (in HTML).
-
#page_text ⇒ Object
return plain text view of page.
-
#page_title ⇒ Object
current web page title.
- #perform_operation(&block) ⇒ Object
-
#repeat_try(num_tries = @@default_timeout || 30, interval = @@default_polling_interval || 1, &block) ⇒ Object
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.
-
#save_current_page(options = {}) ⇒ Object
For current page souce to a file in specified folder for inspection.
- #select_file_for_upload(file_field_name, file_path) ⇒ Object
-
#shall_not_allow(&block) ⇒ Object
(also: #do_not_allow)
fail the test if user can perform the operation.
-
#span_with_id(span_id, options = {}) ⇒ Object
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.
-
#substitute_relative_path_in_src_line(line, script_src, host_url, page_parent_url) ⇒ Object
substut.
-
#support_utf8 ⇒ Object
(also: #support_unicode)
Support browser (IE) operations using unicode Example: click_button(“Google 搜索”) Reference: jira.openqa.org/browse/WTR-219.
-
#symbol_to_sequence(symb) ⇒ Object
Convert :first to 1, :second to 2, and so on…
-
#take_screenshot ⇒ Object
use win32screenshot library to save curernt active window, which shall be IE.
-
#take_screenshot_old ⇒ Object
take_screenshot to save the current active window TODO can’t move mouse.
-
#try(timeout = @@default_timeout, polling_interval = @@default_polling_interval || 1, &block) ⇒ Object
(also: #try_upto)
Try the operation up to specified timeout (in seconds), and sleep given interval (in seconds).
-
#use_current_browser(how = :title, what = /.*/) ⇒ Object
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, “TestWise”) # use browser window with title “TestWise”.
-
#wait_for_element(element_id, timeout = @@default_timeout, interval = @@default_polling_interval) ⇒ Object
Wait the element with given id to be present in web page.
-
#wait_until(timeout = @@default_timeout || 30, polling_interval = @@default_polling_interval || 1, &block) ⇒ Object
Execute the provided block until either (1) it returns true, or (2) the timeout (in seconds) has been reached.
Methods included from Popup
#check_for_popups, #check_for_security_alerts, #click_button_in_javascript_popup, #click_button_in_popup_after, #click_button_in_security_alert_popup, #click_button_in_security_information_popup, #click_popup_window, #ie_popup_clicker, #prepare_to_click_button_in_popup, #start_checking_js_dialog, #verify_alert
Methods included from TestWisePlugin
#connect_to_testwise, #debug, #dump_caller_stack, #notify_screenshot_location, #operation_delay
Instance Method Details
#absolutify_url(src, base_url, parent_url) ⇒ Object
change
<script type="text/javascript" src="/javascripts/prototype.js"></script>
to
<script type="text/javascript" src="http://itest2.com/javascripts/prototype.js"></script>
484 485 486 487 488 489 490 491 492 |
# File 'lib/rwebspec/driver.rb', line 484 def absolutify_url(src, base_url, parent_url) if src.nil? || src.empty? || src == "//:" || src =~ /\s*http:\/\//i return src end return "#{base_url}#{src}" if src =~ /^\s*\// return "#{parent_url}#{src}" if parent_url return src end |
#absolutize_page(content, base_url, current_url_parent) ⇒ Object
<link rel=“stylesheet” type=“text/css” href=“/stylesheets/default.css” /> ‘<script type=“text/javascript” src=“www.jeroenwijering.com/embed/swfobject.js”></script>’ <script type=“text/javascript” src=“/javascripts/prototype.js”></script> <script type=“text/javascript” src=“/javascripts/scriptaculous.js?load=effects,builder”></script> <script type=“text/javascript” src=“/javascripts/extensions/gallery/lightbox.js”></script> <link href=“/stylesheets/extensions/gallery/lightbox.css” rel=“stylesheet” type=“text/css” /> <img src=“images/mission_48.png” />
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 |
# File 'lib/rwebspec/driver.rb', line 443 def absolutize_page(content, base_url, current_url_parent) modified_content = "" content.each_line do |line| if line =~ /<script\s+.*src=["'']?(.*)["'].*/i then script_src = $1 substitute_relative_path_in_src_line(line, script_src, base_url, current_url_parent) elsif line =~ /<link\s+.*href=["'']?(.*)["'].*/i then link_href = $1 substitute_relative_path_in_src_line(line, link_href, base_url, current_url_parent) elsif line =~ /<img\s+.*src=["'']?(.*)["'].*/i then img_src = $1 substitute_relative_path_in_src_line(line, img_src, base_url, current_url_parent) end modified_content += line end return modified_content end |
#absolutize_page_hpricot(content, base_url, parent_url) ⇒ Object
absolutize_page referencs using hpricot
464 465 466 467 468 469 470 471 472 473 474 475 476 477 |
# File 'lib/rwebspec/driver.rb', line 464 def absolutize_page_hpricot(content, base_url, parent_url) return absolutize_page(content, base_url, parent_url) if RUBY_PLATFORM == 'java' begin require 'hpricot' doc = Hpricot(content) base_url.slice!(-1) if ends_with?(base_url, "/") (doc/'link').each { |e| e['href'] = absolutify_url(e['href'], base_url, parent_url) || "" } (doc/'img').each { |e| e['src'] = absolutify_url(e['src'], base_url, parent_url) || "" } (doc/'script').each { |e| e['src'] = absolutify_url(e['src'], base_url, parent_url) || "" } return doc.to_html rescue => e absolutize_page(content, base_url, parent_url) end end |
#ajax_wait_for_element(element_id, seconds, status = 'show', check_interval = @@default_polling_interval) ⇒ Object
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.
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 |
# File 'lib/rwebspec/driver.rb', line 677 def ajax_wait_for_element(element_id, seconds, status='show', check_interval = @@default_polling_interval) count = 0 check_interval = 1 if check_interval < 1 or check_interval > seconds while count < (seconds / check_interval) do search_indicator = @web_browser.element_by_id(element_id) search_indicator_outer_html = search_indicator.outerHtml if search_indicator if status == 'hide' return true if search_indicator_outer_html and !search_indicator_outer_html.include?('style="DISPLAY: none"') else return true if search_indicator_outer_html and search_indicator_outer_html.include?('style="DISPLAY: none"') end sleep check_interval if check_interval > 0 and check_interval < 5 * 60 # wait max 5 minutes count += 1 end return false end |
#allow(&block) ⇒ Object Also known as: shall_allow, allowing
Does not provide real function, other than make enhancing test syntax
Example:
allow { ('Register') }
626 627 628 |
# File 'lib/rwebspec/driver.rb', line 626 def allow(& block) yield end |
#attach_browser(how, what, options = {}) ⇒ Object
Attach to existing browser window
attach_browser(:title, "Page" )
attach_browser(:url, "http://wwww..." )
213 214 215 216 217 218 219 220 221 |
# File 'lib/rwebspec/driver.rb', line 213 def attach_browser(how, what, = {}) .merge!(:browser => is_firefox? ? "Firefox" : "IE") unless [:browser] begin .merge!(:base_url => browser.context.base_url) rescue => e puts "failed to set base_url, ignore : #{e}" end WebBrowser.attach_browser(how, what, ) end |
#background_visit(url, opts = {}) ⇒ Object
Go to specific url in background (i.e not via browwser, different from goto_url) This won’t share the session with what’s currenlty in browser, proxy setting
One use example: resetting database
background_visit("/reset")
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/rwebspec/driver.rb', line 192 def background_visit(url, opts = {}) require 'httpclient' begin client = HTTPClient.new if url && url =~ /^http/ http_response = client.get(url).body else base_url = $ITEST2_PROJECT_BASE_URL || $BASE_URL http_response = client.get("#{base_url}#{url}").body end http_response = http_response.content if http_response.respond_to?("content") rescue => e raise e end end |
#basic_authentication(username, password, options = {}) ⇒ Object
943 944 945 946 947 948 949 950 951 |
# File 'lib/rwebspec/driver.rb', line 943 def basic_authentication(username, password, = {}) if is_celerity? basic_authentication_celerity(username, password) elsif is_firefox? basic_authentication_firefox(username, password) else basic_authentication_ie([:title], username, password, ) end end |
#basic_authentication_celerity(username, password) ⇒ Object
939 940 941 |
# File 'lib/rwebspec/driver.rb', line 939 def basic_authentication_celerity(username, password) @web_browser.celerity.credentials = "#{username}:#{password}" end |
#basic_authentication_firefox(username, password, wait = 3) ⇒ Object
Use JSSH to pass authentication
Window title "Authentication required"
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 |
# File 'lib/rwebspec/driver.rb', line 914 def basic_authentication_firefox(username, password, wait = 3) jssh_command = " var length = getWindows().length; var win; var found = false; for (var i = 0; i < length; i++) { win = getWindows()[i]; if(win.document.title == \"Authentication Required\") { found = true; break; } } if (found) { var jsdocument = win.document; var dialog = jsdocument.getElementsByTagName(\"dialog\")[0]; jsdocument.getElementsByTagName(\"textbox\")[0].value = \"#{username}\"; jsdocument.getElementsByTagName(\"textbox\")[1].value = \"#{password}\"; dialog.getButton(\"accept\").click(); } \n" sleep(wait) $jssh_socket.send(jssh_command, 0) # read_socket() end |
#basic_authentication_ie(title, username, password, options = {}) ⇒ Object
Use AutoIT3 to send password title starts with “Connect to …”
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 |
# File 'lib/rwebspec/driver.rb', line 891 def basic_authentication_ie(title, username, password, = {}) = {:textctrl_username => "Edit2", :textctrl_password => "Edit3", :button_ok => 'Button1' } = .merge() title ||= "" if title =~ /^Connect\sto/ full_title = title else full_title = "Connect to #{title}" end require 'rformspec' login_win = RFormSpec::Window.new(full_title) login_win.send_control_text([:textctrl_username], username) login_win.send_control_text([:textctrl_password], password) login_win.("OK") end |
#begin_at(url) ⇒ Object
Starting browser with a URL
Example:
begin_at("http://www.itest2.com")
140 141 142 143 |
# File 'lib/rwebspec/driver.rb', line 140 def begin_at(url) dump_caller_stack @web_browser.begin_at(url) end |
#browser ⇒ Object
return the underlying RWebSpec::Browser object
84 85 86 |
# File 'lib/rwebspec/driver.rb', line 84 def browser @web_browser end |
#cell_with_id(cell_id, options = {}) ⇒ Object Also known as: table_data_with_id
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
555 556 557 558 559 560 561 |
# File 'lib/rwebspec/driver.rb', line 555 def cell_with_id(cell_id, = {}) if && [:index] then cell(:id => cell_id.to_s, :index => [:index]).text else cell(:id, cell_id).text end end |
#check_ie_version ⇒ Object
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 |
# File 'lib/rwebspec/driver.rb', line 869 def check_ie_version if is_windows? && @ie_version.nil? begin cmd = 'reg query "HKLM\SOFTWARE\Microsoft\Internet Explorer" /v Version'; result = `#{cmd}` version_line = nil result.each do |line| if (line =~ /Version\s+REG_SZ\s+([\d\.]+)/) version_line = $1 end end if version_line =~ /^\s*(\d+)\./ @ie_version = $1.to_i end rescue => e end end end |
#choose_file_dialog(file_path) ⇒ Object
863 864 865 866 867 |
# File 'lib/rwebspec/driver.rb', line 863 def choose_file_dialog(file_path) Watir.autoit.WinWaitActive("Choose File to Upload", '', 10) Watir.autoit.ControlSetText("Choose File to Upload", "", 1148, file_path) Watir.autoit.ControlClick("Choose File to Upload", "", "&Open") end |
#clear_popup(popup_win_title, seconds = 10, yes = true) ⇒ Object
Clear popup windows such as ‘Security Alert’ or ‘Security Information’ popup window,
Screenshot see kb2.adobe.com/cps/165/tn_16588.html
You can also by pass security alerts by change IE setting, kb.iu.edu/data/amuj.html
Example
clear_popup("Security Information", 5, true) # check for Security Information for 5 seconds, click Yes
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 |
# File 'lib/rwebspec/driver.rb', line 822 def clear_popup(popup_win_title, seconds = 10, yes = true) # commonly "Security Alert", "Security Information" if is_windows? sleep 1 autoit = WIN32OLE.new('AutoItX3.Control') # Look for window with given title. Give up after 1 second. ret = autoit.WinWait(popup_win_title, '', seconds) # # If window found, send appropriate keystroke (e.g. {enter}, {Y}, {N}). if ret == 1 then puts "about to send click Yes" if debugging? = yes ? "Button1" : "Button2" # Yes or No autoit.ControlClick(popup_win_title, '', ) end sleep(0.5) else raise "Currently supported only on Windows" end end |
#click_button_with_image_src_contains(image_filename) ⇒ Object Also known as:
Click image buttion with image source name
For an image submit button <input name=“submit” type=“image” src=“/images/search_button.gif”>
("search_button.gif")
351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/rwebspec/driver.rb', line 351 def (image_filename) perform_operation { found = nil raise "no buttons in this page" if .length <= 0 .each { |btn| if btn && btn.src && btn.src.include?(image_filename) then found = btn break end } raise "not image button with src: #{image_filename} found" if found.nil? found.click } end |
#close_all_browsers ⇒ Object
Close all opening browser windows
106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/rwebspec/driver.rb', line 106 def close_all_browsers if @web_browser if is_firefox? FireWatir::Firefox.close_all else Watir::IE.close_all end else browser_type = $ITEST2_BROWSER ? $ITEST2_BROWSER.downcase.to_sym : :ie WebBrowser.close_all_browsers(browser_type) end end |
#close_browser ⇒ Object Also known as: close_ie
Close the current browser window (started by the script). If no browser started, then close all browser windows.
92 93 94 95 96 97 98 99 100 |
# File 'lib/rwebspec/driver.rb', line 92 def close_browser if @web_browser # Old TestWise version # @web_browser.close_browser unless $ITEST2_LEAVE_BROWSER_OPEN_AFTER_RUN @web_browser.close_browser else close_all_browsers end end |
#contains(str) ⇒ Object Also known as: contain
In pages, can’t use include, text.should include(“abc”) won’t work Instead,
text.should contains("abc"
341 342 343 |
# File 'lib/rwebspec/driver.rb', line 341 def contains(str) ContainsText.new(str) end |
#contains_text(text) ⇒ Object
334 335 336 |
# File 'lib/rwebspec/driver.rb', line 334 def contains_text(text) @web_browser.contains_text(text) end |
#context ⇒ Object
132 133 134 |
# File 'lib/rwebspec/driver.rb', line 132 def context @web_browser.context end |
#default_dump_dir ⇒ Object
391 392 393 394 395 396 397 398 399 400 401 402 403 |
# File 'lib/rwebspec/driver.rb', line 391 def default_dump_dir if $ITEST2_RUNNING_SPEC_ID && $ITEST2_WORKING_DIR $ITEST2_DUMP_DIR = File.join($ITEST2_WORKING_DIR, "dump") FileUtils.mkdir($ITEST2_DUMP_DIR) unless File.exists?($ITEST2_DUMP_DIR) spec_run_id = $ITEST2_RUNNING_SPEC_ID spec_run_dir_name = spec_run_id.to_s.rjust(4, "0") unless spec_run_id == "unknown" to_dir = File.join($ITEST2_DUMP_DIR, spec_run_dir_name) else to_dir = ENV['TEMP_DIR'] || (is_windows? ? "C:\\temp" : "/tmp") end end |
#dump_response(stream = nil) ⇒ Object
For debugging
387 388 389 |
# File 'lib/rwebspec/driver.rb', line 387 def dump_response(stream = nil) @web_browser.dump_response(stream) end |
#element_by_id(elem_id) ⇒ Object
Identify DOM element by ID Warning: it is only supported on IE
380 381 382 |
# File 'lib/rwebspec/driver.rb', line 380 def element_by_id(elem_id) @web_browser.element_by_id(elem_id) end |
#element_text(elem_id) ⇒ Object
Warning: this does not work well with Firefox yet.
374 375 376 |
# File 'lib/rwebspec/driver.rb', line 374 def element_text(elem_id) @web_browser.element_value(elem_id) end |
#ends_with?(str, suffix) ⇒ Boolean
506 507 508 509 |
# File 'lib/rwebspec/driver.rb', line 506 def ends_with?(str, suffix) suffix = suffix.to_s str[-suffix.length, suffix.length] == suffix end |
#enter_text_with_id(textfield_id, value) ⇒ Object
for text field can be easier to be identified by attribute “id” instead of “name”, not recommended though
318 319 320 |
# File 'lib/rwebspec/driver.rb', line 318 def enter_text_with_id(textfield_id, value) perform_operation { text_field(:id, textfield_id).set(value) } end |
#expect_page(page_clazz, argument = nil) ⇒ Object
Verify the next page following an operation.
Typical usage:
login_page.click_login
expect_page HomePage
124 125 126 127 128 129 130 |
# File 'lib/rwebspec/driver.rb', line 124 def expect_page(page_clazz, argument = nil) if argument @web_browser.expect_page(page_clazz, argument) else @web_browser.expect_page(page_clazz) end end |
#failsafe(&block) ⇒ Object Also known as: fail_safe
try operation, ignore if errors occur
Example:
failsafe { click_link("Logout") } # try logout, but it still OK if not being able to (already logout))
637 638 639 640 641 642 |
# File 'lib/rwebspec/driver.rb', line 637 def failsafe(& block) begin yield rescue =>e end end |
#firefox ⇒ Object
Return the FireWatir::Firefox instance
153 154 155 |
# File 'lib/rwebspec/driver.rb', line 153 def firefox @web_browser.firefox end |
#goto_page(page) ⇒ Object Also known as: visit
Go to another page on the testing site.
open_browser("http://www.itest2.com")
goto_page("/demo") # visit page http://www.itest2.com/demo
170 171 172 173 174 |
# File 'lib/rwebspec/driver.rb', line 170 def goto_page(page) perform_operation { @web_browser.goto_page(page) if @web_browser } end |
#goto_url(url) ⇒ Object
Go to another web site, normally different site being tested on
open_browser("http://www.itest2.com")
goto_url("http://myorganized.info")
182 183 184 |
# File 'lib/rwebspec/driver.rb', line 182 def goto_url(url) @web_browser.goto_url url end |
#ie ⇒ Object
Return the Watir::IE instance
147 148 149 |
# File 'lib/rwebspec/driver.rb', line 147 def ie @web_browser.ie end |
#is_celerity? ⇒ Boolean
161 162 163 |
# File 'lib/rwebspec/driver.rb', line 161 def is_celerity? RUBY_PLATFORM =~ /java/ && @web_browser end |
#is_firefox? ⇒ Boolean
157 158 159 |
# File 'lib/rwebspec/driver.rb', line 157 def is_firefox? @web_browser.is_firefox? if @web_browser end |
#is_linux? ⇒ Boolean
574 575 576 |
# File 'lib/rwebspec/driver.rb', line 574 def is_linux? RUBY_PLATFORM.downcase.include?("linux") end |
#is_mac? ⇒ Boolean
566 567 568 |
# File 'lib/rwebspec/driver.rb', line 566 def is_mac? RUBY_PLATFORM.downcase.include?("darwin") end |
#is_windows? ⇒ Boolean
570 571 572 |
# File 'lib/rwebspec/driver.rb', line 570 def is_windows? RUBY_PLATFORM.downcase.include?("mswin") or RUBY_PLATFORM.downcase.include?("mingw") end |
#javascript_dialog ⇒ Object
Watir 1.9 way of handling javascript dialog
843 844 845 |
# File 'lib/rwebspec/driver.rb', line 843 def javascript_dialog @web_browser.javascript_dialog end |
#label_with_id(label_id, options = {}) ⇒ Object
return the text of specific (identified by attribute “id”) label tag For page containing
<label id="preferred_ide">TestWise</label>
label_with_id(“preferred_ids”) # => TestWise label_with_id(“preferred_ids”, :index => 2) # => TestWise
531 532 533 534 535 536 537 |
# File 'lib/rwebspec/driver.rb', line 531 def label_with_id(label_id, = {}) if && [:index] then label(:id => label_id.to_s, :index => [:index]).text else label(:id, label_id.to_s).text end end |
#new_popup_window(options) ⇒ Object
368 369 370 |
# File 'lib/rwebspec/driver.rb', line 368 def new_popup_window() @web_browser.new_popup_window() end |
#on(page) {|page| ... } ⇒ Object
Example:
on @page do |i|
i.enter_text('btn1')
i.('btn1')
end
602 603 604 |
# File 'lib/rwebspec/driver.rb', line 602 def on(page, & block) yield page end |
#open_browser(base_url = nil, options = {}) ⇒ Object Also known as: open_browser_with
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 TestWise, used as confiured
3. Use default value set
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 75 76 77 78 79 |
# File 'lib/rwebspec/driver.rb', line 36 def open_browser(base_url = nil, = {}) begin support_unicode rescue => e puts "Unicode may not work in IE, #{e}" end base_url ||= $ITEST2_PROJECT_BASE_URL base_url ||= $BASE_URL raise "base_url must be set" if base_url.nil? = {:speed => "fast", :visible => true, :highlight_colour => 'yellow', :close_others => true, :start_new => false, # start a new browser always :go => true} = .merge [:firefox] = true if "Firefox" == $ITEST2_BROWSER || "Firefox" == $BROWSER ($ITEST2_HIDE_BROWSER) ? $HIDE_IE = true : $HIDE_IE = false if base_url =~ /^file:/ uri_base = base_url else uri = URI.parse(base_url) uri_base = "#{uri.scheme}://#{uri.host}:#{uri.port}" end if [:start_new] || $celerity_loaded @web_browser = WebBrowser.new(uri_base, nil, ) else @web_browser = WebBrowser.reuse(uri_base, ) # Reuse existing browser end if base_url =~ /^file:/ goto_url(base_url) # for files, no base url else (uri.path.length == 0) ? begin_at("/") : begin_at(uri.path) if [:go] end return @web_browser end |
#page_source ⇒ Object
current page source (in HTML)
517 518 519 |
# File 'lib/rwebspec/driver.rb', line 517 def page_source @web_browser.page_source end |
#page_text ⇒ Object
return plain text view of page
522 523 524 |
# File 'lib/rwebspec/driver.rb', line 522 def page_text @web_browser.text end |
#page_title ⇒ Object
current web page title
512 513 514 |
# File 'lib/rwebspec/driver.rb', line 512 def page_title @web_browser.page_title end |
#perform_operation(&block) ⇒ Object
322 323 324 325 326 327 328 329 330 331 332 |
# File 'lib/rwebspec/driver.rb', line 322 def perform_operation(& block) begin dump_caller_stack operation_delay yield rescue RuntimeError => re raise re # ensure # puts "[DEBUG] ensure #{perform_ok}" unless perform_ok end end |
#repeat_try(num_tries = @@default_timeout || 30, interval = @@default_polling_interval || 1, &block) ⇒ Object
Try the operation up to specified times, and sleep given interval (in seconds) Error will be ignored until timeout Example
repeat_try(3, 2) { ('Search' } # 3 times, 6 seconds in total
repeat_try { ('Search' } # using default 5 tries, 2 second interval
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 |
# File 'lib/rwebspec/driver.rb', line 747 def repeat_try(num_tries = @@default_timeout || 30, interval = @@default_polling_interval || 1, & block) num_tries ||= 1 (num_tries - 1).times do |num| begin yield return rescue => e # puts "debug: #{num} failed: #{e}" sleep interval end end # last try, throw error if still fails begin yield rescue => e raise e.to_s + " after trying #{num_tries} times every #{interval} seconds" end yield end |
#save_current_page(options = {}) ⇒ Object
For current page souce to a file in specified folder for inspection
save_current_page(:dir => "C:\\mysite", filename => "abc", :replacement => true)
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
# File 'lib/rwebspec/driver.rb', line 408 def save_current_page( = {}) = {:replacement => true} = .merge() to_dir = [:dir] || default_dump_dir if [:filename] file_name = [:filename] else file_name = Time.now.strftime("%m%d%H%M%S") + ".html" end Dir.mkdir(to_dir) unless File.exists?(to_dir) file = File.join(to_dir, file_name) content = page_source base_url = @web_browser.context.base_url current_url = @web_browser.url current_url =~ /(.*\/).*$/ current_url_parent = $1 if [:replacement] && base_url =~ /^http:/ File.new(file, "w").puts absolutize_page_hpricot(content, base_url, current_url_parent) else File.new(file, "w").puts content end end |
#select_file_for_upload(file_field_name, file_path) ⇒ Object
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 |
# File 'lib/rwebspec/driver.rb', line 847 def select_file_for_upload(file_field_name, file_path) if is_windows? normalized_file_path = file_path.gsub("/", "\\") if $support_ie8 && check_ie_version && @ie_version >= 8 # puts "IE8" file_field(:name, file_field).set(normalized_file_path) # choose_file_dialog(normalized_file_path) else file_field(:name, file_field).set(normalized_file_path) end else # for firefox, just call file_field, it may fail file_field(:name, file_field).set(normalized_file_path) end end |
#shall_not_allow(&block) ⇒ Object Also known as: do_not_allow
fail the test if user can perform the operation
Example:
shall_not_allow { 1/0 }
610 611 612 613 614 615 616 617 618 |
# File 'lib/rwebspec/driver.rb', line 610 def shall_not_allow(& block) operation_performed_ok = false begin yield operation_performed_ok = true rescue end raise "Operation shall not be allowed" if operation_performed_ok end |
#span_with_id(span_id, options = {}) ⇒ Object
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
543 544 545 546 547 548 549 |
# File 'lib/rwebspec/driver.rb', line 543 def span_with_id(span_id, = {}) if && [:index] then span(:id => span_id.to_s, :index => [:index]).text else span(:id, span_id).text end end |
#substitute_relative_path_in_src_line(line, script_src, host_url, page_parent_url) ⇒ Object
substut
495 496 497 498 499 500 501 502 503 504 |
# File 'lib/rwebspec/driver.rb', line 495 def substitute_relative_path_in_src_line(line, script_src, host_url, page_parent_url) unless script_src =~ /^["']?http:/ host_url.slice!(-1) if ends_with?(host_url, "/") if script_src =~ /^\s*\// # absolute_path line.gsub!(script_src, "#{host_url}#{script_src}") else #relative_path line.gsub!(script_src, "#{page_parent_url}#{script_src}") end end end |
#support_utf8 ⇒ Object Also known as: support_unicode
Support browser (IE) operations using unicode
Example:
("Google 搜索")
Reference: jira.openqa.org/browse/WTR-219
582 583 584 585 586 587 |
# File 'lib/rwebspec/driver.rb', line 582 def support_utf8 if is_windows? require 'win32ole' WIN32OLE.codepage = WIN32OLE::CP_UTF8 end end |
#symbol_to_sequence(symb) ⇒ Object
Convert :first to 1, :second to 2, and so on…
799 800 801 802 803 804 805 806 807 808 809 810 811 812 |
# File 'lib/rwebspec/driver.rb', line 799 def symbol_to_sequence(symb) value = {:zero => 0, :first => 1, :second => 2, :third => 3, :fourth => 4, :fifth => 5, :sixth => 6, :seventh => 7, :eighth => 8, :ninth => 9, :tenth => 10}[symb] return value || symb.to_i end |
#take_screenshot ⇒ Object
use win32screenshot library to save curernt active window, which shall be IE
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 |
# File 'lib/rwebspec/driver.rb', line 978 def take_screenshot # puts "calling new take screenshot" if $testwise_screenshot_supported && is_windows? # puts "testwise supported: #{$testwise_screenshot_supported}" begin screenshot_image_filename = "screenshot_" + Time.now.strftime("%m%d%H%M%S") + ".jpg" the_dump_dir = default_dump_dir FileUtils.mkdir_p(the_dump_dir) unless File.exists?(the_dump_dir) screenshot_image_filepath = File.join(the_dump_dir, screenshot_image_filename) screenshot_image_filepath.gsub!("/", "\\") if is_windows? FileUtils.rm_f(screenshot_image_filepath) if File.exist?(screenshot_image_filepath) if is_firefox? then Win32::Screenshot::Take.of(:window, :title => /mozilla\sfirefox/i).write(screenshot_image_filepath) elsif ie Win32::Screenshot::Take.of(:window, :title => /internet\sexplorer/i).write(screenshot_image_filepath) else Win32::Screenshot::Take.of(:foreground).write(screenshot_image_filepath) end notify_screenshot_location(screenshot_image_filepath) rescue => e puts "error on taking screenshot: #{e}" end end end |
#take_screenshot_old ⇒ Object
take_screenshot to save the current active window TODO can’t move mouse
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 |
# File 'lib/rwebspec/driver.rb', line 955 def take_screenshot_old if is_windows? && $ITEST2_DUMP_PAGE begin puts "[DEBUG] Capturing screenshots..." screenshot_image_filename = "rwebspec_" + Time.now.strftime("%m%d%H%M%S") + ".jpg" the_dump_dir = default_dump_dir FileUtils.mkdir_p(the_dump_dir) unless File.exists?(the_dump_dir) screenshot_image_filepath = File.join(the_dump_dir, screenshot_image_filename) screenshot_image_filepath.gsub!("/", "\\") if is_windows? screen_capture(screenshot_image_filepath, true) notify_screenshot_location(screenshot_image_filepath) rescue puts "error: #{Failed to capture screen}" end end end |
#try(timeout = @@default_timeout, polling_interval = @@default_polling_interval || 1, &block) ⇒ Object Also known as: try_upto
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) { ('Search' } # try to click the 'Search' button upto 10 seconds, try every 2 seconds
try { ('Search' }
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 |
# File 'lib/rwebspec/driver.rb', line 776 def try(timeout = @@default_timeout, polling_interval = @@default_polling_interval || 1, & block) start_time = Time.now last_error = nil until (duration = Time.now - start_time) > timeout begin yield last_error = nil return true rescue => e last_error = e end sleep polling_interval end raise "Timeout after #{duration.to_i} seconds with error: #{last_error}." if last_error raise "Timeout after #{duration.to_i} seconds." end |
#use_current_browser(how = :title, what = /.*/) ⇒ Object
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, "TestWise") # use browser window with title "TestWise"
227 228 229 |
# File 'lib/rwebspec/driver.rb', line 227 def use_current_browser(how = :title, what = /.*/) @web_browser = WebBrowser.attach_browser(how, what) end |
#wait_for_element(element_id, timeout = @@default_timeout, interval = @@default_polling_interval) ⇒ Object
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
697 698 699 700 701 702 703 704 705 706 |
# File 'lib/rwebspec/driver.rb', line 697 def wait_for_element(element_id, timeout = @@default_timeout, interval = @@default_polling_interval) start_time = Time.now #TODO might not work with Firefox until @web_browser.element_by_id(element_id) do sleep(interval) if (Time.now - start_time) > timeout raise RuntimeError, "failed to find element: #{element_id} for max #{timeout}" end end end |
#wait_until(timeout = @@default_timeout || 30, polling_interval = @@default_polling_interval || 1, &block) ⇒ Object
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? }
657 658 659 660 |
# File 'lib/rwebspec/driver.rb', line 657 def wait_until(timeout = @@default_timeout || 30, polling_interval = @@default_polling_interval || 1, & block) waiter = Watir::Waiter.new(timeout, polling_interval) waiter.wait_until { yield } end |