Class: RWebSpec::WebDriver::WebBrowser

Inherits:
Object
  • Object
show all
Includes:
ElementLocator
Defined in:
lib/rwebspec-webdriver/web_browser.rb

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

Class Method Summary collapse

Instance Method Summary collapse

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.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rwebspec-webdriver/web_browser.rb', line 24

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

  case RUBY_PLATFORM
  when /java/i
    initialize_celerity_browser(base_url, options)
  when /mswin|windows|mingw/i
    options[:browser] ||= "ie"
    case options[:browser].to_s
    when "firefox"
      initialize_firefox_browser(existing_browser, base_url, options)
    when "chrome"
      initialize_chrome_browser(existing_browser, base_url, options)
    when "ie"
      initialize_ie_browser(existing_browser, options)
    end

  else
    puts "Ruby Linux or Mac platform: firefox"
    options[:browser] ||= "firefox"
    case options[:browser].to_s
    when "firefox"
      initialize_firefox_browser(existing_browser, base_url, options)
    when "chrome"
      initialize_chrome_browser(existing_browser, base_url, options)
    end
  end
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



22
23
24
# File 'lib/rwebspec-webdriver/web_browser.rb', line 22

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


640
641
642
643
644
645
646
647
648
649
650
# File 'lib/rwebspec-webdriver/web_browser.rb', line 640

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]
  if (options[:browser].to_s == "firefox")
    ff = FireWatir::Firefox.attach(how, what)
    return WebBrowser.new_from_existing(ff, site_context)
  else
    return WebBrowser.new_from_existing(Watir::IE.attach(how, what), site_context)
  end
end

.close_all_browsersObject

TODO determine browser type, check FireWatir support or not



303
304
305
306
307
308
309
# File 'lib/rwebspec-webdriver/web_browser.rb', line 303

def self.close_all_browsers
  if RUBY_PLATFORM.downcase.include?("mswin")
    Watir::IE.close_all
  else
    # raise "not supported in FireFox yet."
  end
end

.is_windows?Boolean

is it running in MS Windows platforms?

Returns:

  • (Boolean)


738
739
740
# File 'lib/rwebspec-webdriver/web_browser.rb', line 738

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



130
131
132
# File 'lib/rwebspec-webdriver/web_browser.rb', line 130

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

.reuse(base_url, options) ⇒ Object

TODO resuse not working yet



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/rwebspec-webdriver/web_browser.rb', line 117

def self.reuse(base_url, options)
  if self.is_windows? && $ITEST2_BROWSER != "Firefox"
    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

Instance Method Details

#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



160
161
162
163
164
165
166
# File 'lib/rwebspec-webdriver/web_browser.rb', line 160

def area(* args)
  if is_firefox?
    text_field(* args)
  else
    @browser.send("area", * args)
  end
end

#backObject Also known as: go_back

TODO can’t browse back if on invalid page



365
366
367
# File 'lib/rwebspec-webdriver/web_browser.rb', line 365

def back
  @browser.navigate.back
end

#base_url=(new_base_url) ⇒ Object



273
274
275
276
277
278
279
# File 'lib/rwebspec-webdriver/web_browser.rb', line 273

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:///



320
321
322
323
324
325
326
# File 'lib/rwebspec-webdriver/web_browser.rb', line 320

def begin_at(relative_url)
  if relative_url =~ /\s*^http/
    @browser.navigate.to relative_url
  else
    @browser.navigate.to full_url(relative_url)
  end
end

#browser_opened?Boolean

Returns:

  • (Boolean)


328
329
330
331
332
333
334
# File 'lib/rwebspec-webdriver/web_browser.rb', line 328

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")


544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
# File 'lib/rwebspec-webdriver/web_browser.rb', line 544

def check_checkbox(checkBoxName, values=nil)
  if values
    values.class == Array ? arys = values : arys = [values]
    elements = find_elements(:name, checkBoxName)
    the_checkbox = elements[0] if elements.size == 1
    if the_checkbox
      the_checkbox.click unless the_checkbox.selected?
      return
    end

    arys.each { |cbx_value|
      elements.each do |elem|
        elem.click if elem.attribute('value') == cbx_value && !the_checkbox.selected?
      end
    }
  else
    the_checkbox = find_element(:name, checkBoxName)
    the_checkbox.click unless the_checkbox.selected?
  end
end

#clear_radio_option(radio_group, radio_option) ⇒ Object Also known as: clear_radio_button

Clear a radio button

Usage:
  click_radio_option("country", "Australia")


603
604
605
606
# File 'lib/rwebspec-webdriver/web_browser.rb', line 603

def clear_radio_option(radio_group, radio_option)
  the_radio_button = find_element(:xpath, "//input[@type='radio' and @name='#{radio_group}' and @value='#{radio_option}']")
  the_radio_button.clear
end

#click_button_with_caption(caption, opts = {}) ⇒ Object Also known as: click_button, click_button_with_text

Click a button with caption

TODO: Caption is same as value

Usage:

click_button_with_caption("Confirm payment")


457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# File 'lib/rwebspec-webdriver/web_browser.rb', line 457

def click_button_with_caption(caption, opts={})
  all_buttons = button_elements
  matching_buttons = all_buttons.select{|x| x.attribute('value') == caption}
  if matching_buttons.size > 0

    if opts && opts[:index]
      puts "Call matching buttons: #{matching_buttons.inspect}"
      first_match = matching_buttons[opts[:index].to_i() - 1]
      first_match.click
    end

    the_button = matching_buttons[0]
    the_button.click

  else
    raise "No button with value: #{caption} found"
  end
end

#click_button_with_id(id, opts = {}) ⇒ Object

Click a button with give HTML id Usage:

click_button_with_id("btn_sumbit")


439
440
441
# File 'lib/rwebspec-webdriver/web_browser.rb', line 439

def click_button_with_id(id, opts = {})
  find_element(:id, id).click
end

#click_button_with_image_src_contains(image_filename) ⇒ Object Also known as: click_button_with_image

Click image buttion with image source name

For an image submit button <input name=“submit” type=“image” src=“/images/search_button.gif”>

click_button_with_image("search_button.gif")


498
499
500
501
502
503
504
505
506
507
508
509
510
# File 'lib/rwebspec-webdriver/web_browser.rb', line 498

def click_button_with_image_src_contains(image_filename)
  all_buttons = button_elements
  found = nil
  all_buttons.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:

click_button_with_name("confirm")
click_button_with_name("confirm", :index => 2)


447
448
449
# File 'lib/rwebspec-webdriver/web_browser.rb', line 447

def click_button_with_name(name, opts={})
  find_element(:name, name).click
end

#click_button_with_value(value, opts = {}) ⇒ Object

click_button_with_caption(“Confirm payment”)



480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'lib/rwebspec-webdriver/web_browser.rb', line 480

def click_button_with_value(value, opts={})
  all_buttons = button_elements
  if opts && opts[:index]
    all_buttons.select{|x| x.attribute('value') == caption}[index]
  else
    all_buttons.each do |button|
      if button.attribute('value') == value then
        button.click
        return
      end
    end
  end
end

links



413
414
415
416
417
418
419
420
# File 'lib/rwebspec-webdriver/web_browser.rb', line 413

def click_link_with_id(link_id, opts = {})
  if opts && opts[:index]
    elements = find_elements(:id, link_id)
    elements[opts[:index]-1].click
  else
    find_element(:id, link_id).click
  end
end

click_link_with_text(“Login”) click_link_with_text(“Show”, :index => 2)



425
426
427
428
429
430
431
432
# File 'lib/rwebspec-webdriver/web_browser.rb', line 425

def click_link_with_text(link_text, opts = {})
  if opts && opts[:index]
    elements = find_elements(:link_text, link_text)
    elements[opts[:index]-1].click
  else
    find_element(:link_text, link_text).click
  end
end

#click_radio_option(radio_group, radio_option) ⇒ Object Also known as: click_radio_button

Click a radio button

Usage:
  click_radio_option("country", "Australia")


594
595
596
597
# File 'lib/rwebspec-webdriver/web_browser.rb', line 594

