Phlip,
You write that for your third option (using getHTML) that you wont get the
Internet Explorer's DOM in its exact visual state.
It is actually possible to get some of what you are asking for. Below is my
hack to get HTML out that reflects changes to the DOM.
It doesnt give you the keyboard focus, but if you want, it should be possible
to highlight the element with focus before getting the HTML.
The test code at the end will attach to the first IE it sees, if any, or go to
instant.search.yahoo.com and trigger an AJAX driven modification to the DOM.
The code has some work-arounds. If anyone has constructive comments they will
be much appreciated :-)
- X - - - - - -
# page_capture
require 'watir'
$KCODE = 'UTF8'
require 'jcode'
class Watir::IE
def page_capture(basename="watir_pagecapture_")
# ensure unique filename
# TO DO: make better file name/factor out the file-name code...
no = 0
file_name = "#{basename}_#{sprintf("%03d",no)}.html" # ex.:
watir_pagecapture_001.html
loop do
break unless File.exists?(file_name)
no = no + 1
file_name = "#{basename}_#{sprintf("%03d",no)}.html"
end
# write HTML to file
begin
f = File.new(file_name,"w")
html = ie.document.body.parentelement.outerhtml
# Work-around #1:
# The <!doctype ...> could not be obtained, so for now, using string as
found on http://answers.yahoo.com
f.puts '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"><html>'
# Work-around #2:
# some pages needs to have BASE set do display correctly (e.g.
http://demo.script.aculo.us/shop), so add it..
# TO DO - clean this up - handle the case of no HEAD tag:
html.sub!(/<HEAD>/i,"<HEAD>\n<BASE HREF=\"#{url}\">\n")
# Work-around #3:
# Document does not display correctly with this line in it (e.g. try
http://es.answers.yahoo.com/) - don't know why ??? -
# so comment it out:
html.sub!('<META http-equiv=content-type content="text/html;
charset=UTF-8">',
'<!-- <META http-equiv=content-type content="text/html;
charset=UTF-8"> -->')
# now, write out the modified HTML
f.puts html
ensure
f.close if f
end
return nil unless File.exists?("#{file_name}")
return "Captured HTML in file: #{file_name} for URL: #{url}"
end # def page_capture
end # class
# ############### ############### ############### ##############
# Test, if run as file:
# ############### ############### ############### ##############
if (__FILE__ == $0)
begin
ie = Watir::IE.attach(:title,//)
rescue Watir::Exception::NoMatchingWindowFoundException
# following code is from Bret Pettichord's post
# http://www.mail-archive.com/[email protected]/msg01365.html
# It is included as it gives an example of the captured page being
different from the static HTML downloaded and seen by "view source".
ie = Watir::IE.start("http://instant.search.yahoo.com")
ie.text_field(:name, 'p').set('ruby')
while ! ie.link(:id, 'yschakis').exists? do sleep 0.1; end
puts ie.link(:id, 'yschakis').text
end
puts "- "
puts ie.page_capture("pagecapture_test")
puts "-"
end # test of file
- X - - - - - -
~~ Egil
---------------------------------------------------------------------
Posted via Jive Forums
http://forums.openqa.org/thread.jspa?threadID=4907&messageID=14044#14044
_______________________________________________
Wtr-general mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/wtr-general