Class: RWebSpec::Mechanize::WebBrowser
- Inherits:
-
Object
- Object
- RWebSpec::Mechanize::WebBrowser
- Defined in:
- lib/rwebspec-mechanize/web_browser.rb
Overview
Wrapping WATIR IE and FireWatir Firefox
Instance Attribute Summary collapse
-
#context ⇒ Object
Returns the value of attribute context.
-
#current_form ⇒ Object
Returns the value of attribute current_form.
Class Method Summary collapse
-
.attach_browser(how, what, options = {}) ⇒ Object
Attach to existing browser.
- .close_all_browsers(browser_type = :ie) ⇒ Object
-
.is_windows? ⇒ Boolean
is it running in MS Windows platforms?.
Instance Method Summary collapse
- #agent ⇒ Object
-
#area(*args) ⇒ Object
Wrapp of Watir’s area to support Firefox and Watir.
- #base_url=(new_base_url) ⇒ Object
- #begin_at(relative_url) ⇒ Object
- #browser_opened? ⇒ Boolean
-
#check_checkbox(checkBoxName, values = nil) ⇒ Object
Check a checkbox Usage: check_checkbox(“agree”) check_checkbox(“agree”, “true”).
-
#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_button_with_value)
Click a button with caption Usage: click_button_with_caption(“Confirm payment”).
-
#click_button_with_id(id, opts = {}) ⇒ Object
Occasionaly, the button with ID exists, but now shown in the form (debug form.inspect), add efault to submit.
-
#click_button_with_name(name, opts = {}) ⇒ Object
Click a button with give name Usage: click_button_with_name(“confirm”).
-
#click_link_with_id(link_id, opts = {}) ⇒ Object
links.
- #click_link_with_text(link_text, opts = {}) ⇒ Object (also: #click_link)
-
#click_radio_option(radio_group, radio_option) ⇒ Object
(also: #click_radio_button)
Click a radio button Usage: click_radio_option(“country”, “Australia”).
-
#close_browser ⇒ Object
(also: #close)
Close the browser window.
- #contains_text(text) ⇒ Object
-
#dump_response(stream = nil) ⇒ Object
— For deubgging —.
-
#element(how, what) ⇒ Object
-
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, value) ⇒ Object
(also: #set_form_element, #enter_text)
text fields.
-
#expect_page(page_clazz, argument = nil) ⇒ Object
Verify the next page following an operation.
- #full_url(relative_url) ⇒ Object
-
#goto_page(page) ⇒ Object
Go to a page Usage: open_browser(“www.itest2.com” .… goto_page(“/purchase”) # full url => www.itest.com/purchase.
-
#goto_url(url) ⇒ Object
Go to a URL directly goto_url(“www.itest2.com/downloads”).
-
#identify_form(opts = nil) ⇒ Object
(also: #choose_form)
search_form = page.form_with :name => “f” page.form_with :id => new_client.
-
#initialize(base_url = nil, existing_browser = nil, options = {}) ⇒ WebBrowser
constructor
A new instance of WebBrowser.
-
#javascript_dialog ⇒ Object
Watir 1.9.
-
#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 Watir.
- #modal_dialog(how = nil, what = nil) ⇒ Object
-
#new_popup_window(options, browser = "ie") ⇒ Object
Attach a Watir::Browser instance to a popup window.
- #page ⇒ Object
-
#page_source ⇒ Object
(also: #html_body, #html)
return HTML of current web page.
- #page_title ⇒ Object
-
#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, file_path) ⇒ Object
-
#select_option(selectName, optionText) ⇒ Object
Select a dropdown list by name Usage: select_option(“country”, “Australia”).
-
#set_proxy(address, port, user = nil, password = nil) ⇒ Object
set proxy password.
- #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.
-
#text_sanitize(squeeze_spaces = true) ⇒ Object
Sanitize gem does not work in Java, return empty spaces back.
-
#uncheck_checkbox(checkBoxName, values = nil) ⇒ Object
UnCheck a checkbox Usage: uncheck_checkbox(“agree”) uncheck_checkbox(“agree”, “false”).
-
#url ⇒ Object
current url.
-
#wait_before_and_after ⇒ Object
A convenience method to wait at both ends of an operation for the browser to catch up.
-
#wait_for_browser ⇒ Object
Some browsers (i.e. IE) need to be waited on before more actions can be performed.
Constructor Details
#initialize(base_url = nil, existing_browser = nil, options = {}) ⇒ WebBrowser
Returns a new instance of WebBrowser.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 22 def initialize(base_url = nil, existing_browser = nil, = {}) = {:go => false} = .merge @context = Context.new base_url if base_url @browser = ::Mechanize.new puts "[DELETE DEBUG] => new Mechanize Brwoser with base_url => #{base_url}" require 'logger' # http://mechanize.rubyforge.org/Mechanize.html # TODO option to turn off mechanize logging # puts "Max file buffer #{@browser.max_file_buffer}" # puts "Follow redirect? #{@browser.follow_redirect?}" # puts "redirect limit => #{@browser.redirection_limit}" # @browser.log = Logger.new("mech.log") @browser.keep_alive = false @browser.open_timeout = 15 @browser.read_timeout = 15 # @browser.max_file_buffer=(bytes) if [:go] @browser.get(base_url) else @browser end end |
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
18 19 20 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 18 def context @context end |
#current_form ⇒ Object
Returns the value of attribute current_form.
20 21 22 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 20 def current_form @current_form 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
597 598 599 600 601 602 603 604 605 606 607 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 597 def self.attach_browser(how, what, ={}) = {:browser => "IE"} = .merge() site_context = Context.new([:base_url]) if [:base_url] if ([:browser] == "Firefox") ff = FireWatir::Firefox.attach(how, what) return WebBrowser.new_from_existing(ff, site_context) else return WebBrowser.new_from_existing(Watir::Browser.attach(how, what), site_context) end end |
.close_all_browsers(browser_type = :ie) ⇒ Object
233 234 235 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 233 def self.close_all_browsers(browser_type = :ie) end |
.is_windows? ⇒ Boolean
is it running in MS Windows platforms?
681 682 683 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 681 def self.is_windows? RUBY_PLATFORM.downcase.include?("mswin") or RUBY_PLATFORM.downcase.include?("mingw") end |
Instance Method Details
#agent ⇒ Object
52 53 54 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 52 def agent @browser end |
#area(*args) ⇒ Object
Wrapp of Watir’s area to support Firefox and Watir
Note: FireWatir does not support area directly, treat it as text_field
73 74 75 76 77 78 79 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 73 def area(*args) if is_firefox? text_field(*args) else @browser.send("area", *args) end end |
#base_url=(new_base_url) ⇒ Object
218 219 220 221 222 223 224 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 218 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
245 246 247 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 245 def begin_at(relative_url) @browser.goto full_url(relative_url) end |
#browser_opened? ⇒ Boolean
249 250 251 252 253 254 255 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 249 def browser_opened? begin @browser != nil rescue => e return false end end |
#check_checkbox(checkBoxName, values = nil) ⇒ Object
Check a checkbox Usage:
check_checkbox("agree")
check_checkbox("agree", "true")
504 505 506 507 508 509 510 511 512 513 514 515 516 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 504 def check_checkbox(checkBoxName, values=nil) the_form = identify_form if values values.class == Array ? arys = values : arys = [values] arys.each {|cbx_value| the_form.checkboxes_with(:name => checkBoxName).each do |cb| cb.check if cb.value == cbx_value end } else the_form.checkbox_with(:name => checkBoxName).check end end |
#clear_radio_option(radio_group, radio_option) ⇒ Object Also known as:
Clear a radio button
Usage:
click_radio_option("country", "Australia")
554 555 556 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 554 def clear_radio_option(radio_group, radio_option) radio(:name => radio_group, :value => radio_option).uncheck end |
#click_button_with_caption(caption, opts = {}) ⇒ Object Also known as: , ,
Click a button with caption Usage:
("Confirm payment")
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 435 def (caption, opts={}) = nil found_in_form = nil puts "[DEBUG clieck button] #{caption}" page.forms.each do |the_form| found_in_form = the_form if opts && opts[:index] # wait_before_and_after { = the_form.(:value => caption)[opts[:index]] rescue nil break if # } else # wait_before_and_after { # puts "Find button in form : #{the_form.inspect}" = the_form.(:value => caption) # puts "Find button => #{the_button.inspect}" break if # } end end found_in_form.submit raise "No button found" if .nil? puts "the_button.type => #{the_button.inspect}" if .type == "submit" @browser.submit(found_in_form, found_in_form.(:value => caption)) else found_in_form.(:value => caption).click end end |
#click_button_with_id(id, opts = {}) ⇒ Object
Occasionaly, the button with ID exists, but now shown in the form (debug form.inspect), add efault to submit
TODO button not in
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 354 def (id, opts = {}) = nil page.forms.each_with_index do |the_form, idx| puts "[DEBUG] Click button with id => #{id}, #{the_form.buttons.inspect}, #{idx}" # puts "[DEBUG] click button with id => #{id} | #{opts.inspect} | #{the_form.inspect}" if opts && opts[:index] wait_before_and_after { = the_form.(:id => id)[opts[:index]] } else wait_before_and_after { = the_form.(:id => id) } end if .nil? puts "[DEBUT button with id '#{id}' not found] #{idx}" next end # raise "No button with id: #{id} found " unless the_button # puts "[DEBUG] Submit the form #{the_form.inspect}" if .type == "submit" @browser.submit(the_form, ) else @browser.submit(the_form, ) end end @browser.click() end |
#click_button_with_name(name, opts = {}) ⇒ Object
Click a button with give name Usage:
("confirm")
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 389 def (name, opts={}) the_form = identify_form if opts && opts[:index] wait_before_and_after { = the_form.(:name => name)[opts[:index]] # button(:name => name, :index => opts[:index]).click } else wait_before_and_after { = the_form.(:name => name) } end raise "No button with id: #{id} found " unless if .type == "submit" @browser.submit(the_form, ) else puts "Warning not submit button" @browser.submit(the_form, ) end end |
#click_link_with_id(link_id, opts = {}) ⇒ Object
links
314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 314 def click_link_with_id(link_id, opts = {}) if opts && opts[:index] wait_before_and_after { page.links_with(:id => link_id)[[:index]].click } else wait_before_and_after { page.link_with(:id => link_id).click } end end |
#click_link_with_text(link_text, opts = {}) ⇒ Object Also known as: click_link
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 326 def click_link_with_text(link_text, opts = {}) if opts && opts[:index] wait_before_and_after { page.link_with(:text => link_text)[opts[:index]].click } else wait_before_and_after { begin page.link_with(:text => link_text).click rescue => e # NOTE # e.g. <a href="..."> New client</a>, the link with space # puts "exact link #{link_text} not found, just regex" page.link_with(:text => /#{link_text}/).click end } end end |
#click_radio_option(radio_group, radio_option) ⇒ Object Also known as:
Click a radio button
Usage:
click_radio_option("country", "Australia")
540 541 542 543 544 545 546 547 548 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 540 def click_radio_option(radio_group, radio_option) page.forms.each do |a_form| matching_element = a_form.(:name => radio_group, :value => radio_option) if matching_element matching_element.check break end end end |
#close_browser ⇒ Object Also known as: close
Close the browser window. Useful for automated test suites to reduce test interaction.
228 229 230 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 228 def close_browser @browser = nil end |
#contains_text(text) ⇒ Object
153 154 155 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 153 def contains_text(text) @browser.contains_text(text); end |
#dump_response(stream = nil) ⇒ Object
For deubgging
630 631 632 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 630 def dump_response(stream = nil) stream.nil? ? puts(page_source) : stream.puts(page_source) end |
#element(how, what) ⇒ Object
-
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
97 98 99 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 97 def element(how, what) return @browser.element(how, what) end |
#element_by_id(elem_id) ⇒ Object
Deprecated: using Watir style directly instead
560 561 562 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 560 def element_by_id(elem_id) elem = @browser.document.getElementById(elem_id) end |
#element_source(elementId) ⇒ Object
569 570 571 572 573 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 569 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
564 565 566 567 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 564 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
111 112 113 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 111 def elements(how, what) return @browser.elements(how, what) end |
#enter_text_into_field_with_name(name, value) ⇒ Object Also known as: set_form_element, enter_text
text fields
296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 296 def enter_text_into_field_with_name(name, value) wait_before_and_after { page.forms.each do |the_form| the_form.fields_with(:name => name).each do |x| next unless x.type == "text" || x.type == "password" || x.type == "hidden" || x.type == "textarea" x.value = value break end end } end |
#expect_page(page_clazz, argument = nil) ⇒ Object
Verify the next page following an operation.
Typical usage:
browser.expect_page HomePage
672 673 674 675 676 677 678 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 672 def expect_page(page_clazz, argument = nil) if argument page_clazz.new(self, argument) else page_clazz.new(self) end end |
#full_url(relative_url) ⇒ Object
237 238 239 240 241 242 243 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 237 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("http://www.itest2.com"
....
goto_page("/purchase") # full url => http://www.itest.com/purchase
285 286 287 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 285 def goto_page(page) goto_url full_url(page); end |
#goto_url(url) ⇒ Object
Go to a URL directly
goto_url("http://www.itest2.com/downloads")
291 292 293 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 291 def goto_url(url) @browser.get(url) end |
#identify_form(opts = nil) ⇒ Object Also known as: choose_form
search_form = page.form_with :name => “f”
page.form_with :id => new_client
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 415 def identify_form(opts = nil) puts "[DEBUG] forms_size => #{page.forms.size}" if opts @current_form = page.form_with(opts) @current_form = page.forms.last puts "[DEBUG] select form #{opts} => #{@current_form}" elsif @current_form.nil? @current_form = page.forms.first if @current_form.nil? sleep 0.1 @current_form = page.forms.first end end return @current_form end |
#javascript_dialog ⇒ Object
Watir 1.9
582 583 584 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 582 def javascript_dialog @browser.javascript_dialog 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
131 132 133 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 131 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
149 150 151 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 149 def map(how, what=nil) @browser.map(how, what) end |
#method ⇒ Object
Delegate to Watir
59 60 61 62 63 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 59 [:button, :cell, :checkbox, :div, :form, :frame, :h1, :h2, :h3, :h4, :h5, :h6, :hidden, :image, :li, :link, :map, :pre, :row, :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
81 82 83 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 81 def modal_dialog(how=nil, what=nil) @browser.modal_dialog(how, what) end |
#new_popup_window(options, browser = "ie") ⇒ Object
Attach a Watir::Browser instance to a popup window.
Typical usage
new_popup_window(:url => "http://www.google.com/a.pdf")
613 614 615 616 617 618 619 620 621 622 623 624 625 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 613 def new_popup_window(, browser = "ie") if is_firefox? raise "not implemented" else if [:url] Watir::Browser.attach(:url, [:url]) elsif [:title] Watir::Browser.attach(:title, [:title]) else raise 'Please specify title or url of new pop up window' end end end |
#page ⇒ Object
166 167 168 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 166 def page @browser.current_page end |
#page_source ⇒ Object Also known as: html_body, html
return HTML of current web page
158 159 160 161 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 158 def page_source @browser.page.body #@browser.document.body end |
#page_title ⇒ Object
199 200 201 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 199 def page_title @browser.page.title 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"
661 662 663 664 665 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 661 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 |
#select_file_for_upload(file_field, file_path) ⇒ Object
575 576 577 578 579 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 575 def select_file_for_upload(file_field, file_path) normalized_file_path = RUBY_PLATFORM.downcase.include?("mingw") ? file_path.gsub("/", "\\") : file_path the_form = identify_form the_form.file_uploads.first.file_name = normalized_file_path end |
#select_option(selectName, optionText) ⇒ Object
Select a dropdown list by name Usage:
select_option("country", "Australia")
474 475 476 477 478 479 480 481 482 483 484 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 474 def select_option(selectName, optionText) the_form = identify_form the_select_list = the_form.field_with(:name => selectName) the_select_list..each do |option| if option.text == optionText option.click break end end end |
#set_proxy(address, port, user = nil, password = nil) ⇒ Object
set proxy password
48 49 50 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 48 def set_proxy(address, port, user = nil, password = nil) @browser.set_proxy(address, port, user, password) end |
#show_all_objects ⇒ Object
115 116 117 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 115 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
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 638 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, "#{button}" ) # # this is just cleanup w = nil end end |
#start_window(url = nil) ⇒ Object
586 587 588 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 586 def start_window(url = nil) @browser.start_window(url); end |
#submit(buttonName = nil) ⇒ Object
submit first submit button
487 488 489 490 491 492 493 494 495 496 497 498 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 487 def submit( = nil) if (.nil?) then page.forms.first.submit # buttons.each { |button| # next if button.type != 'submit' # button.click # return # } else () end end |
#text(squeeze_spaces = true) ⇒ Object
return plain text of current web page
171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 171 def text(squeeze_spaces = true) require 'nokogiri' begin page_text_string = Nokogiri::HTML(html).text page_text_string = page_text_string.squeeze(" ") if squeeze_spaces# remove duplicated (spaces) return page_text_string rescue => e puts "failed to santize html source => text, #{e}" end return html end |
#text_sanitize(squeeze_spaces = true) ⇒ Object
Sanitize gem does not work in Java, return empty spaces back
184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 184 def text_sanitize(squeeze_spaces = true) if RUBY_PLATFORM =~ /java/ || RUBY_PLATFORM =~ /jruby/ require 'sanitize' end begin page_text_string = Sanitize.clean(html) page_text_string = page_text_string.squeeze(" ") if squeeze_spaces# remove duplicated (spaces) return page_text_string rescue => e puts "failed to santize html source => text, #{e}" end # @browser.text end |
#uncheck_checkbox(checkBoxName, values = nil) ⇒ Object
UnCheck a checkbox Usage:
uncheck_checkbox("agree")
uncheck_checkbox("agree", "false")
522 523 524 525 526 527 528 529 530 531 532 533 534 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 522 def uncheck_checkbox(checkBoxName, values = nil) the_form = identify_form if values values.class == Array ? arys = values : arys = [values] arys.each {|cbx_value| the_form.checkboxes_with(:name => checkBoxName).each do |cb| cb.uncheck if cb.value == cbx_value end } else the_form.checkbox_with(:name => checkBoxName).uncheck end end |
#url ⇒ Object
current url
214 215 216 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 214 def url @browser.url end |
#wait_before_and_after ⇒ Object
A convenience method to wait at both ends of an operation for the browser to catch up.
265 266 267 268 269 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 265 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.
260 261 |
# File 'lib/rwebspec-mechanize/web_browser.rb', line 260 def wait_for_browser end |