Win32::GuiTest - Alternate distribution of Perl GUI Test Utilities.
use Win32::GuiTest qw(FindWindowLike GetWindowText SetForegroundWindow SendKeys);
$Win32::GuiTest::debug = 0; # Set to "1" to enable verbose mode
my @windows = FindWindowLike(0, "^Microsoft Excel", "^XLMAIN\$"); for (@windows) { print "$_>\t'", GetWindowText($_), "'\n"; SetForegroundWindow($_); SendKeys("%fn~a{TAB}b{TAB}{BS}{DOWN}"); }
// This batch file comes with MS Visual Studio. Running // it first might help with various compilation problems. vcvars32.bat
perl makefile.pl nmake nmake test nmake install
If you are using ActivePerl 5.6 (http://www.activestate.com/Products/ActivePerl/index.html) you can install the binary package I am including instead. You will need to enter PPM (Perl Package Manager) from the command-line. Once you have extracted the files I send you to a directory of your machine, enter PPM and do like this:
C:\TEMP>ppm PPM interactive shell (2.0) - type 'help' for available commands. PPM> install C:\temp\win32-guitest.ppd Install package 'C:\temp\win32-guitest.ppd?' (y/N): Y Retrieving package 'C:\temp\win32-guitest.ppd'... Writing C:\Perl\site\lib\auto\Win32\GuiTest\.packlist PPM>
I extracted them to 'c:\temp', please use the directory where you extracted the files instead.
Most GUI test scripts I have seen/written for Win32 use some variant of Visual Basic (e.g. MS-VB or MS-Visual Test). The main reason is the availability of the SendKeys function.
A nice way to drive Win32 programs from a test script is to use OLE Automation (ActiveX Scripting), but not all Win32 programs support this interface. That is where SendKeys comes handy.
Some time ago Al Williams published a Delphi version in Dr. Dobb's (http://www.ddj.com/ddj/1997/careers1/wil2.htm). I ported it to C and packaged it using h2xs...
The tentative name for this module is Win32::GuiTest (mostly because I plan to include more GUI testing functions).
I've created a Yahoo Group for the module that you can join at http://groups.yahoo.com/group/perlguitest/join
SendKeys($keys[,$delay])
The keystrokes to send are specified in KEYS. There are several characters that have special meaning. This allows sending control codes and modifiers:
~ means ENTER + means SHIFT ^ means CTRL % means ALT
The parens allow character grouping. You may group several characters, so that a specific keyboard modifier applies to all of them.
E.g. SendKeys(``ABC'')
is equivalent to SendKeys(``+(abc)'')
The curly braces are used to quote special characters (SendKeys(``{+}{{}'') sends a '+' and a '{'). You can also use them to specify certain named actions:
Name Action
{BACKSPACE} Backspace {BS} Backspace {BKSP} Backspace {BREAK} Break {CAPS} Caps Lock {DELETE} Delete {DOWN} Down arrow {END} End {ENTER} Enter (same as ~) {ESCAPE} Escape {HELP} Help key {HOME} Home {INSERT} Insert {LEFT} Left arrow {NUMLOCK} Num lock {PGDN} Page down {PGUP} Page up {PRTSCR} Print screen {RIGHT} Right arrow {SCROLL} Scroll lock {TAB} Tab {UP} Up arrow {PAUSE} Pause {F1} Function Key 1 ... ... {F24} Function Key 24 {SPC} Spacebar {SPACE} Spacebar {SPACEBAR} Spacebar {LWI} Left Windows Key {RWI} Right Windows Key {APP} Open Context Menu Key
All these named actions take an optional integer argument, like in {RIGHT 5}. For all of them, except PAUSE, the argument means a repeat count. For PAUSE it means the number of milliseconds SendKeys should pause before proceding.
In this implementation, SendKeys always returns after sending the keystrokes. There is no way to tell if an application has processed those keys when the function returns.
SendMouse($command)
{LEFTDOWN} left button down {LEFTUP} left button up {MIDDLEDOWN} middle button down {MIDDLEUP} middle button up {RIGHTDOWN} right button down {RIGHTUP} right button up {LEFTCLICK} left button single click {MIDDLECLICK} middle button single click {RIGHTCLICK} right button single click {ABSx,y} move to absolute coordinate ( x, y ) {RELx,y} move to relative coordinate ( x, y )
Note: Absolute mouse coordinates range from 0 to 65535. Relative coordinates can be positive or negative. If you need pixel coordinates you can use MouseMoveAbsPix.
Also equivalent low-level functions are available:
SendLButtonUp() SendLButtonDown() SendMButtonUp() SendMButtonDown() SendRButtonUp() SendRButtonDown() SendMouseMoveRel(x,y) SendMouseMoveAbs(x,y)
MouseMoveAbsPix($x,$y)
# Moves to x=200, y=100 in pixel coordinates. MouseMoveAbsPix(200, 100);
FindWindowLike($window,$titleregex,$classregex,$childid,$maxlevel)
You may specify the handle of the window to search under. The routine searches through all of this windows children and their children recursively. If 'undef' then the routine searches through all windows. There is also a regexp used to match against the text in the window caption and another regexp used to match against the text in the window class. If you pass a child ID number, the functions will only match windows with this id. In each case undef matches everything.
PushButton($button[,$delay])
PushChildButton(GetForegroundWindow, BUTTON, DELAY)
PushChildButton($parent,$button[,$delay])
parent - the parent window of the button
button - either the text in a button (e.g. ``Yes'') or the control ID of a button.
delay - the time (0.25 means 250 ms) to wait between the mouse down and the mouse up event. This is useful for debugging.
WaitWindowLike($parent,$wndtitle,$wndclass,$wndid,$depth,$wait)
parent - Where to start (parent window)
wndtitle - Regexp for the window title
wndclass - Regexp for the window class name
wndid - Numeric Window or Control ID
depth - How deep should we search before we stop
wait - How many seconds should we wait before giving up
WaitWindow($wndtitle,[$wait])
wndtitle - Regexp for the window title
wait - How many seconds should we wait before giving up
MenuSelect($menupath,$window,$menu)
Simple Examples: # Exit foreground application through application menu. MenuSelect(``&File|E&xit'');
# Exit foreground application through system menu MenuSelect("&Close", 0, GetSystemMenu(GetForegroundWindow(), FALSE));
window = Regexp for a Window caption / Child caption, or just a Child ID.
parent = Handle to parent window. Default is foreground window. Use
GetDesktopWindow()
return value for this if clicking on an application
title bar.
x_offset = Offset for X axis. Default is 0.
y_offset = Offset for Y axis. Default is 0.
button = {LEFT}, {MIDDLE}, {RIGHT}. Default is {LEFT}
delay = Default is 0. 0.50 = 500 ms. Delay between button down and button up.
Simple Examples:
# Click on CE button if its parent window is in foreground. MouseClick('^CE$');
# Right click on CE button if its parent window is in foreground MouseClick('^CE$', undef, undef, undef, '{RIGHT}');
# Click on 8 button window under the specified parent window; where # [PARENTHWND] will be replaced by a parent handle variable. MouseClick('8', [PARENTHWND]);
# Click on Calculator parent window itself MouseClick('Calculator', GetDesktopWindow());
WMGetText($hwnd)
*WMSetText(hwnd,text)
*GetCursorPos()
*GetCaretPos()
SetFocus(hWnd)
GetDesktopWindow()
*GetWindow(hwnd,uCmd)
*GetWindowText(hwnd)
*GetClassName(hwnd)
*GetParent(hwnd)
*GetWindowLong(hwnd,index)
*SetForegroundWindow(hWnd)
*GetChildWindows(hWnd)
IsChild(hWndParent,hWnd)
*GetChildDepth(hAncestor,hChild)
SendMessage(hwnd,msg,wParam,lParam)
*PostMessage(hwnd,msg,wParam,lParam)
*CheckButton(hwnd)
UnCheckButton(hwnd)
GrayOutButton(hwnd)
IsCheckedButton(hwnd)
IsGrayedButton(hwnd)
IsWindow(hwnd)
*ScreenToClient(hwnd,x,y)
*ClientToScreen(hwnd,x,y)
*GetCaretPos(hwnd)
*ASetFocus(hWnd)
*AGetFocus(hwnd)
*AGetActiveWindow(hwnd)
*AGetForegroundWindow()
*SetActiveWindow(hwnd)
*AEnableWindow(hwnd,fEnable)
*ShowWindow(hwnd,nCmdShow)
*AScreenToNorm(x,y)
NormToScreen(x,y)
GetScreenRes()
GetWindowRect(hWnd)
*GetClientRect(hWnd)
*GetComboText(hwnd,index)
GetListText(hwnd,index)
GetComboContents(hWnd)
GetListContents(hWnd)
IsKeyPressed($key)
IsKeyPressed("ESC"); IsKeyPressed("A"); IsKeyPressed("DOWN");
SendRawKey($virtualkey,$flags)
KEYEVENTF_EXTENDEDKEY - Means it is an extended key (i.e. to distinguish between arrow keys on the numeric keypad and elsewhere). KEYEVENTF_KEYUP - Means keyup. Unspecified means keydown.
#Example use Win32::GuiTest qw/:FUNC :VK/;
while (1) { SendRawKey(VK_DOWN, KEYEVENTF_EXTENDEDKEY); SendKeys "{PAUSE 200}"; }
A class to manage a Windows DIB section. Currently limited in functionality to 24-bit images. Pulled from old code into GuiTest when I (jurasz@imb.uni-karlsruhe.de) needed to create several grayscale screen dumps.
Possible future extenstions: other color resolutions, loading, comparison of bitmaps, getting from clipboard.
Synopsis:
$ds = new Win32::GuiTest::DibSect; $ds->CopyWindow($w); $ds->ToGrayScale(); $ds->SaveAs("bla.bmp"); $ds->ToClipboard();
$ds->CopyClient(GetDesktopWindow(), \@{[GetWindowRect($w)]});
1.50.1-ad
Moved to the CHANGES file.
The SendKeys function is based on the Delphi sourcecode published by Al Williams <http://www.al-williams.com/awc/> in Dr.Dobbs <http://www.ddj.com/ddj/1997/careers1/wil2.htm>.
Copyright (c) 1998-2002 Ernesto Guisado, (c) 2004 Dennis K. Paulsen. All rights reserved. This program is free software; You may distribute it and/or modify it under the same terms as Perl itself.
Ernesto Guisado (erngui@acm.org), http://triumvir.org
Jarek Jurasz (jurasz@imb.uni-karlsruhe.de), http://www.uni-karlsruhe.de/~gm07 wrote
DibSect and some other pieces (see Changes
for details).
Dennis K. Paulsen (ctrondlpaulsden@yahoo.com) wrote various pieces (See Changes
for
details).
Thanks very much to: