Class: Watir::IE

Inherits:
Object show all
Includes:
Container, Exception, PageContainer, WaitHelper
Defined in:
lib/watir-classic/process.rb,
lib/watir-classic/ie-class.rb,
lib/watir-classic/close_all.rb,
lib/watir-classic/ie-process.rb,
lib/watir-classic/contrib/ie-new-process.rb

Defined Under Namespace

Classes: Process

Constant Summary collapse

READYSTATES =

Used internally to determine when IE has finished loading a page

{:complete => 4}
HIGHLIGHT_COLOR =

The default color for highlighting objects as they are accessed.

'yellow'
@@attach_timeout =

Maximum number of seconds to wait when attaching to a window

2.0
@@speed =

The globals $FAST_SPEED and $HIDE_IE are checked both at initialization and later, because they might be set after initialization. Setting them beforehand (e.g. from the command line) will affect the class, otherwise it is only a temporary effect

$FAST_SPEED ? :fast : :slow
@@visible =
$HIDE_IE ? false : true
@@zero_based_indexing =
true

Constants included from Win32

Win32::FindWindowEx, Win32::GW_CHILD, Win32::GW_ENABLEDPOPUP, Win32::GW_HWNDFIRST, Win32::GW_HWNDLAST, Win32::GW_HWNDNEXT, Win32::GW_HWNDPREV, Win32::GW_MAX, Win32::GW_OWNER, Win32::GetUnknown, Win32::GetWindow, Win32::IsWindow, Win32::User32

Instance Attribute Summary collapse

Attributes included from Container

#activeObjectHighLightColor, #page_container, #type_keys, #typingspeed

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PageContainer

#check_for_http_error, #contains_text, #enabled_popup, #execute_script, #html, #set_container, #text

Methods included from Win32

window_exists?

Methods included from Exception

message_for_unable_to_locate

Methods included from Container

#__ole_inner_elements, #alert, #locator_for, #modal_dialog, #set_container, support_element

Methods included from WaitHelper

#wait_until, #wait_while

Constructor Details

#initialize(suppress_new_window = nil) ⇒ IE

Create an IE browser.



89
90
91
# File 'lib/watir-classic/ie-class.rb', line 89

def initialize suppress_new_window=nil
  _new_window_init unless suppress_new_window
end

Instance Attribute Details

#down_load_timeObject (readonly)

The time, in seconds, it took for the new page to load after executing the the last command



73
74
75
# File 'lib/watir-classic/ie-class.rb', line 73

def down_load_time
  @down_load_time
end

#hwndObject

Return the current window handle



320
321
322
323
# File 'lib/watir-classic/ie-class.rb', line 320

def hwnd
  raise "Not attached to a browser" if @ie.nil?
  @hwnd ||= @ie.hwnd
end

#ieObject

the OLE Internet Explorer object



76
77
78
# File 'lib/watir-classic/ie-class.rb', line 76

def ie
  @ie
end

#process_idObject



18
19
20
# File 'lib/watir-classic/contrib/ie-new-process.rb', line 18

def process_id
  @process_id ||= IEProcess.process_id_from_hwnd @ie.hwnd
end

#url_listObject (readonly)

this contains the list of unique urls that have been visited



79
80
81
# File 'lib/watir-classic/ie-class.rb', line 79

def url_list
  @url_list
end

Class Method Details

._find(how, what) ⇒ Object



276
277
278
# File 'lib/watir-classic/ie-class.rb', line 276

def self._find(how, what)
  self._find_all(how, what).first
end

._find_all(how, what) ⇒ Object



280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/watir-classic/ie-class.rb', line 280

def self._find_all(how, what)
  ies = []
  count = -1
  IE.each do |ie|
    window = ie.ie

    case how
    when :url
      ies << window if (what.matches(window.locationURL))
    when :title
      # normal windows explorer shells do not have document
      # note window.document will fail for "new" browsers
      begin
        title = window.locationname
        title = window.document.title
      rescue WIN32OLERuntimeError
      end
      ies << window if what.matches(title)
    when :hwnd
      begin
        ies << window if what == window.HWND
      rescue WIN32OLERuntimeError
      end
    when :index
      count += 1
      if count == what
        ies << window
        break
      end
    when nil
      ies << window
    else
      raise ArgumentError
    end
  end      

  ies
end

.attach(how, what) ⇒ Object

Return a Watir::IE object for an existing IE window. Window can be referenced by url, title, or window handle. Second argument can be either a string or a regular expression in the case of of :url or :title. IE.attach(:url, ‘www.google.com’) IE.attach(:title, ‘Google’) IE.attach(:hwnd, 528140) This method will not work when Watir/Ruby is run under a service (instead of a user).



147
148
149
150
151
# File 'lib/watir-classic/ie-class.rb', line 147

def self.attach how, what
  ie = new true # don't create window
  ie._attach_init(how, what)
  ie
end

.attach_timeoutObject

default value



10
11
12
# File 'lib/watir-classic/ie-class.rb', line 10

def self.attach_timeout
  @@attach_timeout
end

.attach_timeout=(timeout) ⇒ Object



13
14
15
# File 'lib/watir-classic/ie-class.rb', line 13

def self.attach_timeout=(timeout)
  @@attach_timeout = timeout
end

.base_indexObject



61
62
63
# File 'lib/watir-classic/ie-class.rb', line 61

def self.base_index
  self.zero_based_indexing ? 0 : 1
end

.bind(window) ⇒ Object

Return an IE object that wraps the given window, typically obtained from Shell.Application.windows.



162
163
164
165
166
167
# File 'lib/watir-classic/ie-class.rb', line 162

def self.bind window
  ie = new true
  ie.ie = window
  ie.initialize_options
  ie
end

.close_allObject

close all ie browser windows



6
7
8
# File 'lib/watir-classic/close_all.rb', line 6

def self.close_all
  close_all_but nil
end

.eachObject

Yields successively to each IE window on the current desktop. Takes a block. This method will not work when Watir/Ruby is run under a service (instead of a user). Yields to the window and its hwnd.



234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/watir-classic/ie-class.rb', line 234

def self.each
  shell = WIN32OLE.new('Shell.Application')
  ie_browsers = []
  shell.Windows.each do |window|
    next unless (window.path =~ /Internet Explorer/ rescue false)
    next unless (hwnd = window.hwnd rescue false)
    ie = IE.bind(window)
    ie.hwnd = hwnd
    ie_browsers << ie
  end
  ie_browsers.each do |ie|
    yield ie
  end
end

.find(how, what) ⇒ Object

return internet explorer instance as specified. if none is found, return nil. arguments:

:url, url -- the URL of the IE browser window
:title, title -- the title of the browser page
:hwnd, hwnd -- the window handle of the browser window.

This method will not work when Watir/Ruby is run under a service (instead of a user).



271
272
273
274
# File 'lib/watir-classic/ie-class.rb', line 271

def self.find(how, what)
  ie_ole = IE._find(how, what)
  IE.bind ie_ole if ie_ole
end

.new_processObject

Create a new IE window in a new process. This method will not work when Watir/Ruby is run under a service (instead of a user).



116
117
118
119
120
# File 'lib/watir-classic/ie-class.rb', line 116

def self.new_process
  ie = new true
  ie._new_process_init
  ie
end

.new_windowObject

Create a new IE window. Works just like IE.new in Watir 1.4.



82
83
84
85
86
# File 'lib/watir-classic/ie-class.rb', line 82

def self.new_window
  ie = new true
  ie._new_window_init
  ie
end

.optionsObject

Return the options used when creating new instances of IE. BUG: this interface invites misunderstanding/misuse such as IE.options = :zippy]



19
20
21
# File 'lib/watir-classic/ie-class.rb', line 19

def self.options
  {:speed => self.speed, :visible => self.visible, :attach_timeout => self.attach_timeout, :zero_based_indexing => self.zero_based_indexing}
end

.process_countObject

Returns the number of IEXPLORE processes currently running.



16
17
18
# File 'lib/watir-classic/process.rb', line 16

def self.process_count
  Watir::Process.count 'iexplore.exe'
end

.set_options(options) ⇒ Object

set values for options used when creating new instances of IE.



23
24
25
26
27
# File 'lib/watir-classic/ie-class.rb', line 23

def self.set_options options
  options.each do |name, value|
    send "#{name}=", value
  end
end

.speedObject



34
35
36
37
# File 'lib/watir-classic/ie-class.rb', line 34

def self.speed
  return :fast if $FAST_SPEED
  @@speed
end

.speed=(x) ⇒ Object



38
39
40
41
# File 'lib/watir-classic/ie-class.rb', line 38

def self.speed= x
  $FAST_SPEED = nil
  @@speed = x
end

.start(url = nil) ⇒ Object

Create a new IE Window, starting at the specified url. If no url is given, start empty.



101
102
103
# File 'lib/watir-classic/ie-class.rb', line 101

def self.start url=nil
  start_window url
end

.start_process(url = nil) ⇒ Object

Create a new IE window in a new process, starting at the specified URL. Same as IE.start.



132
133
134
135
136
# File 'lib/watir-classic/ie-class.rb', line 132

def self.start_process url=nil
  ie = new_process
  ie.goto url if url
  ie
end

.start_window(url = nil) ⇒ Object

Create a new IE window, starting at the specified url. If no url is given, start empty. Works like IE.start in Watir 1.4.



107
108
109
110
111
# File 'lib/watir-classic/ie-class.rb', line 107

def self.start_window url=nil
  ie = new_window
  ie.goto url if url
  ie
end

.versionObject



249
250
251
252
253
254
255
256
257
# File 'lib/watir-classic/ie-class.rb', line 249

def self.version
  @ie_version ||= begin
                    require 'win32/registry'
                    ::Win32::Registry::HKEY_LOCAL_MACHINE.open("SOFTWARE\\Microsoft\\Internet Explorer") do |ie_key|
                      ie_key.read('Version').last
                    end
                    # OR: ::WIN32OLE.new("WScript.Shell").RegRead("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Internet Explorer\\Version")
                  end
end

.version_partsObject



259
260
261
# File 'lib/watir-classic/ie-class.rb', line 259

def self.version_parts
  version.split('.')
end

.visibleObject



43
44
45
46
# File 'lib/watir-classic/ie-class.rb', line 43

def self.visible
  return false if $HIDE_IE
  @@visible
end

.visible=(x) ⇒ Object



47
48
49
50
# File 'lib/watir-classic/ie-class.rb', line 47

def self.visible= x
  $HIDE_IE = nil
  @@visible = x
end

.zero_based_indexingObject



57
58
59
# File 'lib/watir-classic/ie-class.rb', line 57

def self.zero_based_indexing
  @@zero_based_indexing
end

.zero_based_indexing=(enabled) ⇒ Object



53
54
55
# File 'lib/watir-classic/ie-class.rb', line 53

def self.zero_based_indexing= enabled
  @@zero_based_indexing = enabled
end

Instance Method Details

#_attach_init(how, what) ⇒ Object

this method is used internally to attach to an existing window



154
155
156
157
158
# File 'lib/watir-classic/ie-class.rb', line 154

def _attach_init how, what
  attach_browser_window how, what
  initialize_options
  wait
end

#_new_process_initObject



122
123
124
125
126
127
128
# File 'lib/watir-classic/ie-class.rb', line 122

def _new_process_init
  iep = Process.start
  @ie = iep.window
  @process_id = iep.process_id
  initialize_options
  goto 'about:blank'
end

#_new_window_initObject



93
94
95
96
97
# File 'lib/watir-classic/ie-class.rb', line 93

def _new_window_init
  create_browser_window
  initialize_options
  goto 'about:blank' # this avoids numerous problems caused by lack of a document
end

#activateObject Also known as: bring_to_front

Make the window come to the front



435
436
437
# File 'lib/watir-classic/ie-class.rb', line 435

def activate
  rautomation.activate
end

#active?Boolean Also known as: front?

Returns:

  • (Boolean)


440
441
442
# File 'lib/watir-classic/ie-class.rb', line 440

def active?
  rautomation.active?
end

#add_checker(checker) ⇒ Object

this method is used to add an error checker that gets executed on every page load

  • checker Proc Object, that contains the code to be run



572
573
574
# File 'lib/watir-classic/ie-class.rb', line 572

def add_checker(checker)
  @error_checkers << checker
end

#attach_commandObject



589
590
591
# File 'lib/watir-classic/ie-class.rb', line 589

def attach_command
  "Watir::IE.attach(:hwnd, #{hwnd})"
end

#autoitObject



450
451
452
453
454
# File 'lib/watir-classic/ie-class.rb', line 450

def autoit
  Kernel.warn "Usage of Watir::IE#autoit method is DEPRECATED! Use Watir::IE#rautomation method instead. Refer to https://github.com/jarmo/RAutomation for updating your scripts."
  @autoit ||= ::RAutomation::Window.new(:hwnd => hwnd, :adapter => :autoit)
  @autoit
end

#backObject

Go to the previous page - the same as clicking the browsers back button an WIN32OLERuntimeError exception is raised if the browser cant go back



371
372
373
374
# File 'lib/watir-classic/ie-class.rb', line 371

def back
  @ie.GoBack
  wait
end

#clear_url_listObject

clear the list of urls that we have visited



395
396
397
# File 'lib/watir-classic/ie-class.rb', line 395

def clear_url_list
  @url_list.clear
end

#closeObject

Closes the Browser



400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/watir-classic/ie-class.rb', line 400

def close
  return unless exists?
  @ie.stop
  wait rescue nil
  chwnd = @ie.hwnd.to_i
  @ie.quit
  t = ::Time.now
  while exists?
    # just in case to avoid possible endless loop if failing to close some
    # window or tab
    break if ::Time.now - t > 10
    sleep 0.3
  end
end

#close_modalObject

close modal dialog. unlike IE#modal_dialog.close, does not wait for dialog to appear and does not raise exception if no window is found. returns true if modal was found and close, otherwise false



25
26
27
28
29
# File 'lib/watir-classic/close_all.rb', line 25

def close_modal
  while self.modal_dialog.exists? do
    self.modal_dialog.close
  end
end

#close_othersObject

find other ie browser windows and close them



10
11
12
# File 'lib/watir-classic/close_all.rb', line 10

def close_others
  IE.close_all_but self
end

#cookiesObject



500
501
502
# File 'lib/watir-classic/ie-class.rb', line 500

def cookies
  Cookies.new(self)
end

#dirObject



468
469
470
# File 'lib/watir-classic/ie-class.rb', line 468

def dir
  return File.expand_path(File.dirname(__FILE__))
end

#disable_checker(checker) ⇒ Object

this allows a checker to be disabled

  • checker Proc Object, the checker that is to be disabled



578
579
580
# File 'lib/watir-classic/ie-class.rb', line 578

def disable_checker(checker)
  @error_checkers.delete(checker)
end

#documentObject

Return the current document



477
478
479
# File 'lib/watir-classic/ie-class.rb', line 477

def document
  return @ie.document
end

#exists?Boolean Also known as: exist?

Are we attached to an open browser?

Returns:

  • (Boolean)


331
332
333
334
335
336
337
# File 'lib/watir-classic/ie-class.rb', line 331

def exists?
  begin
    !!(@ie.name =~ /Internet Explorer/)
  rescue WIN32OLERuntimeError, NoMethodError
    false
  end
end

#focusObject

Gives focus to the frame



583
584
585
586
587
# File 'lib/watir-classic/ie-class.rb', line 583

def focus
  active_element = document.activeElement
  active_element.blur unless active_element.tagName == "BODY"
  document.focus
end

#forwardObject

