Programmatically take ScreenShot of Desktop in Ruby? - ruby

I asked this question about taking a picture of a webpage programmatically, and I've downloaded and got webkit2png working (taking pictures of HTML pages like blogs and whatnot). So cool, thanks for showing me that!
Now I would like to start doing more, like being able to take pictures of Flash websites after they have loaded, and of my desktop.
Is it possible to take pictures of Flash websites with webkit2png (considering you might have to wait a few seconds for it to load)?
But the main question is, how do I programmatically take pictures of the desktop? That would allow me to have much more control over what's going on.

You can use xwd(1) to take a screenshot of the root window:
xwd -display :0 -root|xwdtopnm |pnmtopng > $1

This is an awesome question!
A couple of years ago I has to work on a similar project. I found a library, called watir, you can use to control system browsers from Ruby.
At the time I checked, it wasn't really reliable in a Linux environment, but right now it seems to be pretty solid.
Here's a couple of links:
http://90kts.com/blog/2008/capturing-screenshots-in-watir/
http://www.marekj.com/wp/2008/04/desktop-screenshots-with-watir-win32screenshot-and-rmagick/
http://clearspace.openqa.org/thread/13949
http://wiki.openqa.org/display/WTR/FAQ#FAQ-HowdoItakescreenshotsandappendtoaWordfile%3F
I have never tried this solution so I would be really happy if you can write here a feedback if you decide to go with Watir. All the examples targets a Windows server, I didn't found a valid tutorial using a Linux + Firefox environment.

You can use Watir WebDriver like this:
$ irb
irb(main):001:0> require 'watir-webdriver'
=> true
irb(main):002:0> browser = Watir::Browser.new
=> #<Watir::Browser:0x136da92fad77d562 url="about:blank" title="">
irb(main):003:0> browser.goto 'http://stackoverflow.com/questions/1733715/programmatically-take-screenshot-of-desktop-in-ruby'
=> "http://stackoverflow.com/questions/1733715/programmatically-take-screenshot-of-desktop-in-ruby"
irb(main):004:0> browser.screenshot.save 'screenshot.png'
=> #<File:screenshot.png (closed)>
irb(main):005:0>
which seems to do the job rather well! I tested this on my Mac, but I'd be surprised if it didn't work well on Linux too.

Related

Can anyone suggest how to take screenshot of full webpage using ruby selenium?

I want to capture full webpage screenshot in chrome browser using ruby selenium. I am using Rspec testing framework. save_screenshot method captures screenshot only for visible area.
I have gone through the following link,
How to take a screenshot of a full browser page and its elements using selenium-webdriver/capybara in Ruby?
But I don't want to use window resizing or watir gem. Is there any other way or gem to achieve same.
1) You can use https://github.com/samnissen/watir-screenshot-stitch where
Directly employing geckodriver's new full page screenshot functionality (only on Firefox).
Screenshot stitching, paging down a given URL by the size of the viewport, capturing screenshots and adjoining them.
Employing a bundled html2canvas script against the page to generate a png from a canvas element.
2) or use native instrument for your OS - https://paulhammond.org/webkit2png
In code will look like this
webkit2png https://stackoverflow.com/questions/60728482/can-anyone-suggest-how-to-take-screenshot-of-full-webpage-using-ruby-selenium
where:
- main command - webkit2png
- link page - all else
It's old question, but it doesn't hurt to post this. Use Scrot to take screenshot, and later make gif. Since OP only want screenshots, command would be:
require 'screenscrot'
#screen = ScreenScrot.new
#screen.capture(:all)
On linux:
sudo apt install scrot -y && gem install screenscrot

Firefox "Mouseover" issues in Selenium Ruby bindings on Windows

I've been struggling with mouse hover for a couple of days and found a couple of threads here on the topic, but none have helped. I've tried dozens of different approaches and have also modified my code to be more in sync with the examples here, especially Dave Haeffner's suggestions. The current code looks like:
Selenium::WebDriver::Wait.new(timeout: 2).until do
#driver.find_element(link: "ADMIN").displayed?
end
#driver.action.move_to(#driver.find_element(link: "ADMIN")).perform
Selenium::WebDriver::Wait.new(timeout: 2).until do
#driver.find_element(link: "ORGANIZATION").displayed?
end
driver.action.move_to(#driver.find_element(link: "ORGANIZATION")).perform
Selenium::WebDriver::Wait.new(timeout: 2).until do
#driver.find_element(link: "TEAMS").displayed?
end
#driver.find_element(link: "TEAMS").click
end
On macs, this code works fine. On Windows however, it produces:
Failure/Error: #driver.action.move_to(#driver.find_element(link: "ADMIN")).perform
Selenium::WebDriver::Error::InvalidElementStateError:
Cannot perform native interaction: Could not load native events component.
I'm sure the element access is fine, because if I change the first mouse hover to a click action, it works great.
Any help would be appreciated.
You need to use Firefox version 31.0.6 . Versions of Firefox after that don't have the native events support. If you need to use a later version of Firefox, then just make sure your test actions are all non-native, such as using a JavascriptExecutor to create a hover (instead of relying on native events in the firefox driver).

Headless Browser in ruby, non testing purpose

What are the options for using Headless browser in ruby? Already Tried Watir, but it opens a browser window for every page opened, that is un desired side effect. It would be awesome if there was something like Phanthom JS or if i could Phanthom JS in ruby code.
First, Watir does not "open a browser window for every page" as you describe, unless you are doing something wrong.
Second, you can use phantomjs with Watir. Uninstall the Headless gem, and just do
broswer = Watir::Browser.new :phantomjs
browser.goto "some_web_page.com"
browser.close # When you are done.
I suspect you are opening a new browser instance for every page, rather than just using goto. If that's the case, try using .new() or .start() just once, then .goto() after that. That is, just replace :phantomjs in the code above with :ff for example.
Keep in mind that if you use "headless", last I heard it did not yet work on OS X.

Cucumber embed for screenshots not linking to screenshot

Cross-posted from the Cukes Google Group:
I have experimented with a number of methods of saving screenshots,
but settled on the method that is built into watir-webdriver. No
matter which method I have used, I am not able to successfully embed a
link to this image in the Cucumber HTML report.
In c:\ruby\cucumber\project_name\features\support\hooks.rb, I'm using:
After do |scenario|
if scenario.failed?
#browser.driver.save_screenshot("screenshot.png")
embed("screenshot.png", "image/png")
end
end
A link with text "Screenshot" IS added to the report, but the URL is
the project directory path ("c:\ruby\cucumber\project_name") rather
than a direct link to the file ("c:\ruby\cucumber\project_name\screenshot.png"). I have tried a number of different image formats
and direct paths using Dir.pwd with the same results each time.
What am I missing?
Thanks
Windows XP
Ruby 1.8.7
watir-webdriver (0.2.4)
cucumber (0.10.3)
Aslak:
Try this:
After do |scenario|
if scenario.failed?
encoded_img = #browser.driver.screenshot_as(:base64)
embed("data:image/png;base64,#{encoded_img}",'image/png')
end
end
Aslak
Adam:
Aslak was able to see the embedded
image in the file that I emailed him,
while I was still unable to do so in
IE 8. I tried it out in Firefox 3.6
and the image appears as expected.
The problem may have originally been
with the embedding method itself (or
rather, my use of it), but using
Aslak's base64 solution it only fails
to work in the Internet Explorer
browser.
Aslak:
I believe Base64-encoding of images in
HTML pages [1] works in all decent
browsers (sorry, IE is not one of
them). However, it should work in
IE:
http://dean.edwards.name/weblog/2005/06/base64-ie/
(but maybe they broke it in IE8, or
maybe it only works with gifs, or
maybe IE needs a special kind of
base64 encoding, or maybe you should
just ditch IE)
If being able to read cucumber html
reports with screenshots in IE is
really important to you, you could
always write each image to disk:
png = #browser.driver.screenshot_as(:png)
path = (0..16).to_a.map{|a| rand(16).to_s(16)}.join + '.png' # Or use some GUID library to make a unique filename - scenario names are not guaranteed to be unique.
File.open(path, 'wb') {|io| io.write(png)}
embed(path, 'image/png')
Obviously you have to make sure the
relative path you pass to embed is
right (depending on where you write
the html itself)
[1]
http://en.wikipedia.org/wiki/Data_URI_scheme
HTH, Aslak

Render HTML as Firefox would, without running Firefox

What command-line utility renders HTML as Firefox would, creating a
static image, without actually running Firefox and xwd (or ScreenGrab,
etc)?
Since all of Firefox's rendering libraries are open source, I'm
assuming someone's written something like this? It would be very
useful.
I realize static images can't have Flash animation (animated GIF/PNG
notwithstanding), JavaScript, etc, so I'm just looking for something
that renders plain HTML.
html2ps is worth a try, although it does not seem to use the css style sheets. This is a serious limitation.
On Debian/Ubuntu, it is provided as a package, so the classical sudo apt-get install html2ps will be fine.
(I know this has been given in the comments, but for the future reader, I thought it might be easier to find as an answer.)
You could write a small script which simply runs firefox using the command line options does a screen-shot, then closes firefox. Should only be about 3 lines of code to get started.
firefox -url http://mysite.com/homepage.php
https://developer.mozilla.org/en/Command_Line_Options

Resources