Last active
June 21, 2018 08:34
-
-
Save Joseph-N/a4345a97a1f9953d0b4fcc9786d6a70f to your computer and use it in GitHub Desktop.
How to download a file using Capybara and Poltergeist in Ruby. Tutorial Link https://goo.gl/CvzTG9
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | |
function base64encode(str) { | |
var out, i, len; | |
var c1, c2, c3; | |
len = str.length; | |
i = 0; | |
out = ""; | |
while(i < len) { | |
c1 = str.charCodeAt(i++) & 0xff; | |
if(i == len) | |
{ | |
out += base64EncodeChars.charAt(c1 >> 2); | |
out += base64EncodeChars.charAt((c1 & 0x3) << 4); | |
out += "=="; | |
break; | |
} | |
c2 = str.charCodeAt(i++); | |
if(i == len) | |
{ | |
out += base64EncodeChars.charAt(c1 >> 2); | |
out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)); | |
out += base64EncodeChars.charAt((c2 & 0xF) << 2); | |
out += "="; | |
break; | |
} | |
c3 = str.charCodeAt(i++); | |
out += base64EncodeChars.charAt(c1 >> 2); | |
out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)); | |
out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)); | |
out += base64EncodeChars.charAt(c3 & 0x3F); | |
} | |
return out; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def download_file(remote_file_url) | |
result = spider.evaluate_script(<<-JAVASCRIPT) # spider -> Capybara::Session.new(:poltergeist) | |
(function(){ | |
var file; | |
var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | |
function base64encode(str) { | |
var out, i, len; | |
var c1, c2, c3; | |
len = str.length; | |
i = 0; | |
out = ""; | |
while(i < len) { | |
c1 = str.charCodeAt(i++) & 0xff; | |
if(i == len) | |
{ | |
out += base64EncodeChars.charAt(c1 >> 2); | |
out += base64EncodeChars.charAt((c1 & 0x3) << 4); | |
out += "=="; | |
break; | |
} | |
c2 = str.charCodeAt(i++); | |
if(i == len) | |
{ | |
out += base64EncodeChars.charAt(c1 >> 2); | |
out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)); | |
out += base64EncodeChars.charAt((c2 & 0xF) << 2); | |
out += "="; | |
break; | |
} | |
c3 = str.charCodeAt(i++); | |
out += base64EncodeChars.charAt(c1 >> 2); | |
out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)); | |
out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)); | |
out += base64EncodeChars.charAt(c3 & 0x3F); | |
} | |
return out; | |
} | |
function getBinary(url){ | |
var xhr = new XMLHttpRequest(); | |
xhr.open("GET", url, false); | |
xhr.overrideMimeType("text/plain; charset=x-user-defined"); | |
xhr.send(null); | |
return xhr.responseText; | |
} | |
file = base64encode(getBinary("#{remote_file_url}")) | |
return file; | |
})(); | |
JAVASCRIPT | |
download_path = "tmp/report.pdf" # in my case I was downloading pdf files | |
File.open(path, 'wb') do |f| | |
f.write(Base64.decode64(result)) # decode the base64-encoded contents using Ruby's Base64.decode64 method | |
end | |
return download_path # you can do whatever it is really with the freshly downloaded file | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getBinary(url){ | |
var xhr = new XMLHttpRequest(); | |
xhr.open("GET", url, false); | |
xhr.overrideMimeType("text/plain; charset=x-user-defined"); | |
xhr.send(null); | |
return xhr.responseText; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sniff_file_url | |
time = 1 | |
url = nil | |
loop do | |
time+= 1 | |
print "." | |
unless spider.driver.network_traffic.last.response_parts.last.nil? | |
url = spider.driver.network_traffic.last.response_parts.last.url | |
# Find something unique to identify the url you want. In my case the download url had the text 'queue_pdf' with | |
# the ready parameter set to 1. I used a simple regex below to match that | |
break if url.match(/queue_pdf\/.*?ready=1/) || time > 30 | |
end | |
sleep time | |
end | |
url # return the url | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment