AutoItX.pm

filter: Statementstmt 27.7% 15 / 54 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 44.4% 4 / 9 Podpod 100% 4 / 4 totaltotal 19.8% 23 / 116 CC 19 SCAR 45.3
CC
19
Cov
16%
new
182.0
list_windows
20.0
debug
12.0
CRAP 92.6
« AutoItX/Window.pm ↑ Summary AutoItX/Control.pm »
1 package Win32::AutoItX;
2  
3 =head1 NAME
4  
5 Win32::AutoItX - Automate the Windows GUI using AutoItX
6  
7 =head1 SYNOPSIS
8  
9 use Win32::AutoItX;
10  
11 my $a = Win32::AutoItX->new;
12  
13 ### AutoItX native methods ###
14  
15 my $pid = $a->Run('calc.exe');
16  
17 my $clipboard_text = $a->ClipGet;
18 $a->ClipPut("Win32::AutoItX rulez!");
19
20 my $color = $a->PixelGetColor(42, 42);
21  
22 ### Perlish methods ###
23  
24 my $window = $a->get_window('Calculator');
25 $window->wait;
26 for my $control ($window->find_controls) {
27 local $\ = "\n";
28 print "Control $control";
29 print "\thandle: ", $control->handle;
30 print "\ttext: ", $control->text;
31 print "\tx: ", $control->x, "\ty: ", $control->y;
32 print "\twidth: ", $control->width, "\theight: ", $control->height;
33 }
34  
35 my $button_2 = $window->find_controls('2', class => 'Button');
36 my $button_3 = $window->find_controls('3', class => 'Button');
37 my $button_plus = $window->find_controls('+', class => 'Button');
38 my $button_eq = $window->find_controls('=', class => 'Button');
39 my $result = $window->find_controls('0', class => 'Static');
40  
41 $button_2->click;
42 $button_3->click;
43 $button_plus->click;
44 $button_3->click;
45 $button_2->click;
46 $button_eq->click;
47  
48 print "23 + 32 = ", $result->text, "\n";
49  
50  
51  
52 =head1 DESCRIPTION
53  
54 Win32::AutoItX helps to automate the Windows GUI using the AutoItX COM
55 interface. To use this module you have to install AutoIt v3
56 (https://www.autoitscript.com/autoit3/) or register the AutoItX COM/ActiveX
57 component.
58  
59 On the first constructor (L</new>) invoke it tries to initialize the COM
60 library. To avoid issues with the security context don't import L<Win32::OLE>
61 module in the same script using C<use>.
62  
63 =cut
64  
65 1 our $VERSION = '1.01';
66  
67 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 1 ▶use Win32::AutoItX::Window;
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 1 my $initialized;
74  
75 =head1 METHODS
76  
77 =head2 new
78  
79 $obj = Win32::AutoItX->new(%options)
80  
81 creates a new instance of Win32::AutoItX object.
82  
83 Available options:
84  
85 =over
86  
87 =item debug
88  
89 enables the debug mode (Win32::AutoItX will print additional information for
90 debugging).
91  
92 =item ole_warn
93  
94 determines the behavior of the L<Win32::OLE> module when an error happens.
95 Please see L<Win32::OLE/Warn>. Default is 3 (Carp::croak).
96  
97 =item ole_cp
98  
99 determines the codepage used by all translations between Perl strings and
100 Unicode strings used by the OLE interface. Please see L<Win32::OLE/CP>. Default
101 is CP_UTF8.
102  
103 =back
104  
105 =cut
106  
107 0 ▶sub new {
Subroutine new: not called CC=13 SCAR=52.0
Pod documented
108 0 my $class = shift;
109  
110 # Initialize COM and set Impersonation Level to RPC_C_IMP_LEVEL_DELEGATE
111 0 ▶ if (not $initialized or $initialized != $$) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $initialized or $initialized != $$) 0 0 0
Condition0%0/3
not $initialized or $initialized != $$
l!l&&r!l&&!r
000
MC/DC0%0/2
not $initialized or $initialized != $$
not $initialized $initialized != $$
Truth table0%0/3
not(A) or B
A= not $initializedB= $initialized != $$
ABresult
1X1
011
000
112 0 eval {
113 0 require Win32::API;
114 0 ▶ my $CoInit = Win32::API->new(
Branch: 1 branch
BranchTrueFalseTotal
my $CoInit = "Win32::API"->new("OLE32.DLL", "CoInitialize", "P", "N") or croak("Can't find CoInitialize") 0 0 0
115 "OLE32.DLL", "CoInitialize", "P", "N"
116 ) or croak "Can't find CoInitialize";
117 0 ▶ my $CoInitSec = Win32::API->new(
Branch: 1 branch
BranchTrueFalseTotal
my $CoInitSec = "Win32::API"->new("OLE32.DLL", "CoInitializeSecurity", "PNPPNNPNP", "N") or croak("Can't find CoInitializeSecurity") 0 0 0
118 "OLE32.DLL", "CoInitializeSecurity", "PNPPNNPNP", "N"
119 ) or croak "Can't find CoInitializeSecurity";
120  
121 0 my $result = $CoInit->Call(0);
122 0 ▶ croak "CoInitialize failed: $result"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $result or $result != 0 0 0 0
Condition0%0/3
not defined $result or $result != 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $result or $result != 0
not defined $result $result != 0
Truth table0%0/3
not(A) or B
A= not defined $resultB= $result != 0
ABresult
1X1
011
000
123 if not defined $result or $result != 0;
124 0 $result = $CoInitSec->Call(0, -1, 0, 0, 0, 4, 0, 0, 0);
125 0 ▶ croak "CoInitializeSecurity failed: $result"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $result or $result != 0 0 0 0
Condition0%0/3
not defined $result or $result != 0
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $result or $result != 0
not defined $result $result != 0
Truth table0%0/3
not(A) or B
A= not defined $resultB= $result != 0
ABresult
1X1
011
000
126 if not defined $result or $result != 0;
127 };
128 0 ▶ carp $@ if $@ and $ENV{AUTOITX_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $@ and $ENV{"AUTOITX_DEBUG"} 0 0 0
Condition0%0/3
$@ and $ENV{"AUTOITX_DEBUG"}
!ll&&!rl&&r
000
MC/DC0%0/2
$@ and $ENV{"AUTOITX_DEBUG"}
$@ $ENV{"AUTOITX_DEBUG"}
Truth table0%0/3
A and B
A= $@B= $ENV{"AUTOITX_DEBUG"}
ABresult
0X0
100
111
129 0 $initialized = $$;
130 }
131  
132 0 require Win32::OLE;
133  
134 my %args = (
135 debug => $ENV{AUTOITX_DEBUG},
136 0 ole_warn => 3,
137 ole_cp => Win32::OLE->CP_UTF8,
138 @_
139 );
140 my $self = {
141 0 ▶ debug => $args{debug} ? 1 : 0,
Branch: 1 branch
BranchTrueFalseTotal
$args{'debug'} ? : 0 0 0
142 };
143 0 Win32::OLE->Option(Warn => $args{ole_warn});
144 0 Win32::OLE->Option(CP => $args{ole_cp});
145 0 $self->{autoit} = Win32::OLE->new('AutoItX3.Control');
146 print "AutoItX version ", $self->{autoit}->version, "\n",
147 "Win32::AutoItX version $VERSION\n"
148 0 ▶ if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
149  
150 0 return bless $self, $class;
151 }
152 #-------------------------------------------------------------------------------
153  
154 =head2 debug
155  
156 $debug_is_enabled = $obj->debug
157 $obj = $obj->debug($enable_debug)
158  
159 if the argument is defined it enables or disables the debug mode and returns
160 the object reference. Otherwise it returns the current state of debug mode.
161  
162 =cut
163  
164 0 ▶sub debug {
Subroutine debug: not called CC=3 SCAR=24.8
Pod documented
165 0 my ($self, $value) = @_;
166 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
167 0 ▶ $self->{debug} = $value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 0 0 0
168 0 return $self;
169 }
170 0 return $self->{debug};
171 }
172 #-------------------------------------------------------------------------------
173  
174 =head2 list_windows
175  
176 $hash_ref = $obj->list_windows($win_title)
177 $hash_ref = $obj->list_windows($win_title, $win_text)
178  
179 returns a hash reference with C<$handler =E<gt> $title> elements. Optionally
180 windows can be filtered by title and/or text.
181  
182 =cut
183  
184 0 ▶sub list_windows {
Subroutine list_windows: not called CC=4 SCAR=30.0
Pod documented
185 0 my $self = shift;
186 0 my $list = $self->{autoit}->WinList(@_);
187 0 ▶ return {} unless ref $list and ref $list eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $list and ref $list eq "ARRAY" 0 0 0
Condition0%0/3
ref $list and ref $list eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $list and ref $list eq "ARRAY"
ref $list ref $list eq "ARRAY"
Truth table0%0/3
A and B
A= ref $listB= ref $list eq "ARRAY"
ABresult
0X0
100
111
188 0 my %result;
189 0 for my $i (1 .. $#{$list->[0]}) {
190 0 $result{$list->[1][$i]} = $list->[0][$i];
191 }
192 0 return \%result;
193 }
194 #-------------------------------------------------------------------------------
195  
196 =head2 get_window
197  
198 $window = $a->get_window($title)
199 $window = $a->get_window($title, $text)
200  
201 returns a L<Win32::AutoItX::Window> object for the window with specified title
202 and text (optionally).
203  
204 =cut
205  
206 0 ▶sub get_window {
Subroutine get_window: not called CC=1 SCAR=6.9
Pod documented
207 0 return Win32::AutoItX::Window->new(@_);
208 }
209 #-------------------------------------------------------------------------------
210  
211 =head2 AutoItX methods
212  
213 This module also autoloads all AutoItX methods. For example:
214  
215 $obj->WinActivate($win_title) unless $obj->WinActive($win_title);
216  
217 Please see AutoItX Help file for documenation of all available methods.
218  
219 =cut
220  
221 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=2 SCAR=17.9
222 0 my $self = shift;
223 0 my $method = our $AUTOLOAD;
224 0 $method =~ s/.*:://;
225 0 ▶ print "Call AutoItX method $method with params: @_\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
226 if $self->debug;
227 0 $self->{autoit}->$method(@_);
228 }
229 #-------------------------------------------------------------------------------
230  
231 =head1 ENVIRONMENT VARIABLES
232  
233 =head2 AUTOITX_DEBUG
234  
235 enables additional output to the STDOUT. Can be overwrited with C<debug> option
236 in the constructor (L</new>) or with method L</debug>.
237  
238 =head1 SEE ALSO
239  
240 =over
241  
242 =item L<Win32::AutoItX::Window>
243  
244 =item L<Win32::AutoItX::Control>
245  
246 =item AutoItX Help
247  
248 =item https://www.autoitscript.com/autoit3/docs/
249  
250 =item L<Win32::OLE>
251  
252 =back
253  
254 =head1 AUTHOR
255  
256 Mikhail Telnov E<lt>Mikhail.Telnov@gmail.comE<gt>
257  
258 =head1 COPYRIGHT
259  
260 This software is copyright (c) 2017 by Mikhail Telnov.
261  
262 This library is free software; you may redistribute and/or modify it
263 under the same terms as Perl itself.
264  
265 =cut
266  
267 1 1;
« AutoItX/Window.pm ↑ Summary AutoItX/Control.pm »