class RWebSpec::WebBrowser
Wrapping WATIR IE and FireWatir Firefox
Attributes
Public Class Methods
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
# File lib/rwebspec-watir/web_browser.rb, line 499 def self.attach_browser(how, what, options={}) default_options = {:browser => "IE"} options = default_options.merge(options) site_context = Context.new(options[:base_url]) if options[:base_url] return WebBrowser.new_from_existing(Watir::IE.attach(how, what), site_context) end
# File lib/rwebspec-webdriver/web_browser.rb, line 375 def self.close_all_browsers puts "[WARN] not supported yet in RWebSpec-WebDriver" end
is it running in MS Windows platforms?
# File lib/rwebspec-watir/web_browser.rb, line 582 def self.is_windows? RUBY_PLATFORM.downcase.include?("mswin") or RUBY_PLATFORM.downcase.include?("mingw") end
# File lib/rwebspec-watir/web_browser.rb, line 16 def initialize(base_url = nil, existing_browser = nil, options = {}) default_options = {:speed => "zippy", :visible => true, :highlight_colour => 'yellow', :close_others => true } options = default_options.merge options @context = Context.new base_url if base_url initialize_ie_browser(existing_browser, options) end
for popup windows
# File lib/rwebspec-watir/web_browser.rb, line 69 def self.new_from_existing(underlying_browser, web_context = nil) return WebBrowser.new(web_context ? web_context.base_url : nil, underlying_browser, {:close_others => false}) end
# File lib/rwebspec-watir/web_browser.rb, line 53 def self.reuse(base_url, options) if self.is_windows? && ($TESTWISE_BROWSER != "Firefox" && $TESTWISE_BROWSER != "Firefox") require 'watir-classic' # try to avoid # lib/ruby/1.8/dl/win32.rb:11:in `sym': unknown type specifier 'v' Watir::IE.each do |browser_window| return WebBrowser.new(base_url, browser_window, options) end #puts "no browser instance found" WebBrowser.new(base_url, nil, options) else WebBrowser.new(base_url, nil, options) end end
Public Instance Methods
# File lib/rwebspec-watir/web_browser.rb, line 88 def area(*args) @browser.send("area", *args) end
TODO can't browse back if on invalid page
# File lib/rwebspec-webdriver/web_browser.rb, line 433 def back @browser.navigate.back end
# File lib/rwebspec-watir/web_browser.rb, line 206 def base_url=(new_base_url) if @context @conext.base_url = new_base_url return end @context = Context.new base_url end
# File lib/rwebspec-watir/web_browser.rb, line 238 def begin_at(relative_url) @browser.goto full_url(relative_url) end
# File lib/rwebspec-watir/web_browser.rb, line 242 def browser_opened? begin @browser != nil rescue => e return false end end
Check a checkbox Usage:
check_checkbox("agree") check_checkbox("agree", "true")
# File lib/rwebspec-watir/web_browser.rb, line 396 def check_checkbox(checkBoxName, values=nil) if values values.class == Array ? arys = values : arys = [values] arys.each {|cbx_value| if Watir::VERSION =~ /^1/ then checkbox(:name, checkBoxName, cbx_value).set else checkbox(:name => checkBoxName, :value => cbx_value).set end } else checkbox(:name, checkBoxName).set end end
# File lib/rwebspec-webdriver/web_browser.rb, line 311 def checkboxes @browser.find_elements(:xpath, "//input[@type='checkbox']") end
Clear a radio button
Usage: click_radio_option("country", "Australia")
# File lib/rwebspec-watir/web_browser.rb, line 446 def clear_radio_option(radio_group, radio_option) if Watir::VERSION =~ /^2/ then radio(:name => radio_group, :value => radio_option).clear else radio(:name, radio_group, radio_option).clear end end
links
# File lib/rwebspec-watir/web_browser.rb, line 308 def click_link_with_id(link_id, opts = {}) if opts && opts[:index] wait_before_and_after { link(:id => link_id, :index => opts[:index]).click } else wait_before_and_after { link(:id, link_id).click } end end
# File lib/rwebspec-watir/web_browser.rb, line 316 def click_link_with_text(text, opts = {}) if opts && opts[:index] wait_before_and_after { link(:text => text, :index => opts[:index]).click } else wait_before_and_after { link(:text, text).click } end end
Click a radio button
Usage: click_radio_option("country", "Australia")
# File lib/rwebspec-watir/web_browser.rb, line 434 def click_radio_option(radio_group, radio_option) if Watir::VERSION =~ /^1/ then radio(:name, radio_group, radio_option).set else radio(:name => radio_group, :value => radio_option).set end end
# File lib/rwebspec-watir/web_browser.rb, line 226 def close_all_browsers(browser_type = :ie) @browser.windows.each(&:close) end
Close the browser window. Useful for automated test suites to reduce test interaction.
# File lib/rwebspec-watir/web_browser.rb, line 220 def close_browser @browser.close sleep 2 end
# File lib/rwebspec-watir/web_browser.rb, line 164 def contains_text(text) @browser.contains_text(text); end
current url
# File lib/rwebspec-webdriver/web_browser.rb, line 333 def current_url @browser.current_url end
# File lib/rwebspec-webdriver/web_browser.rb, line 328 def divs @browser.find_elements(:tag_name, "divs") end
# File lib/rwebspec-webdriver/web_browser.rb, line 346 def driver @browser end
For deubgging
# File lib/rwebspec-watir/web_browser.rb, line 527 def dump_response(stream = nil) stream.nil? ? puts(page_source) : stream.puts(page_source) end
This is the main method for accessing a generic element with a given attibute
* how - symbol - how we access the element. Supports all values except :index and :xpath * what - string, integer or regular expression - what we are looking for,
Valid values for 'how' are listed in the Watir Wiki - wiki.openqa.org/display/WTR/Methods+supported+by+Element
returns an Watir::Element object
Typical Usage
element(:class, /foo/) # access the first element with class 'foo'. We can use a string in place of the regular expression element(:id, "11") # access the first element that matches an id
# File lib/rwebspec-watir/web_browser.rb, line 108 def element(how, what) return @browser.element(how, what) end
Deprecated: using Watir style directly instead
# File lib/rwebspec-watir/web_browser.rb, line 456 def element_by_id(elem_id) if is_firefox? # elem = @browser.document.getElementById(elem_id) # elem = div(:id, elem_id) || label(:id, elem_id) || button(:id, elem_id) || # span(:id, elem_id) || hidden(:id, elem_id) || link(:id, elem_id) || radio(:id, elem_id) elem = browser.element_by_xpath("//*[@id='#{elem_id}']") else elem = @browser.document.getElementById(elem_id) end end
# File lib/rwebspec-watir/web_browser.rb, line 472 def element_source(elementId) elem = element_by_id(elementId) assert_not_nil(elem, "HTML element: #{elementId} not exists") elem.innerHTML end
# File lib/rwebspec-watir/web_browser.rb, line 467 def element_value(elementId) elem = element_by_id(elementId) elem ? elem.invoke('innerText') : nil end
this is the main method for accessing generic html elements by an attribute
Returns a HTMLElements object
Typical usage:
elements(:class, 'test').each { |l| puts l.to_s } # iterate through all elements of a given attribute elements(:alt, 'foo')[1].to_s # get the first element of a given attribute elements(:id, 'foo').length # show how many elements are foung in the collection
# File lib/rwebspec-watir/web_browser.rb, line 122 def elements(how, what) return @browser.elements(how, what) end
text fields
# File lib/rwebspec-watir/web_browser.rb, line 294 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
Verify the next page following an operation.
Typical usage:
browser.expect_page HomePage
# File lib/rwebspec-watir/web_browser.rb, line 573 def expect_page(page_clazz, argument = nil) if argument page_clazz.new(self, argument) else page_clazz.new(self) end end
# File lib/rwebspec-webdriver/web_browser.rb, line 642 def find_checkboxes_by_name(checkBoxName) elements = find_elements(:name, checkBoxName) elements.reject! {|x| x.tag_name != "input" || x["type"] != "checkbox"} raise "No checkbox with name #{checkBoxName} found" if elements.empty? return elements end
# File lib/rwebspec-webdriver/web_browser.rb, line 160 def find_element(* args) @browser.send("find_element", *args) end
# File lib/rwebspec-webdriver/web_browser.rb, line 164 def find_elements(* args) @browser.send("find_elements", *args) end
return underlying firefox browser object, raise error if not running using Firefox
# File lib/rwebspec-webdriver/web_browser.rb, line 753 def firefox is_firefox? ? @browser : nil; end
# File lib/rwebspec-webdriver/web_browser.rb, line 492 def focus_on_element(elem) begin elem.send_keys("") rescue => e # ignore for example, an on hover table might not be ablet to send keys to end end
# File lib/rwebspec-webdriver/web_browser.rb, line 427 def forward @browser.navigate().forward end
# File lib/rwebspec-watir/web_browser.rb, line 230 def full_url(relative_url) if @context && @context.base_url @context.base_url + relative_url else relative_url end end
Go to a page
Usage: open_browser(:base_url => "http://www.itest2.com") .... goto_page("/purchase") # full url => http://www.itest.com/purchase
# File lib/rwebspec-watir/web_browser.rb, line 282 def goto_page(page) # puts "DEBUG calling goto page => #{page}" @browser.goto full_url(page); end
Go to a URL directly
goto_url("http://www.itest2.com/downloads")
# File lib/rwebspec-watir/web_browser.rb, line 289 def goto_url(url) @browser.goto url end
# File lib/rwebspec-webdriver/web_browser.rb, line 757 def htmlunit raise "can't call this as it is configured to use Celerity" unless RUBY_PLATFORM =~ /java/ @browser end
return underlying browser
# File lib/rwebspec-watir/web_browser.rb, line 554 def ie @browser end
# File lib/rwebspec-webdriver/web_browser.rb, line 68 def initialize_chrome_browser(existing_browser, base_url, options) if existing_browser then @browser = existing_browser return end the_chrome_options = Selenium::WebDriver::Chrome::Options.new # make the same behaviour as Python/JS # leave browser open until calls 'driver.quit' the_chrome_options.add_option("detach", true) if ENV["BROWSER_HEADLESS"] == "true" the_chrome_options.add_argument("--headless") end if defined?(TestWiseRuntimeSupport) && defined?(debugging?) && debugging? browser_debugging_port = get_browser_debugging_port() rescue 19218 # default port puts("Enabled chrome browser debug port: #{browser_debugging_port}") the_chrome_options.add_argument("--remote-debugging-port=#{browser_debugging_port}") else # puts("Chrome debugging port not enabled.") end @browser = Selenium::WebDriver.for(:chrome, :options => the_chrome_options) @browser.navigate.to base_url end
# File lib/rwebspec-webdriver/web_browser.rb, line 54 def initialize_firefox_browser(existing_browser, base_url, options) if existing_browser then @browser = existing_browser return end if options[:profile] @browser = Selenium::WebDriver.for :firefox, :profile => options[:profile] else @browser = Selenium::WebDriver.for :firefox end @browser.navigate.to base_url end
# File lib/rwebspec-webdriver/web_browser.rb, line 105 def initialize_htmlunit_browser(base_url, options) require 'json' caps = Selenium::WebDriver::Remote::Capabilities.htmlunit(:javascript_enabled => false) client = Selenium::WebDriver::Remote::Http::Default.new # client.proxy = Selenium::WebDriver::Proxy.new(:http => "web-proxy.qdot.qld.gov.au:3128") @browser = Selenium::WebDriver.for(:remote, :http_client => client , :desired_capabilities => caps) if options[:go] @browser.navigate.to(base_url) end end
# File lib/rwebspec-watir/web_browser.rb, line 29 def initialize_ie_browser(existing_browser, options) @browser = existing_browser || Watir::IE.new if ($TESTWISE_EMULATE_TYPING && $TESTWISE_TYPING_SPEED) then @browser.set_slow_speed if $TESTWISE_TYPING_SPEED == "slow" @browser.set_fast_speed if $TESTWISE_TYPING_SPEED == 'fast' else @browser.speed = :zippy end return if existing_browser # Watir-classic 3.4 drop the support # @browser.activeObjectHighLightColor = options[:highlight_colour] @browser.visible = options[:visible] unless $HIDE_IE #NOTE: close_others fails begin if options[:close_others] then @browser.windows.reject(&:current?).each(&:close) end rescue => e1 puts "Failed to close others" end end
# File lib/rwebspec-webdriver/web_browser.rb, line 95 def initialize_safari_browser(existing_browser, base_url, options) if existing_browser then @browser = existing_browser return end @browser = Selenium::WebDriver.for :safari @browser.navigate.to base_url end
# File lib/rwebspec-watir/web_browser.rb, line 214 def is_firefox? return false end
# File lib/rwebspec-webdriver/web_browser.rb, line 354 def is_ie? @browser.browser.to_s == "ie" end
Watir 1.9
# File lib/rwebspec-watir/web_browser.rb, line 484 def javascript_dialog @browser.javascript_dialog end
# File lib/rwebspec-webdriver/web_browser.rb, line 307 def links @browser.find_elements(:tag_name, "a") end
Returns the specified ole object for input elements on a web page.
This method is used internally by Watir and should not be used externally. It cannot be marked as private because of the way mixins and inheritance work in watir
* how - symbol - the way we look for the object. Supported values are - :name - :id - :index - :value etc * what - string that we are looking for, ex. the name, or id tag attribute or index of the object we are looking for. * types - what object types we will look at. * value - used for objects that have one name, but many values. ex. radio lists and checkboxes
# File lib/rwebspec-watir/web_browser.rb, line 142 def locate_input_element(how, what, types, value=nil) @browser.locate_input_element(how, what, types, value) end
This is the main method for accessing map tags - msdn.microsoft.com/workshop/author/dhtml/reference/objects/map.asp?frame=true
* how - symbol - how we access the map, * what - string, integer or regular expression - what we are looking for,
Valid values for 'how' are listed in the Watir Wiki - wiki.openqa.org/display/WTR/Methods+supported+by+Element
returns a map object
Typical Usage
map(:id, /list/) # access the first map that matches list. map(:index,2) # access the second map on the page map(:title, "A Picture") # access a map using the tooltip text. See http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/title_1.asp?frame=true
# File lib/rwebspec-watir/web_browser.rb, line 160 def map(how, what=nil) @browser.map(how, what) end
# File lib/rwebspec-watir/web_browser.rb, line 92 def modal_dialog(how=nil, what=nil) @browser.modal_dialog(how, what) end
Attach a Watir::IE instance to a popup window.
Typical usage
new_popup_window(:url => "http://www.google.com/a.pdf")
# File lib/rwebspec-watir/web_browser.rb, line 510 def new_popup_window(options, browser = "ie") if is_firefox? raise "not implemented" else if options[:url] Watir::IE.attach(:url, options[:url]) elsif options[:title] Watir::IE.attach(:title, options[:title]) else raise 'Please specify title or url of new pop up window' end end end
return HTML of current web page
# File lib/rwebspec-watir/web_browser.rb, line 169 def page_source @browser.html() #@browser.document.body end
# File lib/rwebspec-watir/web_browser.rb, line 182 def page_title case @browser.class.to_s when "Watir::IE" @browser.document.title else @browser.title end end
# File lib/rwebspec-webdriver/web_browser.rb, line 315 def radios @browser.find_elements(:xpath, "//input[@type='radio']") end
# File lib/rwebspec-webdriver/web_browser.rb, line 438 def refresh @browser.navigate().refresh end
Save current web page source to file
usage: save_page("/tmp/01.html") save_page() => # will save to "20090830112200.html"
# File lib/rwebspec-watir/web_browser.rb, line 562 def save_page(file_name = nil) file_name ||= Time.now.strftime("%Y%m%d%H%M%S") + ".html" puts "about to save page: #{File.expand_path(file_name)}" if $DEBUG File.open(file_name, "w").puts page_source end
# File lib/rwebspec-watir/web_browser.rb, line 478 def select_file_for_upload(file_field, file_path) normalized_file_path = RUBY_PLATFORM.downcase.include?("mingw") ? file_path.gsub("/", "\\") : file_path file_field(:name, file_field).set(normalized_file_path) end
# File lib/rwebspec-webdriver/web_browser.rb, line 319 def select_lists @browser.find_elements(:tag_name, "select") end
Select a dropdown list by name Usage:
select_option("country", "Australia")
# File lib/rwebspec-watir/web_browser.rb, line 375 def select_option(selectName, option) select_list(:name, selectName).select(option) end
# File lib/rwebspec-watir/web_browser.rb, line 126 def show_all_objects @browser.show_all_objects end
A Better Popup
Handler using the latest Watir version. Posted by Mark_cain@rl.gov
wiki.openqa.org/display/WTR/FAQ#FAQ-HowdoIattachtoapopupwindow%3F
# File lib/rwebspec-watir/web_browser.rb, line 535 def start_clicker( button, waitTime= 9, user_input=nil) # get a handle if one exists hwnd = @browser.enabled_popup(waitTime) if (hwnd) # yes there is a popup w = WinClicker.new if ( user_input ) w.setTextValueForFileNameField( hwnd, "#{user_input}" ) end # I put this in to see the text being input it is not necessary to work sleep 3 # "OK" or whatever the name on the button is w.clickWindowsButton_hwnd( hwnd, "#{button}" ) # # this is just cleanup w = nil end end
# File lib/rwebspec-watir/web_browser.rb, line 488 def start_window(url = nil) @browser.start_window(url); end
submit first submit button
# File lib/rwebspec-watir/web_browser.rb, line 380 def submit(buttonName = nil) if (buttonName.nil?) then buttons.each { |button| next if button.type != 'submit' button.click return } else click_button_with_name(buttonName) end end
return plain text of current web page
# File lib/rwebspec-watir/web_browser.rb, line 178 def text @browser.text end
Check a checkbox Usage:
uncheck_checkbox("agree") uncheck_checkbox("agree", "false")
# File lib/rwebspec-watir/web_browser.rb, line 415 def uncheck_checkbox(checkBoxName, values = nil) if values values.class == Array ? arys = values : arys = [values] arys.each {|cbx_value| if Watir::VERSION =~ /^1/ then checkbox(:name, checkBoxName, cbx_value).clear else checkbox(:name => checkBoxName, :value => cbx_value).clear end } else checkbox(:name, checkBoxName).clear end end
# File lib/rwebspec-webdriver/web_browser.rb, line 350 def underlying_browser @browser end
current url
# File lib/rwebspec-watir/web_browser.rb, line 202 def url @browser.url end
A convenience method to wait at both ends of an operation for the browser to catch up.
# File lib/rwebspec-watir/web_browser.rb, line 261 def wait_before_and_after wait_for_browser yield wait_for_browser end
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.
# File lib/rwebspec-watir/web_browser.rb, line 253 def wait_for_browser # Watir 3 does not support it any more # @browser.waitForIE unless is_firefox? end