Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Problems with WIN32::GUITEST

by dasgar (Priest)
on Jan 26, 2013 at 05:22 UTC ( #1015462=note: print w/replies, xml ) Need Help??

in reply to Problems with WIN32::GUITEST

Like you, I'm running dwimperl on Windows 7 Home Premium 64-bit. When I tried running the code, the printed result was blank and the calculator never did do anything.

To make a long story short, I ended up tweaking the code to get it to work. I believe that the calculator app in Windows 7 must be designed differently that the calculator app in previous Windows version.

For those that want to gory details and my tweaked version of the code, here's a summary my debug process. (Also, some of the info might be useful for others wanting to use Win32::GuiTest to automate applications.)

First, I decided to double-check the version of Win32::GuiTest that is in dwimperl. That version is 1.58. Since the example code was from version 1.60, I updated my Win32::GuiTest module to 1.60. Still no luck.

Then I took a look at the code for the 'keyboard' portion as well as the perldoc for Win32::GuiTest. Since I never saw any activity in the calculator app, I figured that the calls to simulate keyboard input was not working correctly. When I changed the calls to PushButton to SendKeys, then the script really did get the calculator app to behave correctly.

However, the printed result from the script was blank. After a bit of testing, I changed to the printf line to be in a loop looking for a window whose text contained only digits and printed that as the result. That fixed the 'keyboard' section of the code.

Onto debugging the 'mouse' section. At one point I thought that since I had my calculator app defaulting to the scientific mode instead of standard mode was the source of the problem. Tested both modes, but there was no difference.

I tried printing out the text from each child window object, but didn't see anything matched the calculator button labels. So I figured that I needed more information about the components of the calculator app. Some time ago I had looked into AutoIt Script and still had it installed. So I used their AutoIt Window Info Tool, which helped me get the window IDs of the needed buttons. So I tweaked the code to look for those IDs. Again, the printed result was blank. So I reused the same loop that I used from the 'keyboard' section.

Below is the final version of the code that worked on my system. Not sure if this will work on any other versions of Windows 7 or earlier versions of the Windows OS.

#!perl -w # # Written by Gabor Szabo <> # An example how to access the built in calculator (calc.exe) of Windo +ws. # This code assumes your calulator defaults to the Standard view (and +not the Scientific) use strict; use warnings; use Win32::GuiTest qw(:ALL); if (not @ARGV or ($ARGV[0] ne "keyboard" and $ARGV[0] ne "mouse")) { die "Usage: $0 [keyboard|mouse]\n" } system "start calc.exe"; sleep(1); my @windows = FindWindowLike(undef, "Calculator"); if (not @windows) { die "Could not find Calculator\n"; } if (@windows > 1) { die "There might be more than one Calculators running\n"; } if ($ARGV[0] eq "keyboard") { SendKeys('7'); sleep(1); SendKeys('\*'); sleep(1); SendKeys('5'); sleep(1); SendKeys('='); sleep(2); # Catch the content of the first child, # At this point we can only hope that this is the child that holds the + result # as it does not have a title, maybe it has a type that we can check ? my @children = GetChildWindows($windows[0]); foreach my $child (@children) { printf "Result: %s\n",WMGetText($child) if (WMGetText($child) =~ m/ +^\d+$/s); } SendKeys("%{F4}"); # Alt-F4 to exit } if ($ARGV[0] eq "mouse") { my ($left, $top, $right, $bottom) = GetWindowRect($windows[0]); # find the appropriate child window and click on it my @children = GetChildWindows($windows[0]); foreach my $id (qw(137 92 135 121)) { my ($c) = grep {$id == GetWindowID($_)} @children; my ($left, $top, $right, $bottom) = GetWindowRect($c); MouseMoveAbsPix(($right+$left)/2,($top+$bottom)/2); SendMouse("{LeftClick}"); sleep(1); } foreach my $child (@children) { printf "Result: %s\n",WMGetText($child) if (WMGetText($child) =~ m/ +^\d+$/s); } MouseMoveAbsPix($right-10,$top+10); # this probably depends on the re +solution sleep(2); SendMouse("{LeftClick}"); }

Replies are listed 'Best First'.
Re^2: Problems with WIN32::GUITEST
by Anonymous Monk on Jan 26, 2013 at 13:21 UTC

    To make a long story short, I ended up tweaking the code to get it to work. I believe that the calculator app in Windows 7 must be designed differently that the calculator app in previous Windows version.

    Makes sense, with each new windows version microsoft introduces a new "theme", eventually the details have to change (classnames, extra parent windows, etc etc :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1015462]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2018-05-26 10:16 GMT
Find Nodes?
    Voting Booth?