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
|
"""
LDTP v2 Core Text.
@author: Eitan Isaacson <eitan@ascender.com>
@author: Nagappan Alagappan <nagappan@gmail.com>
@copyright: Copyright (c) 2009 Eitan Isaacson
@copyright: Copyright (c) 2009-13 Nagappan Alagappan
@license: LGPL
http://ldtp.freedesktop.org
This file may be distributed and/or modified under the terms of the GNU Lesser General
Public License version 2 as published by the Free Software Foundation. This file
is distributed without any warranty; without even the implied warranty of
merchantability or fitness for a particular purpose.
See 'COPYING' in the source distribution for more information.
Headers in this file shall remain intact.
"""
import re
import pyatspi
from utils import Utils
from fnmatch import translate as glob_trans
from server_exception import LdtpServerException
from keypress_actions import KeyComboAction, KeyPressAction, KeyReleaseAction
class Text(Utils):
def generatekeyevent(self, data):
"""
Functionality of generatekeyevent is similar to typekey of
LTFX project.
@param data: data to type.
@type data: string
@return: 1 on success.
@rtype: integer
"""
key_combo_action=KeyComboAction(data)
key_combo_action()
return 1
def keypress(self, data):
"""
Press key. NOTE: keyrelease should be called
@param data: data to type.
@type data: string
@return: 1 on success.
@rtype: integer
"""
key_press_action=KeyPressAction(key_name=data)
key_press_action()
return 1
def keyrelease(self, data):
"""
Release key. NOTE: keypress should be called before this
@param data: data to type.
@type data: string
@return: 1 on success.
@rtype: integer
"""
key_release_action=KeyReleaseAction(key_name=data)
key_release_action()
return 1
def enterstring(self, window_name, object_name='', data=''):
"""
Type string sequence.
@param window_name: Window name to focus on, either full name,
LDTP's name convention, or a Unix glob.
@type window_name: string
@param object_name: Object name to focus on, either full name,
LDTP's name convention, or a Unix glob.
@type object_name: string
@param data: data to type.
@type data: string
@return: 1 on success.
@rtype: integer
"""
if object_name:
obj=self._get_object(window_name, object_name,
obj_type=['combo_box', 'text', 'entry',
'paragraph', 'password_text', 'editbar'])
self._grab_focus(obj)
if data:
for gui in self._list_guis():
if self._match_name_to_acc(window_name, gui):
self._grab_focus(gui)
text=data
else:
text=window_name # TODO: Major hack, this is a bad API choice
key_combo_action=KeyComboAction(text)
key_combo_action()
return 1
def settextvalue(self, window_name, object_name, data):
"""
Type string sequence.
@param window_name: Window name to type in, either full name,
LDTP's name convention, or a Unix glob.
@type window_name: string
@param object_name: Object name to type in, either full name,
LDTP's name convention, or a Unix glob.
@type object_name: string
@param data: data to type.
@type data: string
@return: 1 on success.
@rtype: integer
"""
obj=self._get_object(window_name, object_name,
obj_type=['combo_box', 'text', 'entry',
'paragraph', 'password_text', 'editbar'])
self._grab_focus(obj)
if obj.getRole() == pyatspi.ROLE_COMBO_BOX:
obj=self._get_child_object_type(obj, pyatspi.ROLE_TEXT)
if not obj:
raise LdtpServerException('Unable to get combo box children')
try:
texti=obj.queryEditableText()
except NotImplementedError:
raise LdtpServerException('Text cannot be entered into object.')
return int(texti.setTextContents(data.encode('utf-8')))
def gettextvalue(self, window_name, object_name, startPosition=None,
endPosition=None):
"""
Get text value
@param window_name: Window name to type in, either full name,
LDTP's name convention, or a Unix glob.
@type window_name: string
@param object_name: Object name to type in, either full name,
LDTP's name convention, or a Unix glob.
@type object_name: string
@param startPosition: Starting position of text to fetch
@type: startPosition: int
@param endPosition: Ending position of text to fetch
@type: endPosition: int
@return: text on success.
@rtype: string
"""
obj=self._get_object(window_name, object_name,
obj_type=['combo_box', 'text', 'entry', 'label',
'paragraph', 'password_text', 'editbar'])
if obj.getRole() == pyatspi.ROLE_COMBO_BOX:
child_obj=self._get_combo_child_object_type(obj)
if child_obj.getRole() == pyatspi.ROLE_LIST:
obj=self._get_child_object_type(obj, pyatspi.ROLE_TEXT)
if not obj:
raise LdtpServerException('Unable to get text object')
elif child_obj.getRole() == pyatspi.ROLE_MENU:
return obj.name
else:
raise LdtpServerException('Unable to get combo box child object')
try:
texti=obj.queryText()
except NotImplementedError:
raise LdtpServerException('Text cannot be retrieved from object %s.' % obj)
if startPosition and startPosition > 0:
start=startPosition
else:
start=0
if endPosition and endPosition > start:
end=endPosition
else:
end=texti.characterCount
text=texti.getText(start, end)
try:
return unicode(text)
except UnicodeDecodeError:
return text
def inserttext(self, window_name, object_name, position, data):
"""
Insert string sequence in given position.
@param window_name: Window name to type in, either full name,
LDTP's name convention, or a Unix glob.
@type window_name: string
@param object_name: Object name to type in, either full name,
LDTP's name convention, or a Unix glob.
@type object_name: string
@param position: position where text has to be entered.
@type data: int
@param data: data to type.
@type data: string
@return: 1 on success.
@rtype: integer
"""
obj=self._get_object(window_name, object_name,
obj_type=['combo_box', 'text', 'entry',
'paragraph', 'password_text', 'editbar'])
self._grab_focus(obj)
if obj.getRole() == pyatspi.ROLE_COMBO_BOX:
obj=self._get_child_object_type(obj, pyatspi.ROLE_TEXT)
if not obj:
raise LdtpServerException('Unable to get combo box children')
try:
texti=obj.queryEditableText()
except NotImplementedError:
raise LdtpServerException('Text cannot be entered into object.')
return int(texti.insertText(position, data.encode('utf-8'), len(data)))
def verifypartialmatch(self, window_name, object_name, partial_text):
"""
Verify partial text
@param window_name: Window name to type in, either full name,
LDTP's name convention, or a Unix glob.
@type window_name: string
@param object_name: Object name to type in, either full name,
LDTP's name convention, or a Unix glob.
@type object_name: string
@param partial_text: Partial text to match
@type object_name: string
@return: 1 on success.
@rtype: integer
"""
try:
if re.search(partial_text,
self.gettextvalue(window_name,
object_name)):
return 1
except:
pass
return 0
def verifysettext(self, window_name, object_name, text):
"""
Verify text is set correctly
@param window_name: Window name to type in, either full name,
LDTP's name convention, or a Unix glob.
@type window_name: string
@param object_name: Object name to type in, either full name,
LDTP's name convention, or a Unix glob.
@type object_name: string
@param text: text to match
@type object_name: string
@return: 1 on success.
@rtype: integer
"""
try:
return int(self._glob_match(text,
self.gettextvalue(window_name,
object_name)))
except:
return 0
def activatetext(self, window_name, object_name):
"""
Activate text
@param window_name: Window name to type in, either full name,
LDTP's name convention, or a Unix glob.
@type window_name: string
@param object_name: Object name to type in, either full name,
LDTP's name convention, or a Unix glob.
@type object_name: string
@return: 1 on success.
@rtype: integer
"""
obj=self._get_object(window_name, object_name)
self._grab_focus(obj)
self._click_object(obj, 'activate')
return 1
def appendtext(self, window_name, object_name, data):
"""
Append string sequence.
@param window_name: Window name to type in, either full name,
LDTP's name convention, or a Unix glob.
@type window_name: string
@param object_name: Object name to type in, either full name,
LDTP's name convention, or a Unix glob.
@type object_name: string
@param data: data to type.
@type data: string
@return: 1 on success.
@rtype: integer
"""
obj=self._get_object(window_name, object_name,
obj_type=['combo_box', 'text', 'entry',
'paragraph', 'password_text', 'editbar'])
self._grab_focus(obj)
if obj.getRole() == pyatspi.ROLE_COMBO_BOX:
obj=self._get_child_object_type(obj, pyatspi.ROLE_TEXT)
if not obj:
raise LdtpServerException('Unable to get combo box children')
try:
texti=obj.queryEditableText()
except NotImplementedError:
raise LdtpServerException('Text cannot be entered into object.')
texti.setTextContents('%s%s' % (texti.getText(0, texti.characterCount),
data.encode('utf-8')))
return 1
def istextstateenabled(self, window_name, object_name):
"""
Verifies text state enabled or not
@param window_name: Window name to type in, either full name,
LDTP's name convention, or a Unix glob.
@type window_name: string
@param object_name: Object name to type in, either full name,
LDTP's name convention, or a Unix glob.
@type object_name: string
@return: 1 on success 0 on failure.
@rtype: integer
"""
try:
obj=self._get_object(window_name, object_name, False,
obj_type=['combo_box', 'text', 'entry',
'paragraph', 'password_text', 'editbar'])
return int(self._check_state(obj, pyatspi.STATE_EDITABLE))
except:
return 0
def getcharcount(self, window_name, object_name):
"""
Get character count
@param window_name: Window name to type in, either full name,
LDTP's name convention, or a Unix glob.
@type window_name: string
@param object_name: Object name to type in, either full name,
LDTP's name convention, or a Unix glob.
@type object_name: string
@return: 1 on success.
@rtype: integer
"""
obj=self._get_object(window_name, object_name,
obj_type=['combo_box', 'text', 'entry',
'paragraph', 'password_text', 'editbar'])
try:
texti=obj.queryText()
except NotImplementedError:
raise LdtpServerException('Unable to get text.')
return texti.characterCount
def getcursorposition(self, window_name, object_name):
"""
Get cursor position
@param window_name: Window name to type in, either full name,
LDTP's name convention, or a Unix glob.
@type window_name: string
@param object_name: Object name to type in, either full name,
LDTP's name convention, or a Unix glob.
@type object_name: string
@return: Cursor position on success.
@rtype: integer
"""
obj=self._get_object(window_name, object_name,
obj_type=['combo_box', 'text', 'entry',
'paragraph', 'password_text', 'editbar'])
try:
texti=obj.queryText()
except NotImplementedError:
raise LdtpServerException('Unable to get text.')
return texti.caretOffset
def setcursorposition(self, window_name, object_name, cursor_position):
"""
Set cursor position
@param window_name: Window name to type in, either full name,
LDTP's name convention, or a Unix glob.
@type window_name: string
@param object_name: Object name to type in, either full name,
LDTP's name convention, or a Unix glob.
@type object_name: string
@param cursor_position: Cursor position to be set
@type object_name: string
@return: 1 on success.
@rtype: integer
"""
obj=self._get_object(window_name, object_name,
obj_type=['combo_box', 'text', 'entry',
'paragraph', 'password_text', 'editbar'])
self._grab_focus(obj)
try:
texti=obj.queryText()
except NotImplementedError:
raise LdtpServerException('Unable to get text.')
texti.setCaretOffset(cursor_position)
return 1
def cuttext(self, window_name, object_name, start_position, end_position=-1):
"""
cut text from start position to end position
@param window_name: Window name to type in, either full name,
LDTP's name convention, or a Unix glob.
@type window_name: string
@param object_name: Object name to type in, either full name,
LDTP's name convention, or a Unix glob.
@type object_name: string
@param start_position: Start position
@type object_name: integer
@param end_position: End position, default -1
Cut all the text from start position till end
@type object_name: integer
@return: 1 on success.
@rtype: integer
"""
obj=self._get_object(window_name, object_name,
obj_type=['combo_box', 'text', 'entry',
'paragraph', 'password_text', 'editbar'])
self._grab_focus(obj)
try:
texti=obj.queryEditableText()
except NotImplementedError:
raise LdtpServerException('Unable to get editable text.')
size=texti.characterCount
if end_position == -1 or end_position > size:
end_position=size
if start_position < 0:
start_position=0
texti.cutText(start_position, end_position)
return 1
def copytext(self, window_name, object_name, start_position, end_position=-1):
"""
copy text from start position to end position
@param window_name: Window name to type in, either full name,
LDTP's name convention, or a Unix glob.
@type window_name: string
@param object_name: Object name to type in, either full name,
LDTP's name convention, or a Unix glob.
@type object_name: string
@param start_position: Start position
@type object_name: integer
@param end_position: End position, default -1
Copy all the text from start position till end
@type object_name: integer
@return: 1 on success.
@rtype: integer
"""
obj=self._get_object(window_name, object_name,
obj_type=['combo_box', 'text', 'entry',
'paragraph', 'password_text', 'editbar'])
self._grab_focus(obj)
try:
texti=obj.queryEditableText()
except NotImplementedError:
raise LdtpServerException('Unable to get editable text.')
size=texti.characterCount
if end_position == -1 or end_position > size:
end_position=size
if start_position < 0:
start_position=0
texti.copyText(start_position, end_position)
return 1
def deletetext(self, window_name, object_name, start_position, end_position=-1):
"""
delete text from start position to end position
@param window_name: Window name to type in, either full name,
LDTP's name convention, or a Unix glob.
@type window_name: string
@param object_name: Object name to type in, either full name,
LDTP's name convention, or a Unix glob.
@type object_name: string
@param start_position: Start position
@type object_name: integer
@param end_position: End position, default -1
Delete all the text from start position till end
@type object_name: integer
@return: 1 on success.
@rtype: integer
"""
obj=self._get_object(window_name, object_name,
obj_type=['combo_box', 'text', 'entry',
'paragraph', 'password_text', 'editbar'])
self._grab_focus(obj)
try:
texti=obj.queryEditableText()
except NotImplementedError:
raise LdtpServerException('Unable to get editable text.')
size=texti.characterCount
if end_position == -1 or end_position > size:
end_position=size
if start_position < 0:
start_position=0
texti.deleteText(start_position, end_position)
return 1
def pastetext(self, window_name, object_name, position=0):
"""
paste text from start position to end position
@param window_name: Window name to type in, either full name,
LDTP's name convention, or a Unix glob.
@type window_name: string
@param object_name: Object name to type in, either full name,
LDTP's name convention, or a Unix glob.
@type object_name: string
@param position: Position to paste the text, default 0
@type object_name: integer
@return: 1 on success.
@rtype: integer
"""
obj=self._get_object(window_name, object_name,
obj_type=['combo_box', 'text', 'entry',
'paragraph', 'password_text', 'editbar'])
self._grab_focus(obj)
try:
texti=obj.queryEditableText()
except NotImplementedError:
raise LdtpServerException('Unable to get editable text.')
size=texti.characterCount
if position > size:
position=size
if position < 0:
position=0
texti.pasteText(position)
return 1
def getstatusbartext(self, window_name, object_name):
"""
Get status bar text
@param window_name: Window name to type in, either full name,
LDTP's name convention, or a Unix glob.
@type window_name: string
@param object_name: Object name to type in, either full name,
LDTP's name convention, or a Unix glob.
@type object_name: string
@return: text on success.
@rtype: string
"""
return self.gettextvalue(window_name, object_name)
|