CaptureDesktop Sample

Description
This sample script uses Phantom's built-in bitmap functions to capture and compare bitmaps of the desktop. The script declares a function to capture a window's bitmap, and applies a mask to the bitmap. The mask is simply a rectangle of black pixels along the bottom of the window (in this case, the bottom of the desktop where the taskbar nominally is). This prevents changes to the system clock or tray icons from being saved to the bitmap file (and therefore resulting in a false miscompare). The script performs a simple test on Notepad to illustrate how to use the capture function and comparison on either side of a testcase. While this script is used on the desktop window, it could in theory be used on any window to verify that it has returned to its original state after a test completes.

This script illustrates the use of the following Phantom features:

- Bitmap capture
- Desktop window retrieval
- Bitmap mask generation
- Bitmap comparison
- Exceptions
- Bitmap functions
- User defined functions

To use this sample, simply copy and paste it into a new script and save it. The script assumes that the Windows TaskBar is on the bottom of the desktop.

[ Back to Samples ]

CaptureDesktop
# Phantom Sample Script
# This script shows how to use a user-defined function to capture
# the bitmap of a window and apply a mask to it.  It also shows
# how to compare two masked bitmaps to see if they are equal.  The
# script does a simple test on Notepad and captures and compares
# the bitmap of the desktop.  However, the function can be used
# on any window, and the mask can be adjusted to create any
# rectangle.
#
# This sample illustrates the use of bitmaps, masks, bitmap
# functions, and accessing the desktop window. It also illustrates
# user- defined functions and exception usage.

#################################################################
# SaveMaskedBitmap Function
# In this case, the desktop window is passed to this function.
# A mask is created that will block out the taskbar (assuming the
# taskbar is on the lower edge of the desktop).  This is in case
# an icon changed or the time changed.  We dont want this to cause
# a false failure.  Note that the origin (0,0) for pixel positions
# is the bottom left of the bitmap, and that all pixel positions
# are 0-based.
function
void SaveMaskedBitmap(window Target, string SaveName){
  # Capture the bitmap of the input window
  bitmap b = Target.CaptureBitmap();
  
  # Get the width and height ot captured bitmap.  This
  # corresponds to the width and height of the window.
  int width = b.GetWidth();
  int height = b.GetHeight();

  # Create x-values for mask rectangle
  int X;
  X[0] = 0;          # Left edge of window
  X[1] = width-1;    # Right edge of window

  # Create y-values for mask rectangle
  int Y;
  Y[0] = 0;         # Bottom of window
  Y[1] = 30;        # 30 pixels from bottom of window

  # Set the mask pixels to black (bottom rectangle of
  # window)
  b.SetPixels(X,Y,0,0,0);

  # Save the bitmap.  You can look at the bitmap file
  # in any image editor to see the effect the mask has on the
  # final bitmap.  It will be a black rectangl eon the bottom
  # of the bitmap.
  b.Save(SaveName);
}

#################################################################
# Begin test code

# Get the desktop window
window w = GetDesktopWindow();

# Save the entire desktop to a bitmap, with a mask on
# the taskbar.
SaveMaskedBitmap(w, "before.bmp");

# Do testing here.  In this example, a simple test
# is done with Notepad.
System("c:\\windows\\system32\\notepad.exe");
window Notepad = MainWin("Notepad*", "Notepad");
Notepad.Maximize();
Notepad.Restore();
Notepad.Close();

# Recapture the bitmap, with the mask, for comparison.
SaveMaskedBitmap(w, "after.bmp");

# Declare local bitmaps to hold the before and after bitmaps
bitmap before;
bitmap after;

# Load the before and after bitmaps
before.Load("before.bmp");
after.Load("after.bmp");

# Compare the bitmaps.  If they are not identical (they should be,
# since the mask obscures the time and icons if they changed), then
# produce and throw an exception.
if(before != after){
  exception e;
  e.SetError("Application did not close cleanly");
  e.throw();
}

# Test completed successfully.
disp("Test completed successfully");

Copyright © 2000-2009 Phantom Automated Solutions, Inc.
[ Home ] [ Contact ] [ Privacy Policy ] [System Requirements]