def click_radio_option(radio_group, radio_option)
  the_radio_button = find_element(:xpath, "//input[@type='radio' and @name='#{radio_group}' and @value='#{radio_option}']")
  the_radio_button.click
end

#close_browserObject Also known as: close

Close the browser window. Useful for automated test suites to reduce test interaction.



296
297
298
299
# File 'lib/rwebspec-webdriver/web_browser.rb', line 296

def close_browser
  @browser.quit
  sleep 1
end

#contains_text(text) ⇒ Object



240
241
242
# File 'lib/rwebspec-webdriver/web_browser.rb', line 240

def contains_text(text)
  @browser.contains_text(text);
end

#current_urlObject Also known as: url

current url



268
269
270
# File 'lib/rwebspec-webdriver/web_browser.rb', line 268

def current_url
  @browser.current_url
end

#dump_response(stream = nil) ⇒ Object


For deubgging




673
674
675
# File 'lib/rwebspec-webdriver/web_browser.rb', line 673

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


184
185
186
# File 'lib/rwebspec-webdriver/web_browser.rb', line 184

def element(how, what)
  return @browser.element(how, what)
end

#element_by_id(elem_id) ⇒ Object



609
610
611
# File 'lib/rwebspec-webdriver/web_browser.rb', line 609

def element_by_id(elem_id)
  @browser.find_element(:id, elem_id)
end

#element_source(elementId) ⇒ Object



617
618
619
620
621
# File 'lib/rwebspec-webdriver/web_browser.rb', line 617

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



613
614
615
# File 'lib/rwebspec-webdriver/web_browser.rb', line 613

def element_value(elementId)
  find_element(:id, elementId).attribute('value')
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


198
199
200
# File 'lib/rwebspec-webdriver/web_browser.rb', line 198

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



391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/rwebspec-webdriver/web_browser.rb', line 391

def enter_text_into_field_with_name(name, text)
  the_element = find_element(:name, name)
  if the_element.tag_name == "input" || the_element.tag_name == "textarea" then
    the_element.clear
    the_element.send_keys(text)
  else
    elements = find_elements(:name, name)
    if elements.size == 1 then
      elements[0].send_keys(text)
    else
      element_set = elements.select {|x| x.tag_name == "textarea" || (x.tag_name == "input" &&  x.attribute("text")) }
      element_set[0].send_keys(text)
    end
  end
  return true
end

#expect_page(page_clazz, argument = nil) ⇒ Object

Verify the next page following an operation.

Typical usage:

browser.expect_page HomePage


729
730
731
732
733
734
735
# File 'lib/rwebspec-webdriver/web_browser.rb', line 729

def expect_page(page_clazz, argument = nil)
  if argument
    page_clazz.new(self, argument)
  else
    page_clazz.new(self)
  end
end

#find_element(*args) ⇒ Object



134
135
136
# File 'lib/rwebspec-webdriver/web_browser.rb', line 134

def find_element(* args)
  @browser.send("find_element", *args)
end

#find_elements(*args) ⇒ Object



138
139
140
# File 'lib/rwebspec-webdriver/web_browser.rb', line 138

def find_elements(* args)
  @browser.send("find_elements", *args)
end

#firefoxObject

return underlying firefox browser object, raise error if not running using Firefox



705
706
707
# File 'lib/rwebspec-webdriver/web_browser.rb', line 705

def firefox
  is_firefox?  ? @browser : nil;
end

#forwardObject Also known as: go_forward



359
360
361
# File 'lib/rwebspec-webdriver/web_browser.rb', line 359

def forward
  @browser.navigate().forward
end

#full_url(relative_url) ⇒ Object



311
312
313
314
315
316
317
# File 'lib/rwebspec-webdriver/web_browser.rb', line 311

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


380
381
382
# File 'lib/rwebspec-webdriver/web_browser.rb', line 380

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")


386
387
388
# File 'lib/rwebspec-webdriver/web_browser.rb', line 386

def goto_url(url)
  @browser.navigate.to url
end

#htmlunitObject



