Class: RWebSpec::WebBrowser
- Inherits:
-
Object
- Object
- RWebSpec::WebBrowser
- Includes:
- ElementLocator
- Defined in:
- lib/rwebspec-watir/web_browser.rb,
lib/rwebspec-webdriver/web_browser.rb
Overview
Wrapping WATIR IE and FireWatir Firefox
Constant Summary
Constants included from ElementLocator
ElementLocator::BUTTON_VALID_TYPES, ElementLocator::CHECK_BOX_TYPES, ElementLocator::FILE_FIELD_TYPES, ElementLocator::HIDDEN_TYPES, ElementLocator::RADIO_TYPES, ElementLocator::TEXT_FILED_TYPES
Instance Attribute Summary collapse
-
#context ⇒ Object
Returns the value of attribute context.
Class Method Summary collapse
-
.attach_browser(how, what, options = {}) ⇒ Object
Attach to existing browser.
- .close_all_browsers ⇒ Object
-
.is_windows? ⇒ Boolean
is it running in MS Windows platforms?.
-
.new_from_existing(underlying_browser, web_context = nil) ⇒ Object
for popup windows.
-
.reuse(base_url, options) ⇒ Object
TODO resuse not working yet.
Instance Method Summary collapse
-
#area(*args) ⇒ Object
Wrapp of area to support Firefox and Watir.
- #as ⇒ Object
-
#back ⇒ Object
(also: #go_back)
TODO can’t browse back if on invalid page.
- #base_url=(new_base_url) ⇒ Object
-
#begin_at(relative_url) ⇒ Object
Crahses where ssshtttp:///.
- #browser_opened? ⇒ Boolean
- #buttons ⇒ Object
-
#check_checkbox(checkBoxName, values = nil) ⇒ Object
Check a checkbox Usage: check_checkbox(“agree”) check_checkbox(“agree”, “true”).
- #checkboxes ⇒ Object
-
#clear_radio_option(radio_group, radio_option) ⇒ Object
(also: #clear_radio_button)
Clear a radio button Usage: click_radio_option(“country”, “Australia”).
-
#click_button_with_caption(caption, opts = {}) ⇒ Object
(also: #click_button, #click_button_with_text)
Click a button with caption.
-
#click_button_with_id(id, opts = {}) ⇒ Object
Click a button with give HTML id Usage: click_button_with_id(“btn_sumbit”) click_button_with_id(“btn_sumbit”, :index => 2) # the secone link with same id, not good gractice in HTML.
-
#click_button_with_image_src_contains(image_filename) ⇒ Object
(also: #click_button_with_image)
Click image buttion with image source name.
-
#click_button_with_name(name, opts = {}) ⇒ Object
Click a button with give name Usage: click_button_with_name(“confirm”) click_button_with_name(“confirm”, :index => 2).
-
#click_button_with_value(value, opts = {}) ⇒ Object
click_button_with_caption(“Confirm payment”).
-
#click_link_with_id(link_id, opts = {}) ⇒ Object
links.
-
#click_link_with_text(link_text, opts = {}) ⇒ Object
(also: #click_link)
click_link_with_text(“Login”) click_link_with_text(“Show”, :index => 2).
-
#click_radio_option(radio_group, radio_option) ⇒ Object
(also: #click_radio_button)
Click a radio button Usage: click_radio_option(“country”, “Australia”).
-
#close_all_browsers ⇒ Object
TODO determine browser type, check FireWatir support or not.
-
#close_browser ⇒ Object
(also: #close)
Close the browser window.
- #contains_text(text) ⇒ Object
-
#current_url ⇒ Object
(also: #url)
current url.
- #divs ⇒ Object
- #driver ⇒ Object
-
#dump_response(stream = nil) ⇒ Object
— For deubgging —.
-
#element(how, what) ⇒ Object
This is the main method for accessing a generic element with a given attibute * how - symbol - how we access the element.
-
#element_by_id(elem_id) ⇒ Object
Deprecated: using Watir style directly instead.
- #element_source(elementId) ⇒ Object
- #element_value(elementId) ⇒ Object
-
#elements(how, what) ⇒ Object
this is the main method for accessing generic html elements by an attribute.
-
#enter_text_into_field_with_name(name, text) ⇒ Object
(also: #set_form_element, #enter_text)
text fields.
-
#expect_page(page_clazz, argument = nil) ⇒ Object
Verify the next page following an operation.
- #find_checkboxes_by_name(checkBoxName) ⇒ Object
- #find_element(*args) ⇒ Object
- #find_elements(*args) ⇒ Object
-
#firefox ⇒ Object
return underlying firefox browser object, raise error if not running using Firefox.
- #focus_on_element(elem) ⇒ Object
- #forward ⇒ Object (also: #go_forward)
- #full_url(relative_url) ⇒ Object
-
#goto_page(page) ⇒ Object
Go to a page Usage: open_browser(:base_url => “www.itest2.com”) .…
-
#goto_url(url) ⇒ Object
Go to a URL directly goto_url(“www.itest2.com/downloads”).
- #htmlunit ⇒ Object
-
#ie ⇒ Object
return underlying browser.
-
#initialize(base_url = nil, existing_browser = nil, options = {}) ⇒ WebBrowser
constructor
A new instance of WebBrowser.
- #initialize_chrome_browser(existing_browser, base_url, options) ⇒ Object
- #initialize_firefox_browser(existing_browser, base_url, options) ⇒ Object
- #initialize_htmlunit_browser(base_url, options) ⇒ Object
- #initialize_ie_browser(existing_browser, options) ⇒ Object
- #initialize_safari_browser(existing_browser, base_url, options) ⇒ Object
- #is_firefox? ⇒ Boolean
- #is_ie? ⇒ Boolean
-
#javascript_dialog ⇒ Object
Watir 1.9.
- #links ⇒ Object
-
#locate_input_element(how, what, types, value = nil) ⇒ Object
Returns the specified ole object for input elements on a web page.
-
#map(how, what = nil) ⇒ Object
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,.
-
#method ⇒ Object
Delegate to WebDriver.
- #modal_dialog(how = nil, what = nil) ⇒ Object
-
#new_popup_window(options, browser = "ie") ⇒ Object
Attach to a popup window, to be removed.
-
#page_source ⇒ Object
(also: #html_body, #html)
return HTML of current web page.
- #page_title ⇒ Object
- #radios ⇒ Object
- #refresh ⇒ Object (also: #refresh_page)
-
#save_page(file_name = nil) ⇒ Object
Save current web page source to file usage: save_page(“/tmp/01.html”) save_page() => # will save to “20090830112200.html”.
- #select_file_for_upload(file_field_name, file_path) ⇒ Object
- #select_lists ⇒ Object
-
#select_option(selectName, text) ⇒ Object
Select a dropdown list by name Usage: select_option(“country”, “Australia”).
- #show_all_objects ⇒ Object
-
#start_clicker(button, waitTime = 9, user_input = nil) ⇒ Object
A Better Popup Handler using the latest Watir version.
- #start_window(url = nil) ⇒ Object
-
#submit(buttonName = nil) ⇒ Object
submit first submit button.
-
#text(squeeze_spaces = true) ⇒ Object
return plain text of current web page.
-
#uncheck_checkbox(checkBoxName, values = nil) ⇒ Object
Uncheck a checkbox Usage: uncheck_checkbox(“agree”) uncheck_checkbox(“agree”, “false”).
- #underlying_browser ⇒ Object
-
#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.
Methods included from ElementLocator
#attribute_expression, #button_elements, #check_box_elements, #equal_pair, #file_field_elements, #find_by_tag, #hidden_elements, #lhs_for, #radio_elements, #select_elements, #should_use_label_element?, #text_area_elements, #text_field_elements
Constructor Details
#initialize(base_url = nil, existing_browser = nil, options = {}) ⇒ WebBrowser
Returns a new instance of WebBrowser.
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/rwebspec-watir/web_browser.rb', line 16 def initialize(base_url = nil, existing_browser = nil, = {}) = {:speed => "zippy", :visible => true, :highlight_colour => 'yellow', :close_others => true } = .merge @context = Context.new base_url if base_url initialize_ie_browser(existing_browser, ) end |
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
14 15 16 |
# File 'lib/rwebspec-watir/web_browser.rb', line 14 def context @context end |
Class Method Details
.attach_browser(how, what, options = {}) ⇒ Object
Attach to existing browser
Usage:
WebBrowser.attach_browser(:title, "iTest2")
WebBrowser.attach_browser(:url, "http://www.itest2.com")
WebBrowser.attach_browser(:url, "http://www.itest2.com", {:browser => "Firefox", :base_url => "http://www.itest2.com"})
WebBrowser.attach_browser(:title, /agileway\.com\.au\/attachment/) # regular expression
499 500 501 502 503 504 |
# File 'lib/rwebspec-watir/web_browser.rb', line 499 def self.attach_browser(how, what, ={}) = {:browser => "IE"} = .merge() site_context = Context.new([:base_url]) if [:base_url] return WebBrowser.new_from_existing(Watir::IE.attach(how, what), site_context) end |
.close_all_browsers ⇒ Object
354 355 356 |
# File 'lib/rwebspec-webdriver/web_browser.rb', line 354 def self.close_all_browsers puts "[WARN] not supported yet in RWebSpec-WebDriver" end |
.is_windows? ⇒ Boolean
is it running in MS Windows platforms?
582 583 584 |
# 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 |
.new_from_existing(underlying_browser, web_context = nil) ⇒ Object
for popup windows
69 70 71 |
# File 'lib/rwebspec-watir/web_browser.rb', line 69 def self.new_from_existing(, web_context = nil) return WebBrowser.new(web_context ? web_context.base_url : nil, , {:close_others => false}) end |
.reuse(base_url, options) ⇒ Object
TODO resuse not working yet
126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/rwebspec-webdriver/web_browser.rb', line 126 def self.reuse(base_url, ) 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, ) end #puts "no browser instance found" WebBrowser.new(base_url, nil, ) else WebBrowser.new(base_url, nil, ) end end |
Instance Method Details
#area(*args) ⇒ Object
Wrapp of area to support Firefox and Watir
163 164 165 |
# File 'lib/rwebspec-webdriver/web_browser.rb', line 163 def area(*args) @browser.send("area", *args) end |
#as ⇒ Object
196 |
# File 'lib/rwebspec-watir/web_browser.rb', line 196 alias as links |
#back ⇒ Object Also known as: go_back
TODO can’t browse back if on invalid page
412 413 414 |
# File 'lib/rwebspec-webdriver/web_browser.rb', line 412 def back @browser.navigate.back end |
#base_url=(new_base_url) ⇒ Object
206 207 208 209 210 211 212 |
# 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 |
#begin_at(relative_url) ⇒ Object
Crahses where ssshtttp:///
367 368 369 |
# File 'lib/rwebspec-webdriver/web_browser.rb', line 367 def begin_at(relative_url) @browser.goto full_url(relative_url) end |
#browser_opened? ⇒ Boolean
242 243 244 245 246 247 248 |
# File 'lib/rwebspec-watir/web_browser.rb', line 242 def browser_opened? begin @browser != nil rescue => e return false end end |
#buttons ⇒ Object
302 303 304 305 |
# File 'lib/rwebspec-webdriver/web_browser.rb', line 302 def = @browser.find_elements(:tag_name, "button") + @browser.find_elements(:xpath, "//input[@type='submit']") + @browser.find_elements(:xpath, "//input[@type='button']") return end |
#check_checkbox(checkBoxName, values = nil) ⇒ Object
Check a checkbox Usage:
check_checkbox("agree")
check_checkbox("agree", "true")
396 397 398 399 400 401 402 403 404 405 406 407 408 409 |
# 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 |
#checkboxes ⇒ Object
290 291 292 |
# File 'lib/rwebspec-webdriver/web_browser.rb', line 290 def checkboxes @browser.find_elements(:xpath, "//input[@type='checkbox']") end |
#clear_radio_option(radio_group, radio_option) ⇒ Object Also known as:
Clear a radio button
Usage:
click_radio_option("country", "Australia")
446 447 448 449 450 451 452 |
# 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 |
#click_button_with_caption(caption, opts = {}) ⇒ Object Also known as: ,
Click a button with caption
TODO: Caption is same as value
Usage:
("Confirm payment")
351 352 353 354 355 356 357 |
# File 'lib/rwebspec-watir/web_browser.rb', line 351 def (caption, opts={}) if opts && opts[:index] wait_before_and_after { (:caption => caption, :index => opts[:index]).click } else wait_before_and_after { (:caption, caption).click } end end |
#click_button_with_id(id, opts = {}) ⇒ Object
Click a button with give HTML id Usage:
("btn_sumbit")
("btn_sumbit", :index => 2) # the secone link with same id, not good gractice in HTML
329 330 331 332 333 334 335 |
# File 'lib/rwebspec-watir/web_browser.rb', line 329 def (id, opts = {}) if opts && opts[:index] wait_before_and_after { (:id => id, :index => opts[:index]).click } else wait_before_and_after { (:id, id).click } 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")
567 568 569 570 571 572 573 574 575 576 577 578 579 |
# File 'lib/rwebspec-webdriver/web_browser.rb', line 567 def (image_filename) = found = nil .select do |x| if x["src"] =~ /#{Regexp.escape(image_filename)}/ found = x break end end raise "not image button with src: #{image_filename} found" if found.nil? found.click end |
#click_button_with_name(name, opts = {}) ⇒ Object
Click a button with give name Usage:
("confirm")
("confirm", :index => 2)
340 341 342 343 344 345 346 |
# File 'lib/rwebspec-watir/web_browser.rb', line 340 def (name, opts={}) if opts && opts[:index] wait_before_and_after { (:name => name, :index => opts[:index]).click } else wait_before_and_after { (:name, name).click } end end |
#click_button_with_value(value, opts = {}) ⇒ Object
click_button_with_caption(“Confirm payment”)
364 365 366 367 368 369 370 |
# File 'lib/rwebspec-watir/web_browser.rb', line 364 def (value, opts={}) if opts && opts[:index] wait_before_and_after { (:value => value, :index => opts[:index]).click } else wait_before_and_after { (:value, value).click } end end |
#click_link_with_id(link_id, opts = {}) ⇒ Object
links
308 309 310 311 312 313 314 |
# 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 |
#click_link_with_text(link_text, opts = {}) ⇒ Object Also known as: click_link
click_link_with_text(“Login”) click_link_with_text(“Show”, :index => 2)
482 483 484 485 486 487 488 |
# File 'lib/rwebspec-webdriver/web_browser.rb', line 482 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_radio_option(radio_group, radio_option) ⇒ Object Also known as:
Click a radio button
Usage:
click_radio_option("country", "Australia")
434 435 436 437 438 439 440 |
# 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 |
#close_all_browsers ⇒ Object
TODO determine browser type, check FireWatir support or not
350 351 352 |
# File 'lib/rwebspec-webdriver/web_browser.rb', line 350 def close_all_browsers(browser_type = :ie) @browser.windows.each(&:close) end |
#close_browser ⇒ Object Also known as: close
Close the browser window. Useful for automated test suites to reduce test interaction.
220 221 222 223 |
# File 'lib/rwebspec-watir/web_browser.rb', line 220 def close_browser @browser.close sleep 2 end |
#contains_text(text) ⇒ Object
164 165 166 |
# File 'lib/rwebspec-watir/web_browser.rb', line 164 def contains_text(text) @browser.contains_text(text); end |
#current_url ⇒ Object Also known as: url
current url
312 313 314 |
# File 'lib/rwebspec-webdriver/web_browser.rb', line 312 def current_url @browser.current_url end |
#divs ⇒ Object
307 308 309 |
# File 'lib/rwebspec-webdriver/web_browser.rb', line 307 def divs @browser.find_elements(:tag_name, "divs") end |
#driver ⇒ Object
325 326 327 |
# File 'lib/rwebspec-webdriver/web_browser.rb', line 325 def driver @browser end |
#dump_response(stream = nil) ⇒ Object
For deubgging
527 528 529 |
# File 'lib/rwebspec-watir/web_browser.rb', line 527 def dump_response(stream = nil) stream.nil? ? puts(page_source) : stream.puts(page_source) end |
#element(how, what) ⇒ Object
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
108 109 110 |
# File 'lib/rwebspec-watir/web_browser.rb', line 108 def element(how, what) return @browser.element(how, what) end |
#element_by_id(elem_id) ⇒ Object
Deprecated: using Watir style directly instead
456 457 458 459 460 461 462 463 464 465 |
# 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 |
#element_source(elementId) ⇒ Object
472 473 474 475 476 |
# 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 |
#element_value(elementId) ⇒ Object
467 468 469 470 |
# File 'lib/rwebspec-watir/web_browser.rb', line 467 def element_value(elementId) elem = element_by_id(elementId) elem ? elem.invoke('innerText') : nil end |
#elements(how, what) ⇒ Object
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
122 123 124 |
# File 'lib/rwebspec-watir/web_browser.rb', line 122 def elements(how, what) return @browser.elements(how, what) end |
#enter_text_into_field_with_name(name, text) ⇒ Object Also known as: set_form_element, enter_text
text fields
294 295 296 297 298 299 300 301 |
# 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 |
#expect_page(page_clazz, argument = nil) ⇒ Object
Verify the next page following an operation.
Typical usage:
browser.expect_page HomePage
573 574 575 576 577 578 579 |
# 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 |
#find_checkboxes_by_name(checkBoxName) ⇒ Object
621 622 623 624 625 626 |
# File 'lib/rwebspec-webdriver/web_browser.rb', line 621 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 |
#find_element(*args) ⇒ Object
139 140 141 |
# File 'lib/rwebspec-webdriver/web_browser.rb', line 139 def find_element(* args) @browser.send("find_element", *args) end |
#find_elements(*args) ⇒ Object
143 144 145 |
# File 'lib/rwebspec-webdriver/web_browser.rb', line 143 def find_elements(* args) @browser.send("find_elements", *args) end |
#firefox ⇒ Object
return underlying firefox browser object, raise error if not running using Firefox
732 733 734 |
# File 'lib/rwebspec-webdriver/web_browser.rb', line 732 def firefox is_firefox? ? @browser : nil; end |
#focus_on_element(elem) ⇒ Object
471 472 473 474 475 476 477 |
# File 'lib/rwebspec-webdriver/web_browser.rb', line 471 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 |
#forward ⇒ Object Also known as: go_forward
406 407 408 |
# File 'lib/rwebspec-webdriver/web_browser.rb', line 406 def forward @browser.navigate().forward end |
#full_url(relative_url) ⇒ Object
230 231 232 233 234 235 236 |
# 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 |
#goto_page(page) ⇒ Object
Go to a page
Usage:
open_browser(:base_url => "http://www.itest2.com")
....
goto_page("/purchase") # full url => http://www.itest.com/purchase
282 283 284 285 |
# 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 |
#goto_url(url) ⇒ Object
Go to a URL directly
goto_url("http://www.itest2.com/downloads")
289 290 291 |
# File 'lib/rwebspec-watir/web_browser.rb', line 289 def goto_url(url) @browser.goto url end |
#htmlunit ⇒ Object
736 737 738 739 |
# File 'lib/rwebspec-webdriver/web_browser.rb', line 736 def htmlunit raise "can't call this as it is configured to use Celerity" unless RUBY_PLATFORM =~ /java/ @browser end |
#ie ⇒ Object
return underlying browser
554 555 556 |
# File 'lib/rwebspec-watir/web_browser.rb', line 554 def ie @browser end |
#initialize_chrome_browser(existing_browser, base_url, options) ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/rwebspec-webdriver/web_browser.rb', line 64 def initialize_chrome_browser(existing_browser, base_url, ) if existing_browser then @browser = existing_browser return end @browser = Selenium::WebDriver.for :chrome @browser.navigate.to base_url end |
#initialize_firefox_browser(existing_browser, base_url, options) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/rwebspec-webdriver/web_browser.rb', line 54 def initialize_firefox_browser(existing_browser, base_url, ) if existing_browser then @browser = existing_browser return end @browser = Selenium::WebDriver.for :firefox @browser.navigate.to base_url end |
#initialize_htmlunit_browser(base_url, options) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/rwebspec-webdriver/web_browser.rb', line 84 def initialize_htmlunit_browser(base_url, ) 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 [:go] @browser.navigate.to(base_url) end end |
#initialize_ie_browser(existing_browser, options) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/rwebspec-watir/web_browser.rb', line 29 def initialize_ie_browser(existing_browser, ) @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 = [:visible] unless $HIDE_IE #NOTE: close_others fails begin if [:close_others] then @browser.windows.reject(&:current?).each(&:close) end rescue => e1 puts "Failed to close others" end end |
#initialize_safari_browser(existing_browser, base_url, options) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/rwebspec-webdriver/web_browser.rb', line 74 def initialize_safari_browser(existing_browser, base_url, ) if existing_browser then @browser = existing_browser return end @browser = Selenium::WebDriver.for :safari @browser.navigate.to base_url end |
#is_firefox? ⇒ Boolean
214 215 216 |
# File 'lib/rwebspec-watir/web_browser.rb', line 214 def is_firefox? return false end |
#is_ie? ⇒ Boolean
333 334 335 |
# File 'lib/rwebspec-webdriver/web_browser.rb', line 333 def is_ie? @browser.browser.to_s == "ie" end |
#javascript_dialog ⇒ Object
Watir 1.9
484 485 486 |
# File 'lib/rwebspec-watir/web_browser.rb', line 484 def javascript_dialog @browser.javascript_dialog end |
#links ⇒ Object
286 287 288 |
# File 'lib/rwebspec-webdriver/web_browser.rb', line 286 def links @browser.find_elements(:tag_name, "a") end |
#locate_input_element(how, what, types, value = nil) ⇒ Object
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
142 143 144 |
# 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 |
#map(how, what = nil) ⇒ Object
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
160 161 162 |
# File 'lib/rwebspec-watir/web_browser.rb', line 160 def map(how, what=nil) @browser.map(how, what) end |
#method ⇒ Object
Delegate to WebDriver
77 78 79 80 81 |
# File 'lib/rwebspec-watir/web_browser.rb', line 77 [:button, :td, :checkbox, :div, :form, :frame, :h1, :h2, :h3, :h4, :h5, :h6, :hidden, :image, :li, :link, :map, :pre, :tr, :radio, :select_list, :span, :table, :text_field, :paragraph, :file_field, :label].each do |method| define_method method do |*args| @browser.send(method, *args) end end |
#modal_dialog(how = nil, what = nil) ⇒ Object
92 93 94 |
# File 'lib/rwebspec-watir/web_browser.rb', line 92 def modal_dialog(how=nil, what=nil) @browser.modal_dialog(how, what) end |
#new_popup_window(options, browser = "ie") ⇒ Object
Attach to a popup window, to be removed
Typical usage
new_popup_window(:url => "http://www.google.com/a.pdf")
510 511 512 513 514 515 516 517 518 519 520 521 522 |
# File 'lib/rwebspec-watir/web_browser.rb', line 510 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, html
return HTML of current web page
169 170 171 172 |
# File 'lib/rwebspec-watir/web_browser.rb', line 169 def page_source @browser.html() #@browser.document.body end |
#page_title ⇒ Object
182 183 184 185 186 187 188 189 |
# 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 |
#radios ⇒ Object
294 295 296 |
# File 'lib/rwebspec-webdriver/web_browser.rb', line 294 def radios @browser.find_elements(:xpath, "//input[@type='radio']") end |
#refresh ⇒ Object Also known as: refresh_page
417 418 419 |
# File 'lib/rwebspec-webdriver/web_browser.rb', line 417 def refresh @browser.navigate().refresh end |
#save_page(file_name = nil) ⇒ Object
Save current web page source to file
usage:
save_page("/tmp/01.html")
save_page() => # will save to "20090830112200.html"
562 563 564 565 566 |
# 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.(file_name)}" if $DEBUG File.open(file_name, "w").puts page_source end |
#select_file_for_upload(file_field_name, file_path) ⇒ Object
478 479 480 481 |
# 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 |
#select_lists ⇒ Object
298 299 300 |
# File 'lib/rwebspec-webdriver/web_browser.rb', line 298 def select_lists @browser.find_elements(:tag_name, "select") end |
#select_option(selectName, text) ⇒ Object
Select a dropdown list by name Usage:
select_option("country", "Australia")
375 376 377 |
# File 'lib/rwebspec-watir/web_browser.rb', line 375 def select_option(selectName, option) select_list(:name, selectName).select(option) end |
#show_all_objects ⇒ Object
126 127 128 |
# File 'lib/rwebspec-watir/web_browser.rb', line 126 def show_all_objects @browser.show_all_objects 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
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 |
# File 'lib/rwebspec-watir/web_browser.rb', line 535 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
488 489 490 |
# File 'lib/rwebspec-watir/web_browser.rb', line 488 def start_window(url = nil) @browser.start_window(url); end |
#submit(buttonName = nil) ⇒ Object
submit first submit button
380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/rwebspec-watir/web_browser.rb', line 380 def submit( = nil) if (.nil?) then .each { || next if .type != 'submit' .click return } else () end end |
#text(squeeze_spaces = true) ⇒ Object
return plain text of current web page
178 179 180 |
# File 'lib/rwebspec-watir/web_browser.rb', line 178 def text @browser.text end |
#uncheck_checkbox(checkBoxName, values = nil) ⇒ Object
Uncheck a checkbox Usage:
uncheck_checkbox("agree")
uncheck_checkbox("agree", "false")
415 416 417 418 419 420 421 422 423 424 425 426 427 428 |
# 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 |
#underlying_browser ⇒ Object
329 330 331 |
# File 'lib/rwebspec-webdriver/web_browser.rb', line 329 def @browser end |
#wait_before_and_after ⇒ Object
A convenience method to wait at both ends of an operation for the browser to catch up.
261 262 263 264 265 |
# File 'lib/rwebspec-watir/web_browser.rb', line 261 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.
253 254 255 256 |
# 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 |