/
application.py
1458 lines (1172 loc) · 56.7 KB
/
application.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# GUI Application automation and testing library
# Copyright (C) 2006-2018 Mark Mc Mahon and Contributors
# https://github.com/pywinauto/pywinauto/graphs/contributors
# http://pywinauto.readthedocs.io/en/latest/credits.html
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of pywinauto nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""The application module is the main one that users will use first.
When starting to automate an application you must initialize an instance
of the Application class. Then you must :func:`Application.start` that
application or :func:`Application.connect()` to a running instance of that
application.
Once you have an Application instance you can access dialogs in that
application either by using one of the methods below. ::
dlg = app.YourDialogTitle
dlg = app.child_window(title="your title", classname="your class", ...)
dlg = app['Your Dialog Title']
Similarly once you have a dialog you can get a control from that dialog
in almost exactly the same ways. ::
ctrl = dlg.YourControlTitle
ctrl = dlg.child_window(title="Your control", classname="Button", ...)
ctrl = dlg["Your control"]
.. note::
For attribute access of controls and dialogs you do not have to
have the title of the control exactly, it does a best match of the
available dialogs or controls.
.. seealso::
:func:`pywinauto.findwindows.find_elements` for the keyword arguments that
can be passed to both: :func:`Application.window` and
:func:`WindowSpecification.child_window`
"""
from __future__ import print_function
import sys
import os
import pickle
import time
import warnings
import multiprocessing
import locale
import codecs
import win32process
import win32api
import win32gui
import win32con
import win32event
import six
from . import timings
from . import controls
from . import findbestmatch
from . import findwindows
from . import handleprops
from . import win32defines
from .backend import registry
from .actionlogger import ActionLogger
from .timings import Timings, wait_until, TimeoutError, wait_until_passes
from .sysinfo import is_x64_Python
from . import deprecated
class AppStartError(Exception):
"""There was a problem starting the Application"""
pass # pragma: no cover
class ProcessNotFoundError(Exception):
"""Could not find that process"""
pass # pragma: no cover
class AppNotConnected(Exception):
"""Application has not been connected to a process yet"""
pass # pragma: no cover
# Display User and Deprecation warnings every time
for warning in (UserWarning, PendingDeprecationWarning):
warnings.simplefilter('always', warning)
#=========================================================================
class WindowSpecification(object):
"""
A specification for finding a window or control
Windows are resolved when used.
You can also wait for existance or non existance of a window
.. implicitly document some private functions
.. automethod:: __getattribute__
.. automethod:: __getitem__
"""
WAIT_CRITERIA_MAP = {'exists': ('exists',),
'visible': ('is_visible',),
'enabled': ('is_enabled',),
'ready': ('is_visible', 'is_enabled',),
'active': ('is_active',),
}
def __init__(self, search_criteria, allow_magic_lookup=True):
"""
Initialize the class
:param search_criteria: the criteria to match a dialog
:param allow_magic_lookup: whether attribute access must turn into child_window(best_match=...) search as fallback
"""
# kwargs will contain however to find this window
if 'backend' not in search_criteria:
search_criteria['backend'] = registry.active_backend.name
if 'process' in search_criteria and 'app' in search_criteria:
raise KeyError('Keywords "process" and "app" cannot be combined (ambiguous). ' \
'Use one option at a time: Application object with keyword "app" or ' \
'integer process ID with keyword "process".')
self.app = search_criteria.get('app', None)
self.criteria = [search_criteria, ]
self.actions = ActionLogger()
self.backend = registry.backends[search_criteria['backend']]
self.allow_magic_lookup = allow_magic_lookup
if self.backend.name == 'win32':
# Non PEP-8 aliases for partial backward compatibility
self.WrapperObject = deprecated(self.wrapper_object)
self.ChildWindow = deprecated(self.child_window)
self.Exists = deprecated(self.exists)
self.Wait = deprecated(self.wait)
self.WaitNot = deprecated(self.wait_not)
self.PrintControlIdentifiers = deprecated(self.print_control_identifiers)
self.Window = deprecated(self.child_window, deprecated_name='Window')
self.Window_ = deprecated(self.child_window, deprecated_name='Window_')
self.window_ = deprecated(self.child_window, deprecated_name='window_')
def __call__(self, *args, **kwargs):
"""No __call__ so return a useful error"""
if "best_match" in self.criteria[-1]:
raise AttributeError("Neither GUI element (wrapper) " \
"nor wrapper method '{0}' were found (typo?)".
format(self.criteria[-1]['best_match']))
message = (
"You tried to execute a function call on a WindowSpecification "
"instance. You probably have a typo for one of the methods of "
"this class or of the targeted wrapper object.\n"
"The criteria leading up to this are: " + str(self.criteria))
raise AttributeError(message)
def __get_ctrl(self, criteria_):
"""Get a control based on the various criteria"""
# make a copy of the criteria
criteria = [crit.copy() for crit in criteria_]
# find the dialog
if 'backend' not in criteria[0]:
criteria[0]['backend'] = self.backend.name
if self.app is not None:
# find_elements(...) accepts only "process" argument
criteria[0]['process'] = self.app.process
del criteria[0]['app']
dialog = self.backend.generic_wrapper_class(findwindows.find_element(**criteria[0]))
ctrls = []
# if there is only criteria for a dialog then return it
if len(criteria) > 1:
# so there was criteria for a control, add the extra criteria
# that are required for child controls
previous_parent = dialog.element_info
for ctrl_criteria in criteria[1:]:
ctrl_criteria["top_level_only"] = False
if "parent" not in ctrl_criteria:
ctrl_criteria["parent"] = previous_parent
if isinstance(ctrl_criteria["parent"], WindowSpecification):
ctrl_criteria["parent"] = ctrl_criteria["parent"].wrapper_object()
# resolve the control and return it
if 'backend' not in ctrl_criteria:
ctrl_criteria['backend'] = self.backend.name
ctrl = self.backend.generic_wrapper_class(findwindows.find_element(**ctrl_criteria))
previous_parent = ctrl.element_info
ctrls.append(ctrl)
if ctrls:
return (dialog, ) + tuple(ctrls)
else:
return (dialog, )
def __resolve_control(self, criteria, timeout=None, retry_interval=None):
"""
Find a control using criteria
* **criteria** - a list that contains 1 or 2 dictionaries
1st element is search criteria for the dialog
2nd element is search criteria for a control of the dialog
* **timeout** - maximum length of time to try to find the controls (default 5)
* **retry_interval** - how long to wait between each retry (default .2)
"""
if timeout is None:
timeout = Timings.window_find_timeout
if retry_interval is None:
retry_interval = Timings.window_find_retry
try:
ctrl = wait_until_passes(
timeout,
retry_interval,
self.__get_ctrl,
(findwindows.ElementNotFoundError,
findbestmatch.MatchError,
controls.InvalidWindowHandle,
controls.InvalidElement),
criteria)
except TimeoutError as e:
raise e.original_exception
return ctrl
def wrapper_object(self):
"""Allow the calling code to get the HwndWrapper object"""
ctrls = self.__resolve_control(self.criteria)
return ctrls[-1]
def child_window(self, **criteria):
"""
Add criteria for a control
When this window specification is resolved it will be used
to match against a control.
"""
# default to non top level windows because we are usualy
# looking for a control
if 'top_level_only' not in criteria:
criteria['top_level_only'] = False
new_item = WindowSpecification(self.criteria[0], allow_magic_lookup=self.allow_magic_lookup)
new_item.criteria.extend(self.criteria[1:])
new_item.criteria.append(criteria)
return new_item
def window(self, **criteria):
"""Deprecated alias of child_window()"""
warnings.warn(
"WindowSpecification.Window() WindowSpecification.Window_(), "
"WindowSpecification.window() and WindowSpecification.window_() "
"are deprecated, please switch to WindowSpecification.child_window()",
DeprecationWarning)
return self.child_window(**criteria)
def __getitem__(self, key):
"""
Allow access to dialogs/controls through item access
This allows::
app['DialogTitle']['ControlTextClass']
to be used to access dialogs and controls.
Both this and :func:`__getattribute__` use the rules outlined in the
HowTo document.
"""
# if we already have 2 levels of criteria (dlg, control)
# then resolve the control and do a getitem on it for the
if len(self.criteria) >= 2: # FIXME - this is surprising
ctrls = self.__resolve_control(self.criteria)
# try to return a good error message if the control does not
# have a __getitem__() method)
if hasattr(ctrls[-1], '__getitem__'):
return ctrls[-1][key]
else:
message = "The control does not have a __getitem__ method " \
"for item access (i.e. ctrl[key]) so maybe you have " \
"requested this in error?"
raise AttributeError(message)
# if we get here then we must have only had one criteria so far
# so create a new :class:`WindowSpecification` for this control
new_item = WindowSpecification(self.criteria[0], allow_magic_lookup=self.allow_magic_lookup)
# add our new criteria
new_item.criteria.append({"best_match": key})
return new_item
def __getattribute__(self, attr_name):
"""
Attribute access for this class
If we already have criteria for both dialog and control then
resolve the control and return the requested attribute.
If we have only criteria for the dialog but the attribute
requested is an attribute of DialogWrapper then resolve the
dialog and return the requested attribute.
Otherwise delegate functionality to :func:`__getitem__` - which
sets the appropriate criteria for the control.
"""
allow_magic_lookup = object.__getattribute__(self, "allow_magic_lookup") # Beware of recursions here!
if not allow_magic_lookup:
try:
return object.__getattribute__(self, attr_name)
except AttributeError:
wrapper_object = self.wrapper_object()
try:
return getattr(wrapper_object, attr_name)
except AttributeError:
message = (
'Attribute "%s" exists neither on %s object nor on'
'targeted %s element wrapper (typo? or set allow_magic_lookup to True?)' %
(attr_name, self.__class__, wrapper_object.__class__))
raise AttributeError(message)
if attr_name in ['__dict__', '__members__', '__methods__', '__class__', '__name__']:
return object.__getattribute__(self, attr_name)
if attr_name in dir(self.__class__):
return object.__getattribute__(self, attr_name)
if attr_name in self.__dict__:
return self.__dict__[attr_name]
# if we already have 2 levels of criteria (dlg, control)
# this third must be an attribute so resolve and get the
# attribute and return it
if len(self.criteria) >= 2: # FIXME - this is surprising
ctrls = self.__resolve_control(self.criteria)
try:
return getattr(ctrls[-1], attr_name)
except AttributeError:
return self.child_window(best_match=attr_name)
else:
# FIXME - I don't get this part at all, why is it win32-specific and why not keep the same logic as above?
# if we have been asked for an attribute of the dialog
# then resolve the window and return the attribute
desktop_wrapper = self.backend.generic_wrapper_class(self.backend.element_info_class())
need_to_resolve = (len(self.criteria) == 1 and hasattr(desktop_wrapper, attr_name))
if hasattr(self.backend, 'dialog_class'):
need_to_resolve = need_to_resolve and hasattr(self.backend.dialog_class, attr_name)
# Probably there is no DialogWrapper for another backend
if need_to_resolve:
ctrls = self.__resolve_control(self.criteria)
return getattr(ctrls[-1], attr_name)
# It is a dialog/control criterion so let getitem
# deal with it
return self[attr_name]
def exists(self, timeout=None, retry_interval=None):
"""
Check if the window exists, return True if the control exists
:param timeout: the maximum amount of time to wait for the
control to exists. Defaults to ``Timings.exists_timeout``
:param retry_interval: The control is checked for existance this number
of seconds. ``Defaults to Timings.exists_retry``
"""
# set the current timings -couldn't set as defaults as they are
# evaluated at import time - and timings may be changed at any time
if timeout is None:
timeout = Timings.exists_timeout
if retry_interval is None:
retry_interval = Timings.exists_retry
# modify the criteria as exists should look for all
# windows - including not visible and disabled
exists_criteria = self.criteria[:]
for criterion in exists_criteria:
criterion['enabled_only'] = False
criterion['visible_only'] = False
try:
self.__resolve_control(exists_criteria, timeout, retry_interval)
return True
except (findwindows.ElementNotFoundError,
findbestmatch.MatchError,
controls.InvalidWindowHandle,
controls.InvalidElement):
return False
@classmethod
def __parse_wait_args(cls, wait_conditions, timeout, retry_interval):
"""Both methods wait & wait_not have the same args handling"""
# set the current timings -couldn't set as defaults as they are
# evaluated at import time - and timings may be changed at any time
if timeout is None:
timeout = Timings.window_find_timeout
if retry_interval is None:
retry_interval = Timings.window_find_retry
# allow for case mixups - just to make it easier to use
wait_for = wait_conditions.lower()
# get checking methods from the map by wait_conditions string
# To avoid needless checks - use a set to filter duplicates
unique_check_names = set()
wait_criteria_names = wait_for.split()
for criteria_name in wait_criteria_names:
try:
check_methods = cls.WAIT_CRITERIA_MAP[criteria_name]
except KeyError:
# Invalid check name in the wait_for
raise SyntaxError('Unexpected criteria - %s' % criteria_name)
else:
unique_check_names.update(check_methods)
# unique_check_names = set(['is_enabled', 'is_active', 'is_visible', 'Exists'])
return unique_check_names, timeout, retry_interval
def __check_all_conditions(self, check_names, retry_interval):
"""
Checks for all conditions
If any check's result != True return False immediately, do not matter others check results.
True will be returned when all checks passed and all of them equal True.
"""
for check_name in check_names:
# timeout = retry_interval because the timeout is handled at higher level
if check_name == 'exists':
check = getattr(self, check_name)
if not check(retry_interval, float(retry_interval) // 2):
return False
else:
continue
try:
# resolve control explicitly to pass correct timing params
ctrls = self.__resolve_control(self.criteria, retry_interval, float(retry_interval) // 2)
check = getattr(ctrls[-1], check_name)
except (findwindows.ElementNotFoundError,
findbestmatch.MatchError,
controls.InvalidWindowHandle,
controls.InvalidElement):
# The control does not exist
return False
else:
if not check():
# At least one check not passed
return False
# All the checks have been done
return True
def wait(self, wait_for, timeout=None, retry_interval=None):
"""
Wait for the window to be in a particular state/states.
:param wait_for: The state to wait for the window to be in. It can
be any of the following states, also you may combine the states by space key.
* 'exists' means that the window is a valid handle
* 'visible' means that the window is not hidden
* 'enabled' means that the window is not disabled
* 'ready' means that the window is visible and enabled
* 'active' means that the window is active
:param timeout: Raise an :func:`pywinauto.timings.TimeoutError` if the window
is not in the appropriate state after this number of seconds.
Default: :py:attr:`pywinauto.timings.Timings.window_find_timeout`.
:param retry_interval: How long to sleep between each retry.
Default: :py:attr:`pywinauto.timings.Timings.window_find_retry`.
An example to wait until the dialog
exists, is ready, enabled and visible: ::
self.Dlg.wait("exists enabled visible ready")
.. seealso::
:func:`WindowSpecification.wait_not()`
:func:`pywinauto.timings.TimeoutError`
"""
check_method_names, timeout, retry_interval = self.__parse_wait_args(wait_for, timeout, retry_interval)
wait_until(timeout, retry_interval,
lambda: self.__check_all_conditions(check_method_names, retry_interval))
# Return the wrapped control
return self.wrapper_object()
def wait_not(self, wait_for_not, timeout=None, retry_interval=None):
"""
Wait for the window to not be in a particular state/states.
:param wait_for_not: The state to wait for the window to not be in. It can be any
of the following states, also you may combine the states by space key.
* 'exists' means that the window is a valid handle
* 'visible' means that the window is not hidden
* 'enabled' means that the window is not disabled
* 'ready' means that the window is visible and enabled
* 'active' means that the window is active
:param timeout: Raise an :func:`pywinauto.timings.TimeoutError` if the window is sill in the
state after this number of seconds.
Default: :py:attr:`pywinauto.timings.Timings.window_find_timeout`.
:param retry_interval: How long to sleep between each retry.
Default: :py:attr:`pywinauto.timings.Timings.window_find_retry`.
An example to wait until the dialog is not ready, enabled or visible: ::
self.Dlg.wait_not("enabled visible ready")
.. seealso::
:func:`WindowSpecification.wait()`
:func:`pywinauto.timings.TimeoutError`
"""
check_method_names, timeout, retry_interval = \
self.__parse_wait_args(wait_for_not, timeout, retry_interval)
wait_until(timeout, retry_interval,
lambda: not self.__check_all_conditions(check_method_names, retry_interval))
# None return value, since we are waiting for a `negative` state of the control.
# Expect that you will have nothing to do with the window closed, disabled, etc.
def _ctrl_identifiers(self):
ctrls = self.__resolve_control(self.criteria)
if ctrls[-1].is_dialog():
# dialog controls are all the control on the dialog
dialog_controls = ctrls[-1].children()
ctrls_to_print = dialog_controls[:]
# filter out hidden controls
ctrls_to_print = [
ctrl for ctrl in ctrls_to_print if ctrl.is_visible()]
else:
dialog_controls = ctrls[-1].top_level_parent().children()
ctrls_to_print = [ctrls[-1]]
# build the list of disambiguated list of control names
name_control_map = findbestmatch.build_unique_dict(dialog_controls)
# swap it around so that we are mapped off the controls
control_name_map = {}
for name, ctrl in name_control_map.items():
control_name_map.setdefault(ctrl, []).append(name)
return control_name_map
def print_control_identifiers(self, depth=None, filename=None):
"""
Prints the 'identifiers'
Prints identifiers for the control and for its descendants to
a depth of **depth** (the whole subtree if **None**).
.. note:: The identifiers printed by this method have been made
unique. So if you have 2 edit boxes, they won't both have "Edit"
listed in their identifiers. In fact the first one can be
referred to as "Edit", "Edit0", "Edit1" and the 2nd should be
referred to as "Edit2".
"""
if depth is None:
depth = sys.maxsize
# Wrap this control
this_ctrl = self.__resolve_control(self.criteria)[-1]
# Create a list of this control and all its descendants
all_ctrls = [this_ctrl, ] + this_ctrl.descendants()
# Create a list of all visible text controls
txt_ctrls = [ctrl for ctrl in all_ctrls if ctrl.can_be_label and ctrl.is_visible() and ctrl.window_text()]
# Build a dictionary of disambiguated list of control names
name_ctrl_id_map = findbestmatch.UniqueDict()
for index, ctrl in enumerate(all_ctrls):
ctrl_names = findbestmatch.get_control_names(ctrl, all_ctrls, txt_ctrls)
for name in ctrl_names:
name_ctrl_id_map[name] = index
# Swap it around so that we are mapped off the control indices
ctrl_id_name_map = {}
for name, index in name_ctrl_id_map.items():
ctrl_id_name_map.setdefault(index, []).append(name)
def print_identifiers(ctrls, current_depth=1, log_func=print):
"""Recursively print ids for ctrls and their descendants in a tree-like format"""
if len(ctrls) == 0 or current_depth > depth:
return
indent = (current_depth - 1) * u" | "
for ctrl in ctrls:
try:
ctrl_id = all_ctrls.index(ctrl)
except ValueError:
continue
ctrl_text = ctrl.window_text()
if ctrl_text:
# transform multi-line text to one liner
ctrl_text = ctrl_text.replace('\n', r'\n').replace('\r', r'\r')
output = indent + u'\n'
output += indent + u"{class_name} - '{text}' {rect}\n"\
"".format(class_name=ctrl.friendly_class_name(),
text=ctrl_text,
rect=ctrl.rectangle())
output += indent + u'{}'.format(ctrl_id_name_map[ctrl_id])
title = ctrl_text
class_name = ctrl.class_name()
auto_id = None
control_type = None
if hasattr(ctrl.element_info, 'automation_id'):
auto_id = ctrl.element_info.automation_id
if hasattr(ctrl.element_info, 'control_type'):
control_type = ctrl.element_info.control_type
if control_type:
class_name = None # no need for class_name if control_type exists
else:
control_type = None # if control_type is empty, still use class_name instead
criteria_texts = []
if title:
criteria_texts.append(u'title="{}"'.format(title))
if class_name:
criteria_texts.append(u'class_name="{}"'.format(class_name))
if auto_id:
criteria_texts.append(u'auto_id="{}"'.format(auto_id))
if control_type:
criteria_texts.append(u'control_type="{}"'.format(control_type))
if title or class_name or auto_id:
output += u'\n' + indent + u'child_window(' + u', '.join(criteria_texts) + u')'
if six.PY3:
log_func(output)
else:
log_func(output.encode(locale.getpreferredencoding(), errors='backslashreplace'))
print_identifiers(ctrl.children(), current_depth + 1, log_func)
if filename is None:
print("Control Identifiers:")
print_identifiers([this_ctrl, ])
else:
log_file = codecs.open(filename, "w", locale.getpreferredencoding())
def log_func(msg):
log_file.write(str(msg) + os.linesep)
log_func("Control Identifiers:")
print_identifiers([this_ctrl, ], log_func=log_func)
log_file.close()
print_ctrl_ids = print_control_identifiers
dump_tree = print_control_identifiers
cur_item = 0
def _resolve_from_appdata(
criteria_, app, timeout=None, retry_interval=None):
"""Should not be used at the moment!"""
# TODO: take a look into this functionality
if timeout is None:
timeout = Timings.window_find_timeout
if retry_interval is None:
retry_interval = Timings.window_find_retry
global cur_item
# get the stored item corresponding to this request
matched_control = app.GetMatchHistoryItem(cur_item)
cur_item += 1
# remove parameters from the original search that changes each time
criteria = [crit.copy() for crit in criteria_]
# Remove any attributes from the current search that are
# completely language dependent
for unloc_attrib in ['title_re', 'title', 'best_match']:
for c in criteria:
if unloc_attrib in c.keys():
del c[unloc_attrib]
#found_criteria = item[0]
#for c in found_criteria:
# if c.has_key('process'):
# del c['process']
#
# They should match - so if they don't print it out.
#if found_criteria != search_criteria:
# print "\t\t", matched[cur_index - 3][0]
# print "\t" ,matched[cur_index - 2][0]
# print search_criteria
# print "---"
# print found_criteria
# raise RuntimeError("Mismatch")
# so let's use the ID from the matched control...
#print item[1]
# we need to try and get a good match for the dialog
# this could be done by matching
# - number/positoin of controls
# - class
# anything else?
dialog_criterion = criteria[0]
#print list(matched_control)
dialog_criterion['class_name'] = matched_control[1]['class_name']
# find all the windows in the process
process_elems = findwindows.find_elements(**dialog_criterion)
dialog = None
ctrl = None
if process_elems:
#similar_child_count = [e for e in process_elems
# if matched_control[1]['control_count'] -2 <=
# len(e.children()) and
# matched_control[1]['control_count'] +2 >=
# len(e.children())]
#if similar_child_count:
# process_hwnds = similar_child_count
#else:
# print("None Similar child count!!???")
# print(matched_control[1]['control_count'], len(handleprops.children(h)))
for e in process_elems:
#print controls.WrapHandle(h).get_properties()
#print "======", h, h, h
dialog = registry.wrapper_class(e)
# if a control was specified also
if len(criteria_) > 1:
# find it in the original data
#print item[2]
# remove those criteria which are langauge specific
ctrl_criterion = criteria[1]
#def has_same_id(other_ctrl):
# print "==="*20
# print "testing", item[2]['control_id'],
# print "against", other_ctrl
# return item[2]['control_id'] == \
# handleprops.controlid(other_ctrl)
ctrl_criterion['class_name'] = matched_control[2]['class_name']
ctrl_criterion['parent'] = dialog.handle
ctrl_criterion['top_level_only'] = False
#ctrl_criterion['predicate_func'] = has_same_id
#print "CTRLCTRJL", ctrl_criterion
ctrl_elems = findwindows.find_elements(**ctrl_criterion)
if len(ctrl_elems) > 1:
same_ids = \
[elem for elem in ctrl_elems
if elem.control_id == matched_control[2]['control_id']]
if same_ids:
ctrl_elems = same_ids
try:
ctrl = registry.wrapper_class(ctrl_elems[0])
except IndexError:
print("-+-+=_" * 20)
#print(found_criteria)
raise
break
# it is possible that the dialog will not be found - so we
# should raise an error
if dialog is None:
raise findwindows.ElementNotFoundError()
if len(criteria_) == 2 and ctrl is None:
raise findwindows.ElementNotFoundError()
if ctrl:
return dialog, ctrl
else:
return (dialog, )
#print process_hwnds
#
# # if best match was specified for the dialog
# # then we need to replace it with other values
# # for now we will just use class_name
# for crit in ['best_match', 'title', 'title_re']:
# if crit in criteria[0]:
# del(criteria[0][crit])
# criteria[0]['class_name'] = app_data[0].class_name()#['class_name']
#
# if len(criteria) > 1:
# # find the best match of the application data
# if criteria[1].has_key('best_match'):
# best_match = findbestmatch.find_best_control_matches(
# criteria[1]['best_match'], app_data)[0]
#
# #visible_controls = [ctrl in app_data if ctrl.is_visible()]
#
# #find the index of the best match item
# ctrl_index = app_data.index(best_match)
# #print best_match[0].window_text()
# ctrl_index, best_match.window_text()
#
# criteria[1]['ctrl_index'] = ctrl_index -1
# #criteria[1]['class_name'] = best_match.class_name()
# #del(criteria[1]['best_match'])
#
# One idea here would be to run the new criteria on the app_data dialog and
# if it returns more then one control then you figure out which one would be
# best - so that you have that info when running on the current dialog
#
# #for criterion in criteria[1:]:
# # this part is weird - we now have to go off and find the
# # index, class, text of the control in the app_data
# # and then find the best match for this control in the
# # current dialog
# # pass
#
#
# dialog = None
#return _resolve_control(criteria_, timeout, retry_interval)
#=========================================================================
class Application(object):
"""
Represents an application
.. implicitly document some private functions
.. automethod:: __getattribute__
.. automethod:: __getitem__
"""
def __init__(self, backend="win32", datafilename=None, allow_magic_lookup=True):
"""
Initialize the Application object
* **backend** is a name of used back-end (values: "win32", "uia").
* **datafilename** is a file name for reading matching history.
"""
self.process = None
self.xmlpath = ''
self.match_history = []
self.use_history = False
self.actions = ActionLogger()
if backend not in registry.backends:
raise ValueError('Backend "{0}" is not registered!'.format(backend))
self.backend = registry.backends[backend]
self.allow_magic_lookup = allow_magic_lookup
if self.backend.name == 'win32':
# Non PEP-8 aliases for partial backward compatibility
self.Start = deprecated(self.start)
self.Connect = deprecated(self.connect)
self.CPUUsage = deprecated(self.cpu_usage)
self.WaitCPUUsageLower = deprecated(self.wait_cpu_usage_lower, deprecated_name='WaitCPUUsageLower')
self.top_window_ = deprecated(self.top_window, deprecated_name='top_window_')
self.active_ = deprecated(self.active, deprecated_name='active_')
self.Windows_ = deprecated(self.windows, deprecated_name='Windows_')
self.windows_ = deprecated(self.windows, deprecated_name='windows_')
self.Window_ = deprecated(self.window, deprecated_name='Window_')
self.window_ = deprecated(self.window, deprecated_name='window_')
# load the match history if a file was specifed
# and it exists
if datafilename and os.path.exists(datafilename):
with open(datafilename, "rb") as datafile:
self.match_history = pickle.load(datafile)
self.use_history = True
def __iter__(self):
"""Raise to avoid infinite loops"""
raise NotImplementedError("Object is not iterable, try to use .windows()")
def connect(self, **kwargs):
"""Connect to an already running process
The action is performed according to only one of parameters
:param process: a process ID of the target
:param handle: a window handle of the target
:param path: a path used to launch the target
:param timeout: a timeout for process start (relevant if path is specified)
.. seealso::
:func:`pywinauto.findwindows.find_elements` - the keyword arguments that
are also can be used instead of **process**, **handle** or **path**
"""
timeout = Timings.app_connect_timeout
retry_interval = Timings.app_connect_retry
if 'timeout' in kwargs and kwargs['timeout'] is not None:
timeout = kwargs['timeout']
if 'retry_interval' in kwargs and kwargs['retry_interval'] is not None:
retry_interval = kwargs['retry_interval']
connected = False
if 'process' in kwargs:
self.process = kwargs['process']
try:
wait_until(timeout, retry_interval, self.is_process_running, value=True)
except TimeoutError:
raise ProcessNotFoundError('Process with PID={} not found!'.format(self.process))
connected = True
elif 'handle' in kwargs:
if not handleprops.iswindow(kwargs['handle']):
message = "Invalid handle 0x%x passed to connect()" % (
kwargs['handle'])
raise RuntimeError(message)
self.process = handleprops.processid(kwargs['handle'])
connected = True
elif 'path' in kwargs:
try:
self.process = timings.wait_until_passes(
timeout, retry_interval, process_from_module,
ProcessNotFoundError, kwargs['path'],
)
except TimeoutError:
raise ProcessNotFoundError('Process "{}" not found!'.format(kwargs['path']))
connected = True
elif kwargs:
kwargs['backend'] = self.backend.name
if 'visible_only' not in kwargs:
kwargs['visible_only'] = False
if 'timeout' in kwargs:
del kwargs['timeout']
self.process = timings.wait_until_passes(
timeout, retry_interval, findwindows.find_element,
exceptions=(findwindows.ElementNotFoundError, findbestmatch.MatchError,
controls.InvalidWindowHandle, controls.InvalidElement),
*(), **kwargs
).process_id
else:
self.process = findwindows.find_element(**kwargs).process_id
connected = True
if not connected: