Open sandeepnagra opened 6 years ago
How is it working in IE 11? Mine don't work at all, I didin't understand the "self.base64" command part, can you help me? Thanks!
@BrunoMarc - sorry for the late response. Here's the script I used for testing:
# #!/usr/bin/env ruby
require "watir"
require "base64"
require 'time'
MAX_SCREENSHOT_WAIT_TIME = 120
module Watir
class Screenshot
def base64_canvas(browser)
@browser = browser
output = nil
return self.base64 if @browser&.name == :firefox || @browser&.name == :internet_explorer # reported bug - https://github.com/niklasvh/html2canvas/issues/1476
@browser.execute_script html2canvas_payload
sleep 3
@browser.execute_script h2c_activator
@browser.wait_until(timeout: MAX_SCREENSHOT_WAIT_TIME) {
output = @browser.execute_script "return window.canvasImgContentDecoded;"
}
raise "Could not generate screenshot blob within #{MAX_SCREENSHOT_WAIT_TIME} seconds" unless output
return output.sub!(/^data\:image\/png\;base64,/, '')
end
private
def h2c_activator
%<
function genScreenshot () {
var canvasImgContentDecoded;
html2canvas(document.body).then(function (canvas) {
window.canvasImgContentDecoded = canvas.toDataURL("image/png");
});
};
genScreenshot();
>.gsub(/\s+/, ' ').strip
end
def html2canvas_payload
path = File.join(Dir.pwd, "vendor/html2canvas.js")
File.read(path)
end
end
end
b = Watir::Browser.new :firefox
b.goto "https://www.massmutual.com/"
img = b.screenshot.base64_canvas(b)
png = Base64.decode64(img)
File.open('full-page-screenshot_with_canvas_redraw.png', 'wb') { |f| f.write(png) }
b.quit
For now I detect what browser I am executing the script for and if it is Firefox or IE, I just return self.base64.
With self.base64 Firefox returns image for just current viewport but IE weirdly returns for the whole webpage (blessing in disguise).
Let me know if this doesn't help.
~Sandeep
Here's my javascript (basically I am trying to get a screenshot of full webpage and not just the page visible in viewport):
It works fine on Chrome 64+, Safari 11, Edge 16 but fails on Firefox with following error:
I get the following error for IE11:
Let me know if more information is required from my end.
Update: On IE11, I am getting full screenshot with just
self.base64
command, so I am good with that. Only issue is Firefox.