How can i save website images with watir, without reloading them with open-uri or similar?
I: The reason why i can't use
File.open(file_name, 'wb') do |f|
f.write open(img.src).read
end # file open
is that the images are generated in the current (login-)session and only once, so an "external" 2nd access isn't possible.
II:
browser.images.save()
- only for ie - isn't helpful either, it opens the save-to dialogue. So its so useless for automation.
Examples: http://wiki.openqa.org/display/WTR/Save+All+Images+on+a+Webpage
require 'watir'
browser = Watir::Browser.new :ie
browser.goto 'http://google.com'
idx = 0
browser.images.each do |x|
puts idx
idx += 1
location = 'c:\tmp\file-' + idx.to_s + '.jpg'
x.save(location)
end
github source: http://rubydoc.info/github/watir/watir-classic/Watir/Image
# File 'lib/watir-classic/image.rb', line 48
def save(path)
@container.goto(src)
begin
fill_save_image_dialog(path)
@container.document.execCommand("SaveAs")
ensure
@container.back
end
end
My best idea atm is to fetch all images by using a proxy. But maybe there is a "watir-way".
Environment:
# ruby 1.9.3p125 (2012-02-16) [i386-mingw32]
# watir (4.0.2 x86-mingw32)
# watir-classic (3.6.0, 3.5.0, 3.4.0)
# watir-webdriver (0.6.4, 0.6.2)
Edit: i am aware that there are different ways to get images from the website, and without event thinking i could build a list with so much solutions, but its about to solve the problem with watir.
browser.image.save(file)
useless? The method opens the save dialogue, inputs the required fields and save the file (ie the dialog is also automated). Or do you mean it is not helpful because it is just for IE and you need to use a different browser? – Frankly