AutoItX/Window.pm

filter: Statementstmt 24.1% 21 / 87 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 31.5% 6 / 19 Podpod 100% 11 / 11 totaltotal 19.2% 38 / 197 CC 32 SCAR 50.0
CC
32
Cov
14%
find_controls
342.0
wait_control
110.0
new
20.0
CRAP 147.9
↑ Summary AutoItX.pm »
1 package Win32::AutoItX::Window;
2  
3 =head1 NAME
4  
5 Win32::AutoItX::Window - OO interface for Windows
6  
7 =head1 SYNOPSIS
8  
9 use Win32::AutoItX;
10  
11 my $a = Win32::AutoItX->new;
12  
13 my $pid = $a->Run('calc.exe');
14 my $window = $a->get_window('Calculator');
15 $window->wait;
16  
17 local $\ = "\n";
18 print "Handle: $window";
19 print "Title: ", $window->title;
20 print "PID: ", $window->process;
21 print "x = ", $window->x, " y = ", $window->y;
22 print "width = ", $window->width, " height = ", $window->height;
23 $window->maximize;
24 sleep 2;
25 $window->restore;
26 sleep 2;
27 $window->move($window->x + 50, $window->y + 50);
28  
29 my $control = $window->get_focus;
30 print "Focused control is $control with text = ", $control->text;
31  
32 # List all buttons
33 my @buttons = $window->find_controls(undef, class => 'Button');
34 foreach my $c (@buttons) {
35 print "Button '$c' has text ", $c->text;
36 }
37  
38 =head1 DESCRIPTION
39  
40 Win32::AutoItX::Window provides an object-oriented interface for AutoItX
41 methods to operate with Windows.
42  
43 =cut
44  
45 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 1 our $VERSION = '1.00';
49  
50 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶use Scalar::Util qw{ blessed };
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 ▶use Win32::AutoItX::Control;
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 0 ▶use overload fallback => 1, '""' => sub { $_[0]->handle };
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=0.2
55  
56 1 my %Helper_Methods = qw{
57 handle WinGetHandle
58 process WinGetProcess
59 state WinGetState
60 exists WinExists
61 is_active WinActive
62 text WinGetText
63 title WinGetTitle
64 x WinGetPosX
65 y WinGetPosY
66 width WinGetPosWidth
67 height WinGetPosHeight
68 client_width WinGetClientSizeWidth
69 client_height WinGetClientSizeHeight
70 activate WinActivate
71 close WinClose
72 kill WinKill
73 select_menu_item WinMenuSelectItem
74 move WinMove
75 set_on_top WinSetOnTop
76 set_state WinSetState
77 set_title WinSetTitle
78 set_transparency WinSetTrans
79 wait WinWait
80 wait_active WinWaitActive
81 wait_not_active WinWaitNotActive
82 wait_close WinWaitClose
83 };
84  
85 =head1 METHODS
86  
87 =head2 new
88  
89 $window = Win32::AutoItX::Window->new($autoitx, $window_title, $window_text)
90  
91 creates the window object.
92  
93 =cut
94  
95 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
96 0 my $class = shift;
97 0 my %self;
98 0 $self{autoitx} = shift;
99 0 $self{title} = shift;
100 0 $self{text} = shift;
101 0 ▶ $self{text} = '' unless defined $self{text};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self{"text"} 0 0 0
102 croak "The first argument should be a Win32::AutoItX object"
103 0 ▶ unless blessed $self{autoitx} and $self{autoitx}->isa('Win32::AutoItX');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self{"autoitx"}) and $self{"autoitx"}->isa("Win32::AutoItX") 0 0 0
Condition0%0/3
builtin::blessed($self{"autoitx"}) and $self{"autoitx"}->isa("Win32::AutoItX")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($self{"autoitx"}) and $self{"autoitx"}->isa("Win32::AutoItX")
builtin::blessed($self{"autoitx"}) $self{"autoitx"}->isa("Win32::AutoItX")
Truth table0%0/3
A and B
A= builtin::blessed($self{"autoitx"})B= $self{"autoitx"}->isa("Win32::AutoItX")
ABresult
0X0
100
111
104 0 return bless \%self, $class;
105 }
106 #-------------------------------------------------------------------------------
107  
108 =head2 get_class_list
109  
110 @class_list = $window->get_class_list()
111  
112 retrieves list with classes from the window.
113  
114 =cut
115  
116 0 ▶sub get_class_list {
Subroutine get_class_list: not called CC=1 SCAR=6.9
Pod documented
117 0 my $self = shift;
118 my @classes = split /\r?\n/,
119 0 $self->WinGetClassList($self->{title}, $self->{text});
120 0 return @classes;
121 }
122 #-------------------------------------------------------------------------------
123  
124 =head2 get_control
125  
126 $control = $window->get_control($control_id)
127  
128 returns a L<Win32::AutoItX::Control> object for the control specified by id.
129  
130 =cut
131  
132 0 ▶sub get_control {
Subroutine get_control: not called CC=1 SCAR=6.9
Pod documented
133 0 my $self = shift;
134 0 my $control_id = shift;
135 return Win32::AutoItX::Control->new(
136 0 $self->{autoitx}, $self->{title}, $self->{text}, $control_id
137 );
138 }
139 #-------------------------------------------------------------------------------
140  
141 =head2 get_focus
142  
143 $control = $window->get_focus()
144  
145 returns a L<Win32::AutoItX::Control> object for the control that has keyboard
146 focus within the window.
147  
148 =cut
149  
150 0 ▶sub get_focus {
Subroutine get_focus: not called CC=1 SCAR=6.9
Pod documented
151 0 my $self = shift;
152 return Win32::AutoItX::Control->new(
153 $self->{autoitx}, $self->{title}, $self->{text},
154 $self->ControlGetFocus($self->{title}, $self->{text})
155 0 );
156 }
157 #-------------------------------------------------------------------------------
158  
159 =head2 find_controls
160  
161 $control = $window->find_controls($text, %options)
162 @controls = $window->find_controls($text, %options)
163  
164 returns a L<Win32::AutoItX::Control> object (or a list of objects in the list
165 context) for matched contols. C<$text> is a raw string or a C<Regexp>.
166  
167 Available options to filter controls:
168  
169 =over
170  
171 =item class
172  
173 a raw string or a C<Regexp> to filter by Control's class.
174  
175 =item visible
176  
177 get visible controls only (set by default).
178  
179 =item enabled
180  
181 get enabled controls only.
182  
183 =back
184  
185 =cut
186  
187 0 ▶sub find_controls {
Subroutine find_controls: not called CC=18 SCAR=58.3
Pod documented
188 0 my $self = shift;
189 0 my $text = shift;
190 0 my %args = (
191 class => undef,
192 visible => 1,
193 enabled => 0,
194 @_
195 );
196  
197 0 my %by_classes;
198 my @controls;
199 0 foreach my $class ($self->get_class_list) {
200 0 ▶ if (defined $args{class}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"class"}) 0 0 0
201 0 ▶ if (ref $args{class} and ref $args{class} eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args{'class'} and ref $args{'class'} eq 'Regexp') { } 0 0 0
Condition0%0/3
ref $args{"class"} and ref $args{"class"} eq "Regexp"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $args{"class"} and ref $args{"class"} eq "Regexp"
ref $args{"class"} ref $args{"class"} eq "Regexp"
Truth table0%0/3
A and B
A= ref $args{"class"}B= ref $args{"class"} eq "Regexp"
ABresult
0X0
100
111
202 0 ▶ next if $class !~ /$args{class}/;
Branch: 1 branch
BranchTrueFalseTotal
unless $class =~ /$args{"class"}/ 0 0 0
203 } else {
204 0 ▶ next if $class ne $args{class};
Branch: 1 branch
BranchTrueFalseTotal
if $class ne $args{"class"} 0 0 0
205 }
206 }
207 0 $by_classes{$class}++;
208 0 my $control = $self->get_control(
209 "[CLASS:$class; INSTANCE:$by_classes{$class}]"
210 );
211 0 ▶ next if $args{visible} and not $control->is_visible;
Branch: 1 branch
BranchTrueFalseTotal
if $args{"visible"} and not $control->is_visible 0 0 0
Condition0%0/3
$args{"visible"} and not $control->is_visible
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"visible"} and not $control->is_visible
$args{"visible"} not $control->is_visible
Truth table0%0/3
A and not(B)
A= $args{"visible"}B= not $control->is_visible
ABresult
0X0
100
111
212 0 ▶ next if $args{enabled} and not $control->is_enabled;
Branch: 1 branch
BranchTrueFalseTotal
if $args{"enabled"} and not $control->is_enabled 0 0 0
Condition0%0/3
$args{"enabled"} and not $control->is_enabled
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"enabled"} and not $control->is_enabled
$args{"enabled"} not $control->is_enabled
Truth table0%0/3
A and not(B)
A= $args{"enabled"}B= not $control->is_enabled
ABresult
0X0
100
111
213 0 ▶ if (defined $text) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $text) 0 0 0
214 0 my $control_text = $control->text;
215 0 ▶ next unless defined $control_text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $control_text 0 0 0
216 0 ▶ if (ref $text and ref $text eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $text and ref $text eq 'Regexp') { } 0 0 0
Condition0%0/3
ref $text and ref $text eq "Regexp"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $text and ref $text eq "Regexp"
ref $text ref $text eq "Regexp"
Truth table0%0/3
A and B
A= ref $textB= ref $text eq "Regexp"
ABresult
0X0
100
111
217 0 ▶ next if $control_text !~ /$text/;
Branch: 1 branch
BranchTrueFalseTotal
unless $control_text =~ /$text/ 0 0 0
218 } else {
219 0 ▶ next if $control_text ne $text;
Branch: 1 branch
BranchTrueFalseTotal
if $control_text ne $text 0 0 0
220 }
221 }
222 0 ▶ return $control unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
223 0 push @controls, $control;
224 }
225 0 return @controls;
226 }
227 #-------------------------------------------------------------------------------
228  
229 =head2 wait_control
230  
231 $control = $w->wait_control(%options)
232  
233 waits until the control will be visible and enabled (optionally) and returns a
234 L<Win32::AutoItX::Control> object.
235  
236 Optional arguments:
237  
238 =over
239  
240 =item control
241  
242 the control id
243  
244 =item class
245  
246 a raw string or a C<Regexp> to filter controls by class
247  
248 =item text
249  
250 a raw string or a C<Regexp> to filter controls by text
251  
252 =item enabled
253  
254 if true wait until the control will be enabled
255  
256 =item timeout
257  
258 timeout in seconds to wait the control (60 by default)
259  
260 =back
261  
262 =cut
263  
264 0 ▶sub wait_control {
Subroutine wait_control: not called CC=10 SCAR=47.0
Pod documented
265 0 my $self = shift;
266 0 my %args = (
267 control => undef,
268 text => undef,
269 class => undef,
270 timeout => undef,
271 enabled => 0,
272 @_
273 );
274 0 ▶ $args{timeout} = 60 unless defined $args{timeout};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"timeout"} 0 0 0
275 0 my $control;
276 0 ▶ $control = $self->get_control($args{control}) if defined $args{control};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"control"} 0 0 0
277  
278 0 my $start = time;
279 0 ▶ while (time - $start < $args{timeout}) {
Branch: 1 branch
BranchTrueFalseTotal
if (time - $start < $args{"timeout"}) 0 0 0
280 0 ▶ if (defined $control) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $control) { } 0 0 0
281 return $control if $control->is_visible
282 0 ▶ and (not $args{enabled} or $control->is_enabled);
Branch: 1 branch
BranchTrueFalseTotal
if $control->is_visible and !$args{"enabled"} || $control->is_enabled 0 0 0
Condition0%0/6
$control->is_visible and !$args{"enabled"} || $control->is_enabled
!ll&&!rl&&r
000
not $args{"enabled"} or $control->is_enabled
l!l&&r!l&&!r
000
MC/DC0%0/3
$control->is_visible and !$args{"enabled"} || $control->is_enabled
$control->is_visible not $args{"enabled"} $control->is_enabled
Truth table0%0/4
A and not(B) or C
A= $control->is_visibleB= not $args{"enabled"}C= $control->is_enabled
ABCresult
0XX0
1000
11X1
1011
283 } else {
284 0 ▶ if (my $c = $self->find_controls($args{text},
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = $self->find_controls($args{"text"}, "class", $args{"class"}, "visible", 1, "enabled", $args{"enabled"})) 0 0 0
285 class => $args{class},
286 visible => 1,
287 enabled => $args{enabled},
288 )) {
289 0 return $c;
290 }
291 }
292 0 ▶ sleep 1 if time - $start < $args{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if time - $start < $args{"timeout"} 0 0 0
293 }
294 0 croak "Timed out ($args{timeout} seconds)";
295 }
296 #-------------------------------------------------------------------------------
297  
298 =head2 handle
299  
300 $handle = $window->handle()
301  
302 retrieves the internal handle of the window.
303  
304 =head2 process
305  
306 $process = $window->process()
307  
308 retrieves the Process ID (PID) associated with the window.
309  
310 =head2 state
311  
312 $state = $window->state()
313  
314 returns a value indicating the state of the window. Multiple values are added
315 together so use biwise Bitwise And (C<&>) to examine the part you are interested
316 in:
317  
318 1 = Window exists
319 2 = Window is visible
320 4 = Windows is enabled
321 8 = Window is active
322 16 = Window is minimized
323 32 = Windows is maximized
324  
325 For example:
326  
327 print "Window is visible and enabled." if $window->state() & 6;
328  
329 =head2 exists
330  
331 $boolean = $window->exists()
332  
333 checks to see if the specified window exists.
334  
335 =head2 is_active
336  
337 $boolean = $window->is_active()
338  
339 checks to see if the window exists and is currently active.
340  
341 =head2 text
342  
343 $text = $window->text()
344  
345 retrieves the text from the window. Up to 64KB of window text can be retrieved.
346 It works on minimized windows, but only works on hidden windows if you've set
347
348 $window->AutoItSetOption(WinDetectHiddenText => 1)
349  
350 =head2 title
351  
352 $title = $window->title()
353  
354 retrieves the full title from the window.
355  
356 =head2 x
357  
358 $x = $window->x()
359  
360 retrieves the X coordinate of the window.
361  
362 =head2 y
363  
364 $y = $window->y()
365  
366 retrieves the Y coordinate of the window.
367  
368 =head2 width
369  
370 $width = $window->width()
371  
372 retrieves the width of the window.
373  
374 =head2 height
375  
376 $height = $window->height()
377  
378 retrieves the height of the window.
379  
380 =head2 client_width
381  
382 $client_width = $window->client_width()
383  
384 retrieves the width of the window's client area.
385  
386 =head2 client_height
387  
388 $client_height = $window->client_height()
389  
390 retrieves the height of the window's client area.
391  
392 =head2 activate
393  
394 $window->activate()
395  
396 activates (gives focus to) the window.
397  
398 =head2 close
399  
400 $window->close()
401  
402 closes the window.
403  
404 =head2 kill
405
406 $window->kill()
407  
408 forces the window to close.
409  
410 =head2 select_menu_item
411  
412 $window->select_menu_item($item1, $item2, ..., $item7)
413  
414 invokes a menu item of the window. You should note that underlined menu items
415 actually contain a & character to indicate the underlining. You can access menu
416 items up to six levels deep; and the window can be inactive, minimized, and/or
417 even hidden. It will only work on standard menus. Unfortunately, many menus in
418 use today are actually custom written or toolbars "pretending" to be menus.
419 This is true for most Microsoft applications.
420  
421 =head2 move
422  
423 $window->move($x, $y)
424 $window->move($x, $y, $width, $height)
425  
426 moves and/or resizes the window. It has no effect on minimized windows, but it
427 works on hidden windows. If very width and height are small (or negative), the
428 window will go no smaller than 112 x 27 pixels. If width and height are large,
429 the window will go no larger than approximately C<12+DesktopWidth> x
430 C<12+DesktopHeight> pixels. Negative values are allowed for the x and y
431 coordinates. In fact, you can move a window off screen; and if the window's
432 program is one that remembers its last window position, the window will appear
433 in the corner (but fully on-screen) the next time you launch the program.
434  
435 =head2 set_on_top
436  
437 $window->set_on_top($flag)
438  
439 changes the window's "Always On Top" attribute. C<$flag> determines whether the
440 window should have the "TOPMOST" flag set: 1=set on top flag, 0 = remove on top
441 flag.
442  
443 =head2 hide
444  
445 $window->hide()
446  
447 hides the window.
448  
449 =cut
450  
451 0 ▶sub hide { $_[0]->set_state($_[0]->SW_HIDE) }
Subroutine hide: not called CC=1 SCAR=6.9
Pod documented
452  
453 =head2 show
454  
455 $window->show()
456  
457 shows the previously hidden window.
458  
459 =cut
460  
461 0 ▶sub show { $_[0]->set_state($_[0]->SW_SHOW) }
Subroutine show: not called CC=1 SCAR=6.9
Pod documented
462  
463 =head2 minimize
464  
465 $window->minimize()
466  
467 minimizes the window.
468  
469 =cut
470  
471 0 ▶sub minimize { $_[0]->set_state($_[0]->SW_MINIMIZE) }
Subroutine minimize: not called CC=1 SCAR=6.9
Pod documented
472  
473 =head2 maximize
474  
475 $window->maximize()
476  
477 maximizes the window.
478  
479 =cut
480  
481 0 ▶sub maximize { $_[0]->set_state($_[0]->SW_MAXIMIZE) }
Subroutine maximize: not called CC=1 SCAR=6.9
Pod documented
482  
483 =head2 restore
484  
485 $window->restore()
486  
487 undoes the window minimization or maximization.
488  
489 =cut
490  
491 0 ▶sub restore { $_[0]->set_state($_[0]->SW_RESTORE) }
Subroutine restore: not called CC=1 SCAR=6.9
Pod documented
492  
493 =head2 set_title
494  
495 $window->set_title($title)
496  
497 changes the title of the window.
498  
499 =head2 set_transparency
500  
501 $window->set_transparency($transparency)
502  
503 sets the transparency of the window: a number in the range 0 - 255. The larger
504 the number, the more transparent the window will become.
505  
506 =head2 wait
507  
508 $window->wait()
509 $window->wait($timeout)
510  
511 pauses execution until the window exists. C<$timeout> in seconds.
512  
513 =head2 wait_active
514  
515 $window->wait_active()
516 $window->wait_active($timeout)
517  
518 pauses execution until the window is active. C<$timeout> in seconds.
519  
520 =head2 wait_not_active
521  
522 $window->wait_not_active()
523 $window->wait_not_active($timeout)
524  
525 pauses execution until the window is not active. C<$timeout> in seconds.
526  
527 =head2 wait_close
528  
529 $window->wait_close()
530 $window->wait_close($timeout)
531  
532 pauses execution until the window does not exist. C<$timeout> in seconds.
533  
534 =head2 AutoItX native methods
535  
536 This module also autoloads all AutoItX methods. For example:
537  
538 $window->WinActivate($win_title) unless $window->WinActive($win_title);
539  
540 Please see AutoItX Help file for documenation of all available methods.
541  
542 =cut
543  
544 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=3 SCAR=24.8
545 0 my $self = shift;
546 0 my $method = our $AUTOLOAD;
547 0 $method =~ s/.*:://;
548  
549 0 my @params = @_;
550 0 ▶ if (exists $Helper_Methods{$method}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $Helper_Methods{$method}) 0 0 0
551 0 $method = $Helper_Methods{$method};
552 0 unshift @params, $self->{title}, $self->{text};
553 }
554 print "Call AutoItX method $method with params: @params\n"
555 0 ▶ if $self->{autoitx}->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"autoitx"}->debug 0 0 0
556 0 $self->{autoitx}->{autoit}->$method(@params);
557 }
558 #-------------------------------------------------------------------------------
559  
560 =head1 SEE ALSO
561  
562 =over
563  
564 =item L<Win32::AutoItX::Control>
565  
566 =item L<Win32::AutoItX>
567  
568 =item AutoItX Help
569  
570 =back
571  
572 =head1 AUTHOR
573  
574 Mikhail Telnov E<lt>Mikhail.Telnov@gmail.comE<gt>
575  
576 =head1 COPYRIGHT
577  
578 This software is copyright (c) 2017 by Mikhail Telnov.
579  
580 This library is free software; you may redistribute and/or modify it
581 under the same terms as Perl itself.
582  
583 =cut
584  
585 1 1;
↑ Summary AutoItX.pm »