Class: Vapir::IE

Inherits:
Browser
  • Object
show all
Includes:
Exception, PageContainer
Defined in:
lib/vapir-ie/process.rb,
lib/vapir-ie/version.rb,
lib/vapir-ie/ie-class.rb,
lib/vapir-ie/close_all.rb,
lib/vapir-ie/ie-process.rb

Defined Under Namespace

Modules: Container, PageContainer, RefreshConstants Classes: Area, Button, CheckBox, Dd, Div, Dl, Dt, Em, FileField, H1, H2, H3, H4, H5, H6, Hidden, InputElement, Label, Li, Map, ModalDialog, ModalDialogDocument, Ol, Option, P, Pre, Process, Radio, SelectList, Span, Strong, TextField, Ul

Constant Summary collapse

VERSION =
'1.7.0'
EMPTY_TAG_NAME =

IE inserts some element whose tagName is empty and just acts as block level element Probably some IE method of cleaning things To pass the same to the xml parser we need to give some name to empty tagName

"DUMMY"
ExistenceFailureCodesRE =

we expect one of these error codes when quitting or checking existence.

Regexp.new(
{ '0x800706ba' => 'The RPC server is unavailable',
  '0x80010108' => 'The object invoked has disconnected from its clients.',
  '0x800706be' => 'The remote procedure call failed.',
  '0x800706b5' => 'The interface is unknown.',
}.keys.join('|'), Regexp::IGNORECASE)
@@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

Constants included from PageContainer

PageContainer::READYSTATE_COMPLETE

Instance Attribute Summary collapse

Attributes included from Container

#type_keys, #typingspeed

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PageContainer

#check_for_http_error, #containing_object, #content_window_object, #document_element, #execute_script, #html, #page_container, #text, #wait

Methods included from Container

#set_container

Constructor Details

#initialize(suppress_new_window = nil) ⇒ IE

Create an IE browser.



84
85
86
# File 'lib/vapir-ie/ie-class.rb', line 84

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



65
66
67
# File 'lib/vapir-ie/ie-class.rb', line 65

def down_load_time
  @down_load_time
end

#hwndObject

Return the current window handle



311
312
313
314
# File 'lib/vapir-ie/ie-class.rb', line 311

def hwnd
  assert_exists
  @hwnd ||= @ie.hwnd
end

#ieObject

the OLE Internet Explorer object



68
69
70
# File 'lib/vapir-ie/ie-class.rb', line 68

def ie
  @ie
end

#loggerObject

access to the logger object



71
72
73
# File 'lib/vapir-ie/ie-class.rb', line 71

def logger
  @logger
end

#url_listObject (readonly)

this contains the list of unique urls that have been visited



74
75
76
# File 'lib/vapir-ie/ie-class.rb', line 74

def url_list
  @url_list
end

Class Method Details

._find(how, what) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/vapir-ie/ie-class.rb', line 261

def self._find(how, what)
  ieTemp = nil
  IE.each do |ie|
    window = ie.ie
    
    case how
    when :url
      ieTemp = window if Vapir::fuzzy_match(window.locationURL, what)
    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
      ieTemp = window if Vapir::fuzzy_match(title, what)
    when :hwnd
      begin
        ieTemp = window if what == window.HWND
      rescue WIN32OLERuntimeError
      end
    else
      raise ArgumentError
    end
  end
  return ieTemp
end

.attach(how, what) ⇒ Object

Return a Vapir::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 Vapir/Ruby is run under a service (instead of a user).



142
143
144
145
146
# File 'lib/vapir-ie/ie-class.rb', line 142

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

.attach_timeoutObject

default value



16
17
18
# File 'lib/vapir-ie/ie-class.rb', line 16

def self.attach_timeout
  @@attach_timeout
end

.attach_timeout=(timeout) ⇒ Object



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

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

.bind(window) ⇒ Object

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



157
158
159
160
161
162
# File 'lib/vapir-ie/ie-class.rb', line 157

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/vapir-ie/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 Vapir/Ruby is run under a service (instead of a user). Yields to the window and its hwnd.



237
238
239
240
241
242
243
244
245
246
# File 'lib/vapir-ie/ie-class.rb', line 237

def self.each
  shell = WIN32OLE.new('Shell.Application')
  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
    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 Vapir/Ruby is run under a service (instead of a user).



256
257
258
259
# File 'lib/vapir-ie/ie-class.rb', line 256

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 Vapir/Ruby is run under a service (instead of a user).



111
112
113
114
115
# File 'lib/vapir-ie/ie-class.rb', line 111

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.



77
78
79
80
81
# File 'lib/vapir-ie/ie-class.rb', line 77

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]



25
26
27
# File 'lib/vapir-ie/ie-class.rb', line 25

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

.process_countObject

Returns the number of IEXPLORE processes currently running.



18
19
20
# File 'lib/vapir-ie/process.rb', line 18

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

.set_options(options) ⇒ Object

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



29
30
31
32
33
# File 'lib/vapir-ie/ie-class.rb', line 29

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

.speedObject



40
41
42
43
# File 'lib/vapir-ie/ie-class.rb', line 40

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

.speed=(x) ⇒ Object



44
45
46
47
# File 'lib/vapir-ie/ie-class.rb', line 44

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.



96
97
98
# File 'lib/vapir-ie/ie-class.rb', line 96

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.



127
128
129
130
131
# File 'lib/vapir-ie/ie-class.rb', line 127

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.



102
103
104
105
106
# File 'lib/vapir-ie/ie-class.rb', line 102

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

.versionObject

the version string, from the registry key HKEY_LOCAL_MACHINESOFTWAREMicrosoftInternet ExplorerVersion



7
8
9
10
11
12
13
14
15
# File 'lib/vapir-ie/ie-process.rb', line 7

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

the version string divided into its numeric parts returned as an array of integers



17
18
19
# File 'lib/vapir-ie/ie-process.rb', line 17

def self.version_parts
  @@ie_version_parts ||= IE.version.split('.').map{|part| part.to_i }
end

.visibleObject



49
50
51
52
# File 'lib/vapir-ie/ie-class.rb', line 49

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

.visible=(x) ⇒ Object



53
54
55
56
# File 'lib/vapir-ie/ie-class.rb', line 53

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

Instance Method Details

#_attach_init(how, what) ⇒ Object

this method is used internally to attach to an existing window



149
150
151
152
153
# File 'lib/vapir-ie/ie-class.rb', line 149

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

#_new_process_initObject



117
118
119
120
121
122
123
# File 'lib/vapir-ie/ie-class.rb', line 117

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

#_new_window_initObject



88
89
90
91
92
# File 'lib/vapir-ie/ie-class.rb', line 88

def _new_window_init
  create_browser_window
  initialize_options
  goto 'about:blank' # this avoids numerous problems caused by lack of a document
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



538
539
540
# File 'lib/vapir-ie/ie-class.rb', line 538

def add_checker(checker)
  @error_checkers << checker
end

#attach_commandObject



868
869
870
# File 'lib/vapir-ie/ie-class.rb', line 868

def attach_command
  "Vapir::IE.attach(:hwnd, #{hwnd})"
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



393
394
395
396
397
398
# File 'lib/vapir-ie/ie-class.rb', line 393

def back
  assert_exists do
    @ie.GoBack
    wait
  end
end

#bring_to_frontObject

Make the window come to the front



467
468
469
# File 'lib/vapir-ie/ie-class.rb', line 467

def bring_to_front
  win_window.really_set_foreground!
end

#browserObject



517
518
519
# File 'lib/vapir-ie/ie-class.rb', line 517

def browser
  self
end

#browser_objectObject



305
306
307
308
# File 'lib/vapir-ie/ie-class.rb', line 305

def browser_object
  assert_exists
  @ie
end

#clear_url_listObject

clear the list of urls that we have visited



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

def clear_url_list
  @url_list.clear
end

#closeObject

Closes the Browser



430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/vapir-ie/ie-class.rb', line 430

def close
  assert_exists
  @ie.stop
  @ie.quit
  # TODO/fix timeout; this shouldn't be a hard-coded magic number. 
  ::Waiter.try_for(32, :exception => WindowFailedToCloseException.new("The browser window did not close"), :interval => 1) do
    begin
      if exists?
        @ie.quit
        false
      else
        true
      end
    rescue WIN32OLERuntimeError
      raise unless $!.message =~ ExistenceFailureCodesRE
      true
    end
  end
  @ie=nil
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
30
# File 'lib/vapir-ie/close_all.rb', line 25

