Save image with watir-webdriver - ruby

How could i save image, which is loaded via watir-webdriver? All manuals and examples show only fetching src of image, and using open-uri saving it. But i need to save that image, which was generated when my page was loaded. How can i do this?
Could i use watir, and watir-webdriver at the same time? For example:
require 'watir-webdriver'
require 'watir'
#driver = Watir::Browser.new :firefox
#driver.goto (#base_url)
#img = #driver.image(id: 'CaptchaImage').save("2131.png")
How can i do something like this? Or else how to get it from cache?
Could anybody help me with it?

OpenURI will help you ..
require "watir-webdriver"
require "open-uri"
b = Watir::Browser.new :chrome
b.goto "http://stackoverflow.com/"
File.open("target_file.jpg", 'wb') do |f|
f.write open(b.img(:class, "sponsor-tag-img").src).read
end
Hope you are not doing anything bad.. :)
Please let me know if it helped.

require 'watir-webdriver'
And if you're doing it frequently, you can extend Watir's Image class with:
Watir::Image.class_eval do
def save path_to_new_file
#so that src can be opened without raising:
#Errno::ENOENT: No such file or directory # rb_sysopen
require 'open-uri'
open(path_to_new_file, 'wb') do |file|
file << open(src).read
end
end
end
so it can be used as follows:
browser = Watir::Browser.start 'google.com'
image = browser.img
image.save 'our_images/hi.png'

According to the watir-webdriver documentation, there is no Image#save method because it hasn't been implemented.
On the other hand, watir-classic does have a Image#save method. This is the example from the rdoc:
browser.image.save("c:/foo/bar.jpg")

This turned out to be a lil harder than it should be, but I needed to accomplish this since an image was only accessible when a valid session cookie is set. This is how I finally managed to accomplish this:
1. Install watir-extentions-element-screenshot
https://github.com/ansoni/watir-extensions-element-screenshot
You probably want to do gem install watir-extensions-element-screenshot.
2. Resize the browser window
This also works with headless phantomJS. After you initialize the browser, set the window size to something rather big to prevent a bug from happening when the image is larger than the browser window.
browser = Watir::Browser.new :phantomjs
browser.window.resize_to(1900, 1080)
3. Get the image element and screenshot it
In my case, the entire site is an image. Luckily, browser.html does show that the image is still encapsulated in an <img> tag, so we can access the image (in this example all images on the page) like so:
browser.elements( :tag_name => "img" ).each do |x|
x.screenshot("file_name.png")
end
This will save the image to file_name.png. It's not the exact same file, rather a screenshot of it. But as far as image download is concerned, this is a perfect solution for me and hopefully for others too!

Related

Full-page screenshot with URL in ruby ​and watir

With the code below, I get a screenshot of only one part of the screen, but I would like to take a full screenshot, which shows the URL of the page as well.
Is it possible?
AfterStep do
encoded_img = #browser.driver.screenshot_as(:base64)
embed("data:image/png;base64,#{encoded_img}",'image/png')
end
Watir doesn't have this provision to capture the screenshot along with a URL. But we can use win32ole to do this.
require 'win32/screenshot'
Win32::Screenshot::Take.of(:desktop).write(image_path)
In my case for capturing the full-screenshot with a URL, I do the following
# Code to capture the full-page screenshot with a URL
require 'watir'
require 'win32ole'
require 'win32/screenshot'
# Launch a browser and navigate to the page
browser = Watir::Browser.new :ie
browser.goto "https://www.google.com"
win_title = browser.title #Fetch the Title
# Use AutoIt to get the focus of the browser to front
WIN32OLE.new("AutoItX3.Control").ControlFocus(win_title, "", "")
# Capture the screen shot of the desktop
sleep 2 # Hold for 2s
image_path = "image_path#{rand(10000)}.png"
Win32::Screenshot::Take.of(:desktop).write(image_path)
In my case for capturing the Full-screenshot with URL, I do the following
#Code to capture the Full-Page screenshot with URL
require 'watir'
require 'win32ole'
require 'win32/screenshot'
#Launch Browser and navigate to page
browser = Watir::Browser.new :ie
browser.goto "https://www.google.com"
#Fetch the Title
win_title = browser.title
#Use AutoIt to get the focus of the browser to front
WIN32OLE.new("AutoItX3.Control").ControlFocus(win_title, "", "")
#Capture the screen shot of the desktop
sleep 2 # Hold for 2s
image_path = "image_path#{rand(10000)}.png"
Win32::Screenshot::Take.of(:desktop).write(image_path)

Watir doesn't see element with Phantomjs

I'm trying to take a screenshot of form with this code:
require 'watir'
browser = Watir::Browser.new :phantomjs
browser.driver.manage.window.maximize
browser.goto 'https://www.binbank.ru/landing/credits/'
browser.ul(class: 'r-buttons').li(text: '6').click
sleep 2
browser.screenshot.save 'a.png'
And a.png doesn't capture form. With Firefox as browser form is seen on a.png. Why is that? How can I interact with this form with PhantomJS?
PhantomJS seems to have problems with SSL on that page. You somehow need to pass --ignore-ssl-errors=true to the underlying webdriver.
Judging by How to pass browser parameter to Watir this can be done like this:
args = %w{--ignore-ssl-errors=true}
browser = Watir::Browser.new(:phantomjs, :args => args)

how do I loop a click action(refresh) until a specific string is found?

I am writing a watir script where I need to loop a click action untill a text is found. Please let me know how to do this.
thanks
You could do something like this:
require 'watir'
b = Watir::Browser.new
b.goto('http://www.example.com')
until b.text.include? "example"
b.refresh
end
But watir and watir-webdriver have methods to handle dynamic page elements. This link has examples on waiting with watir-webdriver.

Clicking group of links in watir

I am new to ruby, and I am trying to work with watir. I think I got the basics, but I am having trouble clicking all links whose id matches a regex. I tried this;
require "watir-webdriver"
browser = Watir::Browser.new :ff
browser.goto "http://mysite.com"
browser.links(:id, /asd[0-7]/).each do |adv|
adv.click
sleep 1
end
But it doesn't seem to be clicking the links. I am doing something wrong here? Links are opening in new windows, so looping through them is no problem. But I couldn't make the loop work.
This kind of investigation is better in IRB. Anyway, you should validate that you have links to click.
require "watir-webdriver"
browser = Watir::Browser.new :ff
browser.goto "https://rvm.io/"
links = browser.links(:href => /gemsets/)
links.count
I changed mine up to use a site I can access and has links.

How can I continuously open websites using Watir?

I have an array of url strings (i.e. "http://www.cnn.com") which I want to iterate through and open in Safari using watir.
urlArray.each do |url|
browser.goto(url)
end
will open the first page, but it never proceeds to the next pages in the array.
Any ideas on what's going on?
This worked for me, it opened both Google and Yahoo.
require "rubygems"
require "safariwatir"
urlArray = ["http://google.com", "http://yahoo.com"]
browser = Watir::Safari.new
urlArray.each do |url|
browser.goto url
end
When I added "http://www.cnn.com" to urlArray
urlArray = ["http://www.cnn.com", "http://google.com", "http://yahoo.com"]
it opened just cnn.com, so the problem is at that page.

Resources