Module: Vapir::IE::PageContainer

Includes:
Exception, Container, PageContainer
Included in:
Vapir::IE, ModalDialogDocument
Defined in:
lib/vapir-ie/page_container.rb

Overview

A PageContainer contains an HTML Document. In other words, it is a what JavaScript calls a Window.

this assumes that document_object is defined on the includer.

Defined Under Namespace

Modules: WebBrowserReadyState

Constant Summary collapse

READYSTATE_COMPLETE =
WebBrowserReadyState::Complete

Instance Method Summary collapse

Methods included from Container

#handling_existence_failure, #log

Instance Method Details

#check_for_http_errorObject

This method checks the currently displayed page for http errors, 404, 500 etc It gets called internally by the wait method, so a user does not need to call it explicitly



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/vapir-ie/page_container.rb', line 29

def check_for_http_error
  # check for IE7
  n = self.document.invoke('parentWindow').navigator.appVersion
  m=/MSIE\s(.*?);/.match( n )
  if m and m[1] =='7.0'
    if m = /HTTP (\d\d\d.*)/.match( self.title )
      raise NavigationException, m[1]
    end
  else
    # assume its IE6
    url = self.document.location.href
    if /shdoclc.dll/.match(url)
      m = /id=IEText.*?>(.*?)</i.match(self.html)
      raise NavigationException, m[1] if m
    end
  end
  false
end

#closeObject



62
63
64
# File 'lib/vapir-ie/page_container.rb', line 62

def close
  content_window_object.close
end

#content_window_objectObject



48
49
50
# File 'lib/vapir-ie/page_container.rb', line 48

def content_window_object
  document_object.parentWindow
end

#execute_script(source) ⇒ Object

Execute the given JavaScript string



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/vapir-ie/page_container.rb', line 67

def execute_script(source)
  retried=false
  result=nil
  begin
    result=document_object.parentWindow.eval(source)
  rescue WIN32OLERuntimeError, NoMethodError
    # don't retry more than once; don't catch anything but the particular thing we're looking for 
    if retried || $!.message !~ /unknown property or method:? `eval'/
      raise
    end
    # this can happen if no scripts have executed at all - the 'eval' function doesn't exist. 
    # execScript works, but behaves differently than eval (it doesn't return anything) - but 
    # once an execScript has run, eval is subsequently defined. so, execScript a blank script, 
    # and then try again with eval.
    document_object.parentWindow.execScript('null')
    retried=true
    retry
  end
  return result
end

#htmlObject

The HTML of the current page



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

def html
  document_element.outerHTML
end

#textObject

The text of the current page



58
59
60
# File 'lib/vapir-ie/page_container.rb', line 58

def text
  document_element.innerText
end

#wait(options = {}) ⇒ Object Also known as: page_container_wait

Block execution until the page has loaded.

nodoc

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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/vapir-ie/page_container.rb', line 92

def wait(options={})
  return unless config.wait
  unless options.is_a?(Hash)
    raise ArgumentError, "given options should be a Hash, not #{options.inspect} (#{options.class})\nold conflicting arguments of no_sleep or last_url are gone"
  end
  options={:sleep => false, :interval => 0.1, :timeout => config.wait_timeout}.merge(options)
  @xml_parser_doc = nil
  @down_load_time = nil
  start_load_time = Time.now
  
  if respond_to?(:browser_object)
    ::Waiter.try_for(options[:timeout]-(Time.now-start_load_time), :interval => options[:interval], :exception => "The browser was still busy after #{options[:timeout]} seconds") do
      return unless exists?
      handling_existence_failure(:handle => proc { false }) { !browser_object.busy }
    end
    ::Waiter.try_for(options[:timeout]-(Time.now-start_load_time), :interval => options[:interval], :exception => "The browser's readyState was still not ready for interaction after #{options[:timeout]} seconds") do
      return unless exists?
      handling_existence_failure(:handle => proc { false }) do
        [WebBrowserReadyState::Interactive, WebBrowserReadyState::Complete].include?(browser_object.readyState)
      end
    end
  end
  # if the document object is gone, then we want to just return. 
  # in subsequent code where we want the document object, we will call this proc 
  # so that we don't have to deal with checking for error / returning every time. 
  doc_or_ret = proc do
    begin
      document_object
    rescue WIN32OLERuntimeError, NoMethodError
      return
    end
  end
  ::Waiter.try_for(options[:timeout]-(Time.now-start_load_time), :interval => options[:interval], :exception => "The browser's document was still not defined after #{options[:timeout]} seconds") do
    return unless exists?
    doc_or_ret.call
  end
  urls=[]
  all_frames_complete_result=::Waiter.try_for(options[:timeout]-(Time.now-start_load_time), :interval => options[:interval], :exception => nil, :condition => proc{|result| result==true }) do
    return unless exists?
    all_frames_complete?(doc_or_ret.call, urls)
  end
  case all_frames_complete_result
  when false
    raise "A frame on the browser did not come into readyState complete after #{options[:timeout]} seconds"
  when ::Exception
    message = "A frame on the browser encountered an error.\n"
    if all_frames_complete_result.message =~ /0x80070005/
      message += "An 'Access is denied' error might be fixed by adding the domain of the site to your 'Trusted Sites'.\n"
    end
    message+="Original message was:\n\n"
    message+=all_frames_complete_result.message
    raise all_frames_complete_result.class, message, all_frames_complete_result.backtrace
  when true
    # dandy; carry on. 
  else
    # this should never happen. 
    raise "Unexpected result from all_frames_complete?: #{all_frames_complete_result.inspect}"
  end
  @url_list=(@url_list || [])+urls
  
  @down_load_time= Time.now - start_load_time
  run_error_checks if respond_to?(:run_error_checks)
  sleep @pause_after_wait if options[:sleep]
  @down_load_time
end