def close_modal
  modal_dialog=modal_dialog(:timeout => 0, :error => false)
  if modal_dialog && modal_dialog.exists?
    modal_dialog.close
  end
end

#close_othersObject

find other ie browser windows and close them



10
11
12
# File 'lib/vapir-ie/close_all.rb', line 10

def close_others
  IE.close_all_but self
end

#dirObject



502
503
504
# File 'lib/vapir-ie/ie-class.rb', line 502

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



544
545
546
# File 'lib/vapir-ie/ie-class.rb', line 544

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

#documentObject Also known as: document_object

Return the current document



511
512
513
514
# File 'lib/vapir-ie/ie-class.rb', line 511

def document
  assert_exists
  return @ie.document
end

#element_object_by_xpath(xpath) ⇒ Object

return the first element object (not Element) that matches the xpath



761
762
763
764
# File 'lib/vapir-ie/ie-class.rb', line 761

def element_object_by_xpath(xpath)
  objects= element_objects_by_xpath(xpath)
  return (objects && objects[0])
end

#element_objects_by_xpath(xpath) ⇒ Object

execute xpath and return an array of elements



767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
# File 'lib/vapir-ie/ie-class.rb', line 767

def element_objects_by_xpath(xpath)
  doc = xmlparser_document_object 
  modifiedXpath = ""
  selectedElements = Array.new
  
  # strip any trailing slash from the xpath expression (as used in watir unit tests)
  xpath.chop! unless (/\/$/ =~ xpath).nil?
  
  doc.xpath(xpath).each do |element|
    modifiedXpath = element.path
    temp = element_by_absolute_xpath(modifiedXpath) # temp = a DOM/COM element
    selectedElements << temp if temp != nil
  end
  #puts selectedElements.length
  if selectedElements.length == 0
    return nil
  else
    return selectedElements
  end
end

#exists?Boolean Also known as: exist?

Are we attached to an open browser?

Returns:

  • (Boolean)


344
345
346
347
348
349
350
351
# File 'lib/vapir-ie/ie-class.rb', line 344

def exists?
  !!(@ie && begin
    @ie.name
  rescue WIN32OLERuntimeError
    raise unless $!.message =~ ExistenceFailureCodesRE
    false
  end)
end

#focusObject

Gives focus to the frame



558
559
560
561
# File 'lib/vapir-ie/ie-class.rb', line 558

def focus
  document.activeElement.blur
  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



402
403
404
405
406
407
# File 'lib/vapir-ie/ie-class.rb', line 402

def forward
  assert_exists do
    @ie.GoForward
    wait
  end
end

#front?Boolean

Returns:

  • (Boolean)


471
472
473
# File 'lib/vapir-ie/ie-class.rb', line 471

def front?
  win_window.foreground?
end

#goto(url) ⇒ Object

Navigate to the specified URL.

* url - string - the URL to navigate to


383
384
385
386
387
388
389
# File 'lib/vapir-ie/ie-class.rb', line 383

def goto(url)
  assert_exists do
    @ie.navigate(url)
    wait
    return @down_load_time
  end
end

#initialize_optionsObject



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

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

  @element_object = nil
  @page_container = self
  @error_checkers = []
  
  @logger = DefaultLogger.new
  @url_list = []
end

#log(what) ⇒ Object



359
360
361
# File 'lib/vapir-ie/ie-class.rb', line 359

def log(what)
  @logger.debug(what) if @logger
end

#maximizeObject

Maximize the window (expands to fill the screen)



452
453
454
# File 'lib/vapir-ie/ie-class.rb', line 452

def maximize
  win_window.maximize!
end

#minimizeObject

Minimize the window (appears as icon on taskbar)



457
458
459
# File 'lib/vapir-ie/ie-class.rb', line 457

def minimize
  win_window.minimize!
end


321
322
323
324
325
326
327
# File 'lib/vapir-ie/ie-class.rb', line 321

def modal_dialog(options={})
  assert_exists do
    raise ArgumentError, "options argument must be a hash; received #{options.inspect} (#{options.class})" unless options.is_a?(Hash)
    modal=IE::ModalDialog.new(self, options.merge(:error => false))
    modal.exists? ? modal : nil
  end
end


