Desktop Screenshots with Watir, win32screenshot, RMagick and Snagit

Here is what I had with win32screenshot and RMagick (later replaced with Snagit)

# Runtime Test Housekeeping place
module TestRun

  # at runtime takes a screenshot of the desktop and saves it in log dir.
  # you must install win32screenshot gem and RMagic package for this to work.
  def self.screenshot(context=nil)
    if $testrun[:screenshot] == true
      # prevent runtime error when screenshot gem and RMagic are not installed on client machine.
      begin
        require ‘win32screenshot’
        require ‘RMagick’
        width, height, bitmap = Win32::Screenshot.desktop
        imglist = Magick::ImageList.new
        imgl = imglist.from_blob(bitmap)
        time_stamp_s = Time.new.strftime(’%m%d_%H%M_%S’)
        screenshot_filename = context ? “#{time_stamp_s}_#{context}.png” : “#{time_stamp_s}.png”
        imgl.write(File.join(logpath,screenshot_filename))
        $log.info(”Screenshot captured: #{screenshot_filename}”)
      rescue Exception => ex
        $log.error(”TestRun.screenshot(#{context} — #{ex}”)
      end

    end

  end

end

#usage
TestRun.screenshot() # => makes a MMDD_HHMM_SS.png file
TestRun.screenshot(’blabla’) # appends the string to filename.

The above is an older solution, I’ve had it working for a while but now it has stopped after I upgraded rubygems system. It still works on my other test machines but not on my primary box.

Instead I have installed snagit 8 and I am using it for web page capture. it’s better, definitely better with the COM interface.
I just wanted to post the code here in case I need to refer to it again in the future. (note: logpath method returns path to log directory. The global $testrun hash holds runtime options for the test framework.

Thanks win32screenshot and thanks RMagick, it was fun using it, moving on to snagit OLE interface.

UPDATE:
Here is my current reimplementation using snagit

def self.screenshot(context=nil)
    if $testrun[:screenshot] == true #runtime options guard access

      begin
        require ‘win32ole’
        snagit = WIN32OLE.new(’Snagit.ImageCapture’)
        time_stamp_s = Time.new.strftime(’%m%d_%H%M_%S’)
        screenshot_filename = context ? “#{time_stamp_s}_#{context}” : “#{time_stamp_s}”
        # config input
        snagit.Input = 0 #desktop
        #configure output
        snagit.Output = 2 #file
        snagit.OutputImageFile2.FileType = 5 #:png
        snagit.OutputImageFile2.FileNamingMethod = 1 # fixed #
        #location
        snagit.OutputImageFile2.Directory = logpath   # set directy where filename will be saved
        snagit.OutputImageFile2.Filename = screenshot_filename
        # do the duty
        snagit.Capture
        # wait for capture to complete
        until snagit.IsCaptureDone do
          sleep 0.5
        end
        $log.info(”Screenshot captured: #{screenshot_filename}”)
      rescue Exception => ex
        $log.error(”TestRun.screenshot(#{context} — #{ex}”)
      end
    end
  end

The above works pretty well. my next thing is to implement taking a screenshot of a page with setting Input active window and scrolling it. It’ll be fun.

close Reblog this comment
blog comments powered by Disqus