0

I have the following html code:

I saw that for Watir-webdriver the "Watir::Image.file_size" method is not currently supported. I found out that the "Watir-Classic/Image.rb" has the same method, and it seems that can be used.

# this method returns the filesize of the image, as an int
def file_size
  assert_exists
  @o.invoke("fileSize").to_i
end

I created a method that should retrieve the image size, but it seems I am not initializing the object correctly. Here is my code from the method:

img_src="/location/on_the_server/image"
chart_image = Watir::Image.new(:src, img_src)
puts chart_image.file_size

The problem is that I receive the following error:

"ArgumentError: invalid argument "/location/on_the_server/image""

I saw that for initialization the object requires (container,specifiers). I tried to change the initialization line to "chart_image = Watir::Image.new(img_src, :src)" but the error keeps appearing.

Could anyone tell me what am I doing wrong?

Is there another way to get the file size of an image from a website?

Thank you.

Cristian M
  • 357
  • 1
  • 3
  • 18

1 Answers1

2

You should not be initializing Watir::Image directly. Instead, you should use the image() method of a browser or element object.

#Assuming that browser = Watir::Browser that is open
img_src="/location/on_the_server/image"
chart_image = browser.image(:src, img_src)
puts chart_image.file_size

Update - Download Image to Determine File Size:

You could download the image using with open-uri (or similar) and then use Ruby's File class to determine the size:

require 'watir-webdriver'
require "open-uri"

#Specify where to save the image
save_file = 'C:\Users\my_user\Desktop\image.png'

#Get the src of the image you want. In this example getting the first image on Google.
browser = Watir::Browser.new
browser.goto('www.google.ca')
image_location = browser.image.src

#Save the file
File.open(save_file, 'wb') do |fo|
  fo.write open(image_location, :ssl_verify_mode => OpenSSL::SSL::VERIFY_NONE).read
end

#Output the size
puts File.size(save_file).size
Justin Ko
  • 46,526
  • 5
  • 91
  • 101
  • While this solves your initializing object issue, I think the file_size method is still unlikely to work as-is. Based on this other [question](http://stackoverflow.com/questions/1126905/how-can-you-determine-the-file-size-in-javascript), the fileSize property is only supported in IE. – Justin Ko Jun 26 '12 at 13:19
  • An alternative to get the image size is to download and checked the saved file's size - answer updated with example. – Justin Ko Jun 26 '12 at 13:34
  • Thanks Justin. your solution works like you said it would. There is a new issue though... I observed that my image don't have a usual URL (like "server.address/image.png") it has a the following format: "https://server/servlet/parallel.img.charts.JChartFactory?config=chart_N1eD7AjH00.xml&". In the browser I have the option to copy and save the image. the main issue is that I receive the next error: `OpenSSL::SSL::SSLError: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed`. – Cristian M Jun 27 '12 at 07:59
  • I have found a solution to this issue [here](http://stackoverflow.com/questions/5720484/how-to-solve-certificate-verify-failed-on-windows), but is only for ruby on rails. I am stalled here :|. – Cristian M Jun 27 '12 at 07:59
  • What version of Ruby are you using? There are different solutions depending on your version. – Justin Ko Jun 27 '12 at 15:45
  • When opening with Open-URI, you need to change the SSL verify mode to OpenSSL::SSL::VERIFY_NONE. Example updated with this. – Justin Ko Jun 27 '12 at 17:04
  • Thank you very much Justin. Now everything works perfectly. :) – Cristian M Jun 28 '12 at 09:17