329
330
331
332
333
# File 'lib/vapir-ie/ie-class.rb', line 329

def modal_dialog!(options={})
  assert_exists do
    IE::ModalDialog.new(self, options.merge(:error => true))
  end
end

#refreshObject

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



417
418
419
420
421
422
# File 'lib/vapir-ie/ie-class.rb', line 417

def refresh
  assert_exists do
    @ie.refresh2(RefreshConstants::REFRESH_COMPLETELY)
    wait
  end
end

#restoreObject

Restore the window (after minimizing or maximizing)



462
463
464
# File 'lib/vapir-ie/ie-class.rb', line 462

def restore
  win_window.restore!
end

#run_error_checksObject

this method runs the predefined error checks



530
531
532
533
534
# File 'lib/vapir-ie/ie-class.rb', line 530

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

#screen_capture(filename, dc = :window) ⇒ Object

saves a screenshot of this browser window to the given filename.

second argument, optional, specifies what area to take a screenshot of.

  • :client takes a screenshot of the client area, which excludes the menu bar and other window trimmings.

  • :window (default) takes a screenshot of the full browser window

  • :desktop takes a screenshot of the full desktop



492
493
494
495
496
497
498
499
500
# File 'lib/vapir-ie/ie-class.rb', line 492

def screen_capture(filename, dc=:window)
  if dc==:desktop
    screenshot_win=WinWindow.desktop_window
    dc=:window
  else
    screenshot_win=win_window
  end
  screenshot_win.capture_to_bmp_file(filename, :dc => dc)
end

#send_keys(key_string) ⇒ Object

Send key events to IE window. See www.autoitscript.com/autoit3/docs/appendix/SendKeys.htm for complete documentation on keys supported and syntax.



478
479
480
481
482
483
484
# File 'lib/vapir-ie/ie-class.rb', line 478

def send_keys(key_string)
  assert_exists do
    require 'vapir-ie/autoit'
    bring_to_front
    Vapir.autoit.Send key_string
  end
end

#set_fast_speedObject

deprecated: use speed = :fast instead



215
216
217
# File 'lib/vapir-ie/ie-class.rb', line 215

def set_fast_speed
  self.speed = :fast
end

#set_logger(logger) ⇒ Object

deprecated: use logger= instead



355
356
357
# File 'lib/vapir-ie/ie-class.rb', line 355

def set_logger(logger)
  @logger = logger
end

#set_slow_speedObject

deprecated: use speed = :slow instead



220
221
222
# File 'lib/vapir-ie/ie-class.rb', line 220

def set_slow_speed
  self.speed = :slow
end

#show_activeObject

this method shows the name, id etc of the object that is currently active - ie the element that has focus its mostly used in irb when creating a script



550
551
552
553
554
555
# File 'lib/vapir-ie/ie-class.rb', line 550

def show_active # TODO/fix: move to common; test 
  
  current_object = document.activeElement
  current_element = base_class.factory(current_object)
  current_element.to_s
end

#speedObject



209
210
211
212
# File 'lib/vapir-ie/ie-class.rb', line 209

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



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

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.



373
374
375
# File 'lib/vapir-ie/ie-class.rb', line 373

def status
  return @ie.statusText
end

#titleObject

Return the title of the document



368
369
370
# File 'lib/vapir-ie/ie-class.rb', line 368

def title
  @ie.document.title
end

#urlObject

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



522
523
524
525
# File 'lib/vapir-ie/ie-class.rb', line 522

def url
  assert_exists
  return @ie.LocationURL
end

#visibleObject



224
225
226
227
# File 'lib/vapir-ie/ie-class.rb', line 224

def visible
  assert_exists
  @ie.visible
end

#visible=(boolean) ⇒ Object



228
229
230
231
# File 'lib/vapir-ie/ie-class.rb', line 228

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

#win_windowObject



317
318
319
# File 'lib/vapir-ie/ie-class.rb', line 317

def win_window
  @win_window||= WinWindow.new(hwnd)
end

#xmlparser_document_objectObject

Functions written for using xpath for getting the elements.



565
566
567
568
569
570
# File 'lib/vapir-ie/ie-class.rb', line 565

def xmlparser_document_object
  if @xml_parser_doc == nil
    create_xml_parser_doc
  end
  return @xml_parser_doc
end