Automated GUI Testing
by George Nistorica
|
Pages: 1, 2, 3, 4
After sending the right combination of keys (Alt+F, O), the code expects
that the window containing the Button will pop up. Then it uses
FindWindowLike() again, using as a search item the title of the
window containing the button (in this case, here). Remember what I said
about the windows hierarchy?
Next, it ensures that the Button window has the focus, although this is not
entirely necessary at this point. After bringing the window to the front, the code
searches for a button in the window (I already know that there's only one
button there).
@button = FindWindowLike( $window[0], "", "Button" );
This narrows down the search: "Search for a window of the class
Button under the window that has the ID $window[0],"
the window having the ID in $window[0] having been previously found by
its title.
PushChildButton( $window[0], $button[0], 0.25 );
is here just for the power of example, as it doesn't work for the Tk button. It would work for a native Win32 button.
The trick is that the code can still push it using the mouse! Having the
button ID, as returned by FindWindowLike(), the code calls the
click_on_the_middle_of_window function.
sub click_on_the_middle_of_window {
my $window = shift;
print "* Moving the mouse over the window id: $window\n";
my ( $left, $top, $right, $bottom ) = GetWindowRect($window);
MouseMoveAbsPix( ( $right + $left ) / 2, ( $top + $bottom ) / 2 );
sleep(1);
print "* Left Clicking on the window id: $window\n";
SendMouse("{LeftClick}");
sleep(1);
}
The function takes a window ID as its parameter, searches its rectangle
using GetWindowRect(), and then moves the mouse pointer right in the
middle of it with MouseMoveAbsPix().
With the pointer over the button, sending LeftClick presses the
button.
Moving Around with the Mouse
As seen earlier, moving the mouse is straightforward: just use
MouseMoveAbsPix(). It takes as parameters the coordinates where
you want the pointer to be (horizontal and vertical positions) in pixels.
It is useful to use other two functions in conjunction:
SendMouse() and GetWindowRect().
SendMouse sends a mouse action to the Desktop. It takes only
one parameter: a mouse action such as {LeftDown},
{LeftUp}, or {LeftClick}. For more details, see the
function's documentation.
You can also move the mouse wheel using MouseMoveWheel(). It
takes a positive or a negative argument, indicating the direction of the
motion.
To send an action, you need to know where we send it. Usually you will move
the mouse pointer over a window. GetWindowRect() is useful to find the
coordinates of a window.
It can be simpler to create a wrapper around these three functions in order
to move the mouse pointer over a selected window, and then generate a mouse action, as I did
with click_on_the_middle_of_window().
Further Reading
Here are some links you may find useful.
- Win32::GuiTest documentation
- Win32::GuiTest::Examples
- The PerlGuiTest group on Yahoo; this is quite an active group.
- Win32::GuiTest extended tutorial
You must be logged in to the O'Reilly Network to post a talkback.
Showing messages 1 through 2 of 2.
- Defects in the sample programs
2005-08-13 01:43:19 Agent [Reply]
The sample programs are pretty cool except for a few defects.
It's important to run tester.pl without starting tested.pl manually. Actually, tester.pl will spawn tested.pl internally.
I think it's incorrect to hard-code the Perl installation path to tester.pl:
# path to where the perl interpreter resides
my $perl = 'c:\Perl\bin\perl.exe';
I installed Perl on D:\ not C:\, which really caused problems on my platform. When I modified the code a bit, they all work fine.- Defects in the sample programs
2005-08-16 05:58:19 UltraDM [Reply]
" It's important to run tester.pl without starting tested.pl manually. Actually, tester.pl will spawn tested.pl internally."
I consider it important that tester.pl spawns the tested.pl. It is a feature ;-)
" I think it's incorrect to hard-code the Perl installation path to tester.pl:"
This is half-true. The sample programs are examples on testing GUIs, not guides to programming. Moreover, things that might be variable are coded as $vars at the top of the program to be changed if needed.
- Defects in the sample programs



