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.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rwebspec-webdriver/web_browser.rb', line 23

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
  
  options[:browser] ||= "ie" if RUBY_PLATFORM =~ /mingw/
  case options[:browser].to_s.downcase
    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)
    when  "htmlunit"
       initialize_htmlunit_browser(base_url, options)
  end
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



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

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


635
636
637
638
639
640
641
642
643
644
645
# File 'lib/rwebspec-webdriver/web_browser.rb', line 635

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



309
310
311
# File 'lib/rwebspec-webdriver/web_browser.rb', line 309

def self.close_all_browsers
  raise "not implemented"
end

.is_windows?Boolean

is it running in MS Windows platforms?

Returns:

  • (Boolean)


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

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



121
122
123
# File 'lib/rwebspec-webdriver/web_browser.rb', line 121

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



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/rwebspec-webdriver/web_browser.rb', line 108

def self.reuse(base_url, options)
  if self.is_windows? && $TESTWISE_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



151
152
153
154
155
156
157
# File 'lib/rwebspec-webdriver/web_browser.rb', line 151

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



367
368
369
# File 'lib/rwebspec-webdriver/web_browser.rb', line 367

def back
  @browser.navigate.back
end

#base_url=(new_base_url) ⇒ Object



276
277
278
279
280
281
282
# File 'lib/rwebspec-webdriver/web_browser.rb', line 276

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



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

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)


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

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


539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# File 'lib/rwebspec-webdriver/web_browser.rb', line 539

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 && !elem.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")


598
599
600
601
# File 'lib/rwebspec-webdriver/web_browser.rb', line 598

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


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

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


441
442
443
# File 'lib/rwebspec-webdriver/web_browser.rb', line 441

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


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

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)


449
450
451
# File 'lib/rwebspec-webdriver/web_browser.rb', line 449

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



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

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



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

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)



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

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


589
590
591
592
# File 'lib/rwebspec-webdriver/web_browser.rb', line 589

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.



302
303
304
305
# File 'lib/rwebspec-webdriver/web_browser.rb', line 302

def close_browser
  @browser.quit
  sleep 1
end

#contains_text(text) ⇒ Object



231
232
233
# File 'lib/rwebspec-webdriver/web_browser.rb', line 231

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

#current_urlObject Also known as: url

current url



271
272
273
# File 'lib/rwebspec-webdriver/web_browser.rb', line 271

def current_url
  @browser.current_url
end

#driverObject



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

def driver
  @browser
end

#dump_response(stream = nil) ⇒ Object


For deubgging




668
669
670
# File 'lib/rwebspec-webdriver/web_browser.rb', line 668

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


175
176
177
# File 'lib/rwebspec-webdriver/web_browser.rb', line 175

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

#element_by_id(elem_id) ⇒ Object



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

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

#element_source(elementId) ⇒ Object



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

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



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

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


189
190
191
# File 'lib/rwebspec-webdriver/web_browser.rb', line 189

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



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

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


724
725
726
727
728
729
730
# File 'lib/rwebspec-webdriver/web_browser.rb', line 724

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



125
126
127
# File 'lib/rwebspec-webdriver/web_browser.rb', line 125

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

#find_elements(*args) ⇒ Object



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

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

#firefoxObject

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



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

def firefox
  is_firefox?  ? @browser : nil;
end

#forwardObject Also known as: go_forward



361
362
363
# File 'lib/rwebspec-webdriver/web_browser.rb', line 361

def forward
  @browser.navigate().forward
end

#full_url(relative_url) ⇒ Object



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

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


382
383
384
# File 'lib/rwebspec-webdriver/web_browser.rb', line 382

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


388
389
390
# File 'lib/rwebspec-webdriver/web_browser.rb', line 388

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

#htmlunitObject



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

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

#ieObject

return underlying browser



695
696
697
# File 'lib/rwebspec-webdriver/web_browser.rb', line 695

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

#initialize_chrome_browser(existing_browser, base_url, options) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/rwebspec-webdriver/web_browser.rb', line 55

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



45
46
47
48
49
50
51
52
53
# File 'lib/rwebspec-webdriver/web_browser.rb', line 45

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



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

def initialize_htmlunit_browser(base_url, options)
  puts "XXXXX start HtmlUnit..."
  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

#initialize_ie_browser(existing_browser, options) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rwebspec-webdriver/web_browser.rb', line 78

def initialize_ie_browser(existing_browser, options)
  if existing_browser then
    @browser = existing_browser
    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
  end

  @browser = Selenium::WebDriver.for :ie
  #      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
  #      @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)


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

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

#is_ie?Boolean

Returns:

  • (Boolean)


292
293
294
# File 'lib/rwebspec-webdriver/web_browser.rb', line 292

def is_ie?
  @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


209
210
211
# File 'lib/rwebspec-webdriver/web_browser.rb', line 209

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


227
228
229
# File 'lib/rwebspec-webdriver/web_browser.rb', line 227

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

#methodObject

Delegate to WebDriver



136
137
138
139
140
141
142
143
# File 'lib/rwebspec-webdriver/web_browser.rb', line 136

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


159
160
161
# File 'lib/rwebspec-webdriver/web_browser.rb', line 159

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


651
652
653
654
655
656
657
658
659
660
661
662
663
# File 'lib/rwebspec-webdriver/web_browser.rb', line 651

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



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

def page_source
  @browser.page_source
end

#page_titleObject



242
243
244
# File 'lib/rwebspec-webdriver/web_browser.rb', line 242

def page_title
  @browser.title
end

#refreshObject Also known as: refresh_page



372
373
374
# File 'lib/rwebspec-webdriver/web_browser.rb', line 372

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"


713
714
715
716
717
# File 'lib/rwebspec-webdriver/web_browser.rb', line 713

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



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

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


518
519
520
# File 'lib/rwebspec-webdriver/web_browser.rb', line 518

def select_option(selectName, text)	
				Selenium::WebDriver::Support::Select.new(find_element(:name, selectName)).select_by(:text, text)
end

#show_all_objectsObject



193
194
195
# File 'lib/rwebspec-webdriver/web_browser.rb', line 193

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



676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
# File 'lib/rwebspec-webdriver/web_browser.rb', line 676

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



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

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

#submit(buttonName = nil) ⇒ Object

submit first submit button



523
524
525
526
527
528
529
530
531
532
533
# File 'lib/rwebspec-webdriver/web_browser.rb', line 523

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

#text(squeeze_spaces = true) ⇒ Object



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

def text(squeeze_spaces = true)
				@browser.find_element(:tag_name, "body").text
end

#text_with_sanitizeObject

Deprecated.


251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/rwebspec-webdriver/web_browser.rb', line 251

def text_with_sanitize				
	begin
		require 'sanitize'
     	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}"
		return @browser.html
	end
end

#uncheck_checkbox(checkBoxName, values = nil) ⇒ Object

Check a checkbox Usage:

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


564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
# File 'lib/rwebspec-webdriver/web_browser.rb', line 564

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



288
289
290
# File 'lib/rwebspec-webdriver/web_browser.rb', line 288

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.



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

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.



341
342
343
# File 'lib/rwebspec-webdriver/web_browser.rb', line 341

def wait_for_browser
  # NOTE: no need any more
end