0

It is image tag on the page accessed by capybara via HTTPS protocol:

<img src="path">

Is it any way to get image file from the page using capybara with any kind of driver?

I can not use something like File.read('path') because image is also accessible via HTTPS only. My latest researches brought me to such kind of solution:

  1. Visit page
  2. Save page to png (webkit driver has such useful ability)
  3. Crop image

But I do believe that pretty solution exists.

Edited 1:

I've tried out padde's solution, but here is response body:

<html><head><title>Object moved</title></head> 
    <body>
        <h2>Object moved to <a href=\"/Bledy/Blad404.aspx?aspxerrorpath=/CaptchaType.ashx\">here</a>.</h2> 
    </body>
</html>

Edited 2:

> curl -I image_path

5860cf30abf5d5480
HTTP/1.1 302 Found
Cache-Control: private
Content-Length: 168
Content-Type: text/html; charset=utf-8
Location: /Bledy/Blad404.aspx?aspxerrorpath=/CaptchaType.ashx
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Sat, 03 Nov 2012 17:18:55 GMT
FUT
  • 373
  • 2
  • 17

1 Answers1

2

What you probably want is a HTTPS request from Ruby if i get this right. Try:

require 'net/https'

url = URI.parse('path')

Net::HTTP.start(url.host, url.port, :use_ssl => true, :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|
  res = http.get(url.request_uri)
  open("image.png", "wb") do |f|
    f.write(res.body)
  end
end

For cropping, you can either use chunky_png (pure Ruby) or rmagick (requires ImageMagick)

Edit: If you want to follow redirects you can do

require 'net/https'

def process_image( content )
  # do your cropping here

  open("image.png", "wb") do |f|
    f.write(content)
  end
end

def fetch( url )
  Net::HTTP.start(url.host, url.port, :use_ssl => true, :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|
    response = http.get(url.request_uri)
    case response.code
    when Net::HTTPRedirection
      fetch response['location']
    else
      process_image response.body
    end
  end
end

fetch URI.parse('path')
Patrick Oscity
  • 53,604
  • 17
  • 144
  • 168
  • Certificate verification failed. Don't know why. As I understand when interpreter enters the block - connection to that server is established and all stored session cookies will be sent when http.get is executed. Anyway you pointed the right direction of search for me. Thanks! – FUT Nov 03 '12 at 16:39
  • 1
    I guess you are using Ruby 1.9 where certificate verification is stricter than in previous versions. You can work around this problem by using `:verify_mode => OpenSSL::SSL::VERIFY_NONE`. I have updated my answer, too. – Patrick Oscity Nov 03 '12 at 16:49
  • Yes, you are right! But response is: `Object moved

    Object moved to here.

    ` It is simmilar to my previous wget and curl results.
    – FUT Nov 03 '12 at 17:04
  • Well that's an issue with the web service you are calling. Does the same path work when you are entering it into the browser? Maybe an example url would be helpful. – Patrick Oscity Nov 03 '12 at 17:06
  • Yes, it works just great. I think I'm missing something valuable here. – FUT Nov 03 '12 at 17:07
  • 1
    That probably means, your browser is redirecting you. You could try something like http://stackoverflow.com/questions/6934185/ruby-net-http-following-redirects – Patrick Oscity Nov 03 '12 at 17:09
  • 1
    Please type `curl -I ` in the command line and show me the results so i can help you. – Patrick Oscity Nov 03 '12 at 17:12
  • I'll check the link and amend/append current comment. Thank you! – FUT Nov 03 '12 at 17:13
  • Of course I will but currently we are on the road to the answer :) I have updated the question. Check it please. – FUT Nov 03 '12 at 17:22
  • Check my edit. I'll leave the rest up to you, because this is turning into one of these "please do my work for me" questions. – Patrick Oscity Nov 03 '12 at 18:06