Watir has some built in functions for taking screenshots, but they are not very good. The best way to take screenshots is by using SnagIt.
Here is an example function for taking screenshots by using SnagIt. This function is also capable of taking screenshots of invisible windows:
def screencapture fname
        snagit = WIN32OLE.new('Snagit.ImageCapture')
        visibilityState=$ie.getIE.Visible
        $ie.getIE.Visible = true 
        $ie.bring_to_front
        $ie.maximize  
        snagit.Input = 1 #window
        snagit.Output = 2 #file
        snagit.InputWindowOptions.XPos = 350  
        # select XPosition for window ID
        snagit.InputWindowOptions.YPos = 350  
        # select YPosition for window ID
        snagit.InputWindowOptions.SelectionMethod = 3  
        # capture window under the X, Y point specified above.
        snagit.AutoScrollOptions.StartingPosition = 3  
        # set scroll to top and left 
        snagit.OutputImageFile.FileType = 5 #:png
        snagit.OutputImageFile.FileNamingMethod = 1 # fixed
        tempStoragePath="c:\\screenshot\\"
        if (! File::exists?( tempStoragePath ))  
            tempStoragePath=Dir.pwd+"//screenshot//"
            if (! File::exists?( tempStoragePath ))  
                Dir.mkdir tempStoragePath
            end      
        end
        snagit.OutputImageFile.Directory = tempStoragePath
        snagit.OutputImageFile.Filename = fname
        snagit.AutoScrollOptions.AutoScrollMethod = 0
        snagit.Capture
        # wait for capture to complete
        until snagit.IsCaptureDone do
            sleep 0.5
        end
        $ie.getIE.Visible = visibilityState
end
If you don't have SnagIt installed, then another easy way to take screenshots is by using PHP.
For example, create following PHP file:
<?php
if(!isset($argv[1]))
{
    echo "Image not specified!";
    exit;
}
else
    $image = $argv[1];
$imagefile = "c://".$image.".png";
$im = imagegrabscreen();
imagepng($im, $imagefile);
imagedestroy($im);
?>
... and then, for example, following Ruby function:
def screencapture fname
    cmd = 'PHP.exe C:\\screenshot.php ' + fname
    puts %x{#{cmd}}
    sleep 8
end 
No comments:
Post a Comment