Go to the next page - the same as clicking the browsers forward button an WIN32OLERuntimeError exception is raised if the browser cant go forward



378
379
380
381
# File 'lib/watir-classic/ie-class.rb', line 378

def forward
  @ie.GoForward
  wait
end

#goto(url) ⇒ Object

Navigate to the specified URL.

* url - string - the URL to navigate to


362
363
364
365
366
367
# File 'lib/watir-classic/ie-class.rb', line 362

def goto(url)
  url = "http://" + url unless url =~ %r{://} || url == "about:blank"
  @ie.navigate(url)
  wait
  return @down_load_time
end

#initialize_optionsObject



169
170
171
172
173
174
175
176
177
178
# File 'lib/watir-classic/ie-class.rb', line 169

def initialize_options
  self.visible = IE.visible
  self.speed = IE.speed

  @ole_object = nil
  @page_container = self
  @error_checkers = []
  @activeObjectHighLightColor = HIGHLIGHT_COLOR
  @url_list = []
end

#inspectObject



390
391
392
# File 'lib/watir-classic/ie-class.rb', line 390

def inspect
  '#<%s:0x%x url=%s title=%s>' % [self.class, hash*2, url.inspect, title.inspect]
end

#killObject



22
23
24
25
# File 'lib/watir-classic/contrib/ie-new-process.rb', line 22

def kill
  iep = IEProcess.new process_id
  iep.stop
end

#maximizeObject

Maximize the window (expands to fill the screen)



416
417
418
# File 'lib/watir-classic/ie-class.rb', line 416

def maximize
  rautomation.maximize
end

#minimizeObject

Minimize the window (appears as icon on taskbar)



421
422
423
# File 'lib/watir-classic/ie-class.rb', line 421

def minimize
  rautomation.minimize
end

#minimized?Boolean

Returns:

  • (Boolean)


425
426
427
# File 'lib/watir-classic/ie-class.rb', line 425

def minimized?
  rautomation.minimized?
end

#nameObject



326
327
328
# File 'lib/watir-classic/ie-class.rb', line 326

def name
  :ie
end

#rautomationObject



445
446
447
448
# File 'lib/watir-classic/ie-class.rb', line 445

def rautomation
  @rautomation ||= ::RAutomation::Window.new(:hwnd => hwnd)
  @rautomation
end

#refreshObject

Refresh the current page - the same as clicking the browsers refresh button an WIN32OLERuntimeError exception is raised if the browser cant refresh



385
386
387
388
# File 'lib/watir-classic/ie-class.rb', line 385

def refresh
  @ie.refresh2(3)
  wait
end

#restoreObject

Restore the window (after minimizing or maximizing)



430
431
432
# File 'lib/watir-classic/ie-class.rb', line 430

def restore
  rautomation.restore
end

#run_error_checksObject

this method runs the predefined error checks



566
567
568
# File 'lib/watir-classic/ie-class.rb', line 566

def run_error_checks
  @error_checkers.each { |e| e.call(self) }
end

#screenshotObject



486
487
488
# File 'lib/watir-classic/ie-class.rb', line 486

def screenshot
  Screenshot.new(hwnd)
end

#send_keys(*keys) ⇒ Object

Activates the window and sends keys to it.

Example:

browser.send_keys("Hello World{enter}")

Refer to RAutomation::Adapter::WinFfi::KeystrokeConverter.convert_special_characters for special characters conversion.

See Also:

  • RAutomation::Window#send_keys


464
465
466
# File 'lib/watir-classic/ie-class.rb', line 464

def send_keys(*keys)
  rautomation.send_keys *keys
end

#set_fast_speedObject

deprecated: use speed = :fast instead



214
215
216
# File 'lib/watir-classic/ie-class.rb', line 214

def set_fast_speed
  self.speed = :fast
end

#set_slow_speedObject

deprecated: use speed = :slow instead



219
220
221
# File 'lib/watir-classic/ie-class.rb', line 219

def set_slow_speed
  self.speed = :slow
end

#speedObject



208
209
210
211
# File 'lib/watir-classic/ie-class.rb', line 208

def speed
  return @speed if @speed == :slow
  return @type_keys ? :fast : :zippy
end

#speed=(how_fast) ⇒ Object

Specifies the speed that commands will be executed at. Choices are:

  • :slow (default)

  • :fast

  • :zippy

With IE#speed= :zippy, text fields will be entered at once, instead of character by character (default).



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/watir-classic/ie-class.rb', line 186

def speed= how_fast
  case how_fast
  when :zippy then
    @typingspeed = 0
    @pause_after_wait = 0.01
    @type_keys = false
    @speed = :fast
  when :fast then
    @typingspeed = 0
    @pause_after_wait = 0.01
    @type_keys = true
    @speed = :fast
  when :slow then
    @typingspeed = 0.08
    @pause_after_wait = 0.1
    @type_keys = true
    @speed = :slow
  else
    raise ArgumentError, "Invalid speed: #{how_fast}"
  end
end

#statusObject

Return the status of the window, typically from the status bar at the bottom.



350
351
352
353
354
# File 'lib/watir-classic/ie-class.rb', line 350

def status
  @ie.statusText
rescue WIN32OLERuntimeError
  ""
end

#titleObject

Return the title of the document



345
346
347
# File 'lib/watir-classic/ie-class.rb', line 345

def title
  @ie.document.title
end

#urlObject

returns the current url, as displayed in the address bar of the browser



482
483
484
# File 'lib/watir-classic/ie-class.rb', line 482

def url
  return @ie.LocationURL
end

#visibleObject



223
224
225
# File 'lib/watir-classic/ie-class.rb', line 223

def visible
  @ie.visible
end

#visible=(boolean) ⇒ Object



226
227
228
# File 'lib/watir-classic/ie-class.rb', line 226

def visible=(boolean)
  @ie.visible = boolean if boolean != @ie.visible
end

#wait(no_sleep = false) ⇒ Object

Block execution until the page has loaded.

Will raise Timeout::Error if page hasn’t been loaded within 5 minutes.

nodoc

Note: This code needs to be prepared for the ie object to be closed at any moment!



514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
# File 'lib/watir-classic/ie-class.rb', line 514

def wait(no_sleep=false)
  @xml_parser_doc = nil
  @down_load_time = 0.0
  interval = 0.05
  start_load_time = ::Time.now

  Timeout::timeout(5*60) do
    begin
      while @ie.busy
        sleep interval
      end

      until READYSTATES.has_value?(@ie.readyState)
        sleep interval
      end

      until @ie.document
        sleep interval
      end

      documents_to_wait_for = [@ie.document]
    rescue WIN32OLERuntimeError # IE window must have been closed
      @down_load_time = ::Time.now - start_load_time
      return @down_load_time
    end

    while doc = documents_to_wait_for.shift
      begin
        until READYSTATES.has_key?(doc.readyState.to_sym)
          sleep interval
        end
        @url_list << doc.location.href unless @url_list.include?(doc.location.href)
        doc.frames.length.times do |n|
          begin
            documents_to_wait_for << doc.frames[n.to_s].document
          rescue WIN32OLERuntimeError, NoMethodError
          end
        end
      rescue WIN32OLERuntimeError
      end
    end
  end

  @down_load_time = ::Time.now - start_load_time
  run_error_checks
  sleep @pause_after_wait unless no_sleep
  @down_load_time
end

#window(specifiers = {}, &blk) ⇒ Object



490
491
492
493
494
# File 'lib/watir-classic/ie-class.rb', line 490

def window(specifiers={}, &blk)
  win = Window.new(self, specifiers, &blk)
  win.use &blk if blk
  win
end

#windows(specifiers = {}, &blk) ⇒ Object



496
497
498
# File 'lib/watir-classic/ie-class.rb', line 496

def windows(specifiers={}, &blk)
  self.class._find_all(specifiers.keys.first, specifiers.values.first).map {|ie| Window.new(self, specifiers, IE.bind(ie), &blk)}
end