709
710
711
712
# File 'lib/rwebspec-webdriver/web_browser.rb', line 709

def htmlunit
  raise "can't call this as it is configured to use Celerity" unless RUBY_PLATFORM =~ /java/
  @browser
end

#ieObject

return underlying browser



700
701
702
# File 'lib/rwebspec-webdriver/web_browser.rb', line 700

def ie
  @browser.class == "internet_explorer"  ? @browser : nil;
end

#initialize_chrome_browser(existing_browser, base_url, options) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/rwebspec-webdriver/web_browser.rb', line 69

def initialize_chrome_browser(existing_browser, base_url, options)
  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



59
60
61
62
63
64
65
66
67
# File 'lib/rwebspec-webdriver/web_browser.rb', line 59

def initialize_firefox_browser(existing_browser, base_url, options)
  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



79
80
81
82
83
84
85
# File 'lib/rwebspec-webdriver/web_browser.rb', line 79

def initialize_htmlunit_browser(base_url, options)
  #      default_celerity_options = {:proxy => nil, :browser => :firefox, :resynchronize => true, :log_level => :off}
  #      options = default_celerity_options.merge options
  #      options.each { |k, v| options.delete(k) unless default_celerity_options.keys.include?(k) }
  @browser = Selenium::WebDriver.for :htmlunit
  @browser.navigate.to base_url
end

#initialize_ie_browser(existing_browser, options) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rwebspec-webdriver/web_browser.rb', line 87

def initialize_ie_browser(existing_browser, options)
  if existing_browser then
    @browser = existing_browser
    if $ITEST2_EMULATE_TYPING && $ITEST2_TYPING_SPEED then
      @browser.set_slow_speed if $ITEST2_TYPING_SPEED == 'slow'
      @browser.set_fast_speed if $ITEST2_TYPING_SPEED == 'fast'
    else
      @browser.speed = :zippy
    end
    return
  end

  @browser = Selenium::WebDriver.for :ie
  #      if $ITEST2_EMULATE_TYPING && $ITEST2_TYPING_SPEED then
  #        @browser.set_slow_speed if $ITEST2_TYPING_SPEED == 'slow'
  #        @browser.set_fast_speed if $ITEST2_TYPING_SPEED == 'fast'
  #      else
  #        @browser.speed = :zippy
  #      end
  #      @browser.activeObjectHighLightColor = options[:highlight_colour]
  #      @browser.visible = options[:visible] unless $HIDE_IE
  #      #NOTE: close_others fails
  #      if RUBY_VERSION =~ /^1\.8/ && options[:close_others] then
  #        @browser.close_others
  #      else
  #        puts "close other browser instances not working yet in Ruby 1.9.1 version of Watir"
  #      end
end

#is_firefox?Boolean

Returns:

  • (Boolean)


290
291
292
# File 'lib/rwebspec-webdriver/web_browser.rb', line 290

def is_firefox?
  @browser.browser.to_s == "firefox"
end

#is_ie?Boolean

Returns:

  • (Boolean)


285
286
287
288
# File 'lib/rwebspec-webdriver/web_browser.rb', line 285

def is_ie?
  puts @browser.browser.to_s
  @browser.browser.to_s == "ie"
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


218
219
220
# File 'lib/rwebspec-webdriver/web_browser.rb', line 218

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


236
237
238
# File 'lib/rwebspec-webdriver/web_browser.rb', line 236

def map(how, what=nil)
  @browser.map(how, what)
end

#methodObject

Delegate to WebDriver



145
146
147
148
149
150
151
152
# File 'lib/rwebspec-webdriver/web_browser.rb', line 145

[: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|
  tag_name = method
  define_method method do |* args|
    if args.size == 2 then
      find_element(args[0].to_sym, args[1])
    end
  end
end


168
169
170
# File 'lib/rwebspec-webdriver/web_browser.rb', line 168

def modal_dialog(how=nil, what=nil)
  @browser.modal_dialog(how, what)
end

#new_popup_window(options, browser = "ie") ⇒ Object

Attach a Watir::IE instance to a popup window.

Typical usage

new_popup_window(:url => "http://www.google.com/a.pdf")


656
657
658
659
660
661
662
663
664
665
666
667
668
# File 'lib/rwebspec-webdriver/web_browser.rb', line 656

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

#page_sourceObject Also known as: html_body, html

return HTML of current web page



245
246
247
# File 'lib/rwebspec-webdriver/web_browser.rb', line 245

def page_source
  @browser.page_source
end

#page_titleObject



251
252
253
# File 'lib/rwebspec-webdriver/web_browser.rb', line 251

def page_title
  @browser.title
end

#refreshObject Also known as: refresh_page



370
371
372
# File 'lib/rwebspec-webdriver/web_browser.rb', line 370

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"


718
719
720
721
722
# File 'lib/rwebspec-webdriver/web_browser.rb', line 718

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



623
624
625
626
627
# File 'lib/rwebspec-webdriver/web_browser.rb', line 623

def select_file_for_upload(file_field, file_path)
  is_on_windows = RUBY_PLATFORM.downcase.include?("mingw") || RUBY_PLATFORM.downcase.include?("mswin")
  normalized_file_path = is_on_windows ? file_path.gsub("/", "\\") : file_path
  file_field(:name, file_field).set(normalized_file_path)
end

#select_option(selectName, text) ⇒ Object

Select a dropdown list by name Usage:

select_option("country", "Australia")


516
517
518
519
520
521
522
523
524
525
# File 'lib/rwebspec-webdriver/web_browser.rb', line 516

def select_option(selectName, text)
  select_box = find_element(:name, selectName)

  options = select_box.find_elements(:tag_name, "option")
  options.each do |opt|
    # puts opt.methods
    opt.click if text ==  opt.text
  end
  # select_list(:name, selectName).select(option)
end

#show_all_objectsObject



202
203
204
# File 'lib/rwebspec-webdriver/web_browser.rb', line 202

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



681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
# File 'lib/rwebspec-webdriver/web_browser.rb', line 681

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

#start_window(url = nil) ⇒ Object



629
630
631
# File 'lib/rwebspec-webdriver/web_browser.rb', line 629

def start_window(url = nil)
  @browser.start_window(url);
end

#submit(buttonName = nil) ⇒ Object

submit first submit button



528
529
530
531
532
533
534
535
536
537
538
# File 'lib/rwebspec-webdriver/web_browser.rb', line 528

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

#textObject

TODO return plain text of current web page



256
257
258
259
# File 'lib/rwebspec-webdriver/web_browser.rb', line 256

def text
  Sanitize.clean(html)
  # @browser.text
end

#uncheck_checkbox(checkBoxName, values = nil) ⇒ Object

Check a checkbox Usage:

uncheck_checkbox("agree")
uncheck_checkbox("agree", "false")


569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
# File 'lib/rwebspec-webdriver/web_browser.rb', line 569

def uncheck_checkbox(checkBoxName, values = nil)
  if values
    values.class == Array ? arys = values : arys = [values]
    elements = find_elements(:name, checkBoxName)
    the_checkbox = elements[0] if elements.size == 1
    if the_checkbox
      the_checkbox.click if the_checkbox.selected?
      return
    end

    arys.each { |cbx_value|
      elements.each do |elem|
        elem.click if elem.attribute('value') == cbx_value && the_checkbox && the_checkbox.selected?
      end
    }
  else
    the_checkbox = find_element(:name, checkBoxName)
    the_checkbox.click if the_checkbox.selected?
  end
end

#underlying_browserObject



281
282
283
# File 'lib/rwebspec-webdriver/web_browser.rb', line 281

def underlying_browser
  @browser
end

#wait_before_and_afterObject

A convenience method to wait at both ends of an operation for the browser to catch up.



346
347
348
349
350
# File 'lib/rwebspec-webdriver/web_browser.rb', line 346

def wait_before_and_after
  wait_for_browser
  yield
  wait_for_browser
end

#wait_for_browserObject

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.



339
340
341
# File 'lib/rwebspec-webdriver/web_browser.rb', line 339

def wait_for_browser
  # NOTE: no need any more
end