Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I played recently with Win32::GuiTest. A neat module by Ernesto Guisado, already mentioned by monks. Great for sending random keystrokes and mouse clicks to your application. It does not offer, however, any help with learning the mouse moves or clicks (neither do I :-).

The script below is a demo monkey test, that beats on the Windows Calculator app.

When you start this kind of script on your machine, you may have a problem: it takes over your mouse cursor when sending the mouse clicks. If you programmed it for an hour's worth of clicks, started it, and then you changed your mind, how would you stop it?

I kludged a solution based on the Term::ReadKey module, which I wrapped in my ReadKeypress module, below. The script frequently checks whether you pressed the Esc key, and to do that, it activates the comand window from which you launched the script, and calls the nonblocking ReadKeypress::escape().

If anyone knows of a more elegant solution for this problem, please let me know.

Rudif

Anyway, here goes the demo

#! perl # /pl/GuiTest/monkeytest-calculator.pl # rudif@lecroy.com 12 Dec 2001 # a demo monkey test # morphed from Win32::GuiTest test scripts # fire up the windows Calculator app # then start this script from a Command Prompt window # to interrupt the script, press Esc several times use strict; use Win32::GuiTest qw/ FindWindowLike GetChildDepth GetChildWindows GetClassName GetDesktopWindow GetScreenRes GetWindowRect GetWindowText IsCheckedButton IsWindow SendKeys SetForegroundWindow WMGetText MouseMoveAbsPix SendLButtonDown SendLButtonUp GetCursorPos SendMouseMoveRel SendMouse /; use ReadKeypress; # Find App window my $app = "Calculator"; my @windows = FindWindowLike(0, $app); my $appwin = $windows[0]; print "not " unless scalar @windows == 1; printf "ok 5: %08x \n", $appwin; # App window coordinates (pixels) on desktop my ($left, $top, $right, $bottom) = GetWindowRect($appwin); my ($width, $height) = ($right - $left, $bottom - $top); printf "ok 6: appwin $left, $top, $right, $bottom, ($width, $height)\n +", $appwin; # Find the Command Prompt window my @cmds = FindWindowLike(0, "Command Prompt", ""); printf STDERR "ok 7: %08x \n", $cmds[0]; my $cmdprompt = $cmds[0]; SetForegroundWindow($appwin); print "ok 7\n"; my ($margx, $margy) = ( 30, 40 ); my ($awleft, $awtop) = ($left+$margx, $top+$margy); my ($awright, $awbottom) = ($right-$margx, $bottom-$margy); my ($stepx, $stepy) = (60,40); # approximates the Menu spacing my ($maxposx, $maxposy) = ( int(($awright - $awleft) / $stepx), int(($ +awbottom - $awtop) / $stepy)); my ($minpixx, $minpixy) = ($awleft, $awtop); my ($maxpixx, $maxpixy) = ($awleft + $maxposx * $stepx, $awtop + $maxp +osy * $stepy); my $pause1 = 1000; MouseMoveAbsPix($minpixx, $minpixy); SendKeys "{PAUSE $pause1}"; MouseMoveAbsPix($maxpixx, $minpixy); SendKeys "{PAUSE $pause1}"; MouseMoveAbsPix($maxpixx, $maxpixy); SendKeys "{PAUSE $pause1}"; MouseMoveAbsPix($minpixx, $maxpixy); SendKeys "{PAUSE $pause1}"; MouseMoveAbsPix($minpixx, $minpixy); SendKeys "{PAUSE $pause1}"; my $pause = 50; print STDERR "PRESS ESCAPE SEVERAL TIMES TO QUIT\n"; print STDERR "\nrandom scan of menus\n"; my $n = 33; for my $x (0..$n) { for my $y (0..4) { my $posx = randposx($x); # random column MouseMoveAbsPix $posx, posy(0); # top menu line SendMouse "{LEFTCLICK}"; SendKeys "{PAUSE $pause}"; MouseMoveAbsPix $posx, randposy($y); # down in the same column +, random SendMouse "{LEFTCLICK}"; SendKeys "{PAUSE $pause}"; checkEscapeAndPause(50); } } print STDERR "\nlinear scan of menus\n"; for my $x (0..$maxposx) { for my $y (0..$maxposy) { MouseMoveAbsPix posx($x), posy(0); # top menu line SendMouse "{LEFTCLICK}"; SendKeys "{PAUSE $pause}"; MouseMoveAbsPix posx($x), posy($y); # down in the same column SendMouse "{LEFTCLICK}"; SendKeys "{PAUSE $pause}"; checkEscapeAndPause(50); } } print STDERR "\ndone\n"; # # subs # sub randpos { (randposx(), randposx()); } sub randposx { posx($maxposx * rand()); } sub randposy { posy($maxposy * rand()); } sub posx { my $posx = shift; ($minpixx + $stepx * $posx); } sub posy { my $posy = shift; ($minpixy + $stepy * $posy); } sub checkEscapeAndPause { my $millisec = shift || 10; # print STDERR "checkEscapeAndPause $millisec\n"; print STDERR "."; SendKeys "{PAUSE $millisec}"; SetForegroundWindow($cmdprompt); SendKeys "{PAUSE 200}"; if (ReadKeypress::escape()) { print STDERR "\nEscaped!\n"; exit(); } SetForegroundWindow($appwin); } __END__
#!perl -w use strict; use warnings; package ReadKeypress; use Term::ReadKey; use Fcntl; if ($^O =~ /Win32/i) { sysopen(IN,'CONIN$',O_RDWR) or die "Unable to open console input:$ +!"; sysopen(OUT,'CONOUT$',O_RDWR) or die "Unable to open console outpu +t:$!"; } else { open(IN,"</dev/tty"); *OUT = *IN; } ReadMode 4, \*IN; sub keypress { ReadKey(-1, \*IN) || ''; } sub escape { chr(27) eq keypress(); } 1; __END__ =head1 NAME ReadKeypress - Perl extension for nonblocking polling the keyboard. =head1 SYNOPSIS use ReadKeypress; print "press Escape to stop\n"; for (1..42) { sleep 1; print "$_ "; print "Escaped!\n" and exit if ReadKeypress::escape(); } =head1 DESCRIPTION ReadKeypress defines two methods for non-blocking reading of keyboard. =over 4 =item ReadKeypress::keypress() Tries to read one character from keyboard and returns immediately, wit +hout blocking. It returns the character read if any was available, the empty string ' +' otherwise. =item ReadKeypress::escape() Returns immediately, with a true value if Escape (x1B) was pressed, fa +lse value otherwise. =back =head1 AUTHOR Rudi Farkas rudif@lecroy.com =head1 SEE ALSO Term::ReadKey documentation. =cut

In reply to Re: Application for 'Quality Assurance' by Rudif
in thread Application for 'Quality Assurance' by brianarn

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2022-01-25 19:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (67 votes). Check out past polls.

    Notices?