-
Notifications
You must be signed in to change notification settings - Fork 31
/
transform.py
1510 lines (1286 loc) · 58 KB
/
transform.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
# coding=utf8
import re
import os
import bs4
import math
import json
import codecs
import argparse
from lxml import etree
from pprint import pprint
from bs4 import BeautifulSoup
# package_name = None
source_width, source_height, source_dpi = None, None, None
target_width, target_height, target_dpi = None, None, None
BOUND_PATTERN = re.compile(r'\[([-+]?\d+),([-+]?\d+)\]\[[-+]?(\d+),([-+]?\d+)\]')
VIEW_ATTR_PATTERN = re.compile(r'^(?P<classname>.*)\{(?P<address>.*)\s(?P<visible>.)(?P<focusable>.)(?P<enabled>.)(?P<draw_mask>.)(?P<scroll_horiz>.)(?P<scroll_verti>.)(?P<clickable>.)(?P<long_clickable>.)((?P<context_clickable>.)\s|\s)(.+)\s(?P<left>-?\d+),(?P<top>-?\d+)\-(?P<right>-?\d+),(?P<bottom>-?\d+)((\s(?P<view_id>#[a-zA-Z0-9]+)\s(?P<resource_id>.+))|(\s*(.*)))\}')
REPLAY_ACTION_PATTERN = re.compile(r'\[ReplayAction\]-(?P<action_count>\d+):\s+\[(?P<action>.*)\]\-(?P<info>.*)')
REPLAY_VIEW_INSTRUMENTATION_PATTERN = re.compile(r'\[ReplayViewInstrumentation\]:\s+(.*)')
REPLAY_TIME_INTERVAL_PATTERN = re.compile(r'\[ReplayTimeInterval\]-(\d+):\s+(.*)')
def replace_package_name(rid, package_name):
if rid is None:
return ''
idx = rid.find(':id/')
if idx == -1:
return rid
prefix = rid[:idx]
if prefix.lower() == 'app':
return package_name + rid[idx:]
return rid
def parse_resolution(resolution):
"""
return: width, height, resolution
"""
resolution = resolution.strip()
splits = resolution.split(',')
return int(splits[0]), int(splits[1]), int(splits[2])
def parse_bounds(bounds):
"""
left, top, right, bottom
"""
match = BOUND_PATTERN.match(bounds)
return int(match.group(1)), int(match.group(2)), int(match.group(3)), int(match.group(4))
def get_bounds(left, top, right, bottom):
return '[%d,%d][%d,%d]' % (left, top, right, bottom)
def parse_view(line):
view_str = line.strip()
match = VIEW_ATTR_PATTERN.match(view_str)
if match is None:
return None
view_attrs = dict()
for attr in ['classname', 'address', 'visible', 'focusable', 'enabled', 'draw_mask', 'scroll_horiz', 'scroll_verti', 'clickable', 'long_clickable', 'context_clickable', 'left', 'top', 'right', 'bottom', 'view_id', 'resource_id']:
if attr in ['visible', 'focusable', 'enabled', 'draw_mask', 'scroll_horiz', 'scroll_verti', 'clickable', 'long_clickable', 'context_clickable']:
value = match.group(attr)
if attr == 'visible':
if value == 'V':
view_attrs[attr] = 'true'
else:
view_attrs[attr] = 'false'
else:
if value == '.':
view_attrs[attr] = 'false'
else:
view_attrs[attr] = 'true'
else:
view_attrs[attr] = match.group(attr)
return view_attrs
def read_ui_xml(xml_file):
with codecs.open(xml_file, 'r', encoding='utf8') as f:
return BeautifulSoup(f.read(), 'lxml')
def lxml_read_ui_xml(xml_file):
return etree.parse(xml_file)
def px2dp(px, dpi):
return (px * 160) / dpi
def dp2px(dp, dpi):
return dp * (dpi / 160)
def transform_coordinate(source_x, source_y):
"""
source_x: pixel
source_y: pixel
"""
source_x_dp, source_y_dp = px2dp(source_x, source_dpi), px2dp(source_y, source_dpi)
target_x, target_y = dp2px(source_x_dp, target_dpi), dp2px(source_y_dp, target_dpi)
return target_x, target_y
def parse_action(log, match):
action_count = int(match.group('action_count'))
action = match.group('action')
info = json.loads(match.group('info'))
action_block = {
'action_count': action_count,
'action': action,
'info': info,
'plid': info['plid'],
'package': info['package']
}
return action_block
def parse_instrumentation(log, match):
info = json.loads(match.group(1))['payload']
if 'action_count' not in info:
return None
return info
def parse_time_interval(log, match):
action_count = int(match.group(1))
info = json.loads(match.group(2))
info['action_count'] = action_count
return info
def parse_trace(trace_file):
actions = list()
instrumentations = list()
time_intervals = list()
with open(trace_file, 'r') as f:
for log in f:
log = log.strip()
match = REPLAY_ACTION_PATTERN.match(log)
if match:
actions.append(parse_action(log, match))
continue
match = REPLAY_VIEW_INSTRUMENTATION_PATTERN.match(log)
if match:
instrument = parse_instrumentation(log, match)
if instrument:
instrumentations.append(instrument)
continue
match = REPLAY_TIME_INTERVAL_PATTERN.match(log)
if match:
time_intervals.append(parse_time_interval(log, match))
continue
# Link instrumentation to actions
for instrumentation in reversed(instrumentations):
action_count = int(instrumentation['action_count'])
for action in actions:
if action['action_count'] == action_count:
if 'instrumentation' not in action:
action['instrumentation'] = list()
action['instrumentation'].append(instrumentation)
# Link time_intervals to actions
for ti in time_intervals:
action_count = ti['action_count']
_candidate = None
_candidate_action_count = -1
for action in actions:
if action['action_count'] == action_count:
action['time_interval'] = ti['interval']
_candidate = None
break
if action['action_count'] < action_count and action['action_count'] > _candidate_action_count:
_candidate = action
_candidate_action_count = action['action_count']
if _candidate is not None:
_candidate['time_interval'] += ti['interval']
pprint(actions)
return actions
def get_view_hierarchy_component_bounds(component):
"""
:param component, BeautifulSoup element
"""
width, height = int(component['right']) - int(component['left']), int(component['bottom']) - int(component['top'])
elements = list(component.parents)
elements.reverse()
elements.append(component)
left, top = 0, 0
for element in elements:
if 'left' in element.attrs and len(element['left']) > 0:
left += int(element['left'])
top += int(element['top'])
return get_bounds(left, top, left + width, top + height)
def find_best_match_candidate(candidates, x, y):
best_match_candidate = None
best_match_x_dis, best_match_y_dis = math.inf, math.inf
for cc in candidates:
left, top, right, bottom = cc['abs_left'], cc['abs_top'], cc['abs_right'], cc['abs_bottom']
if left <= x <= right and top <= y <= bottom:
# Matched
x_dis = min(x - left, right - x)
y_dis = min(y - top, bottom - y)
if x_dis <= best_match_x_dis and y_dis <= best_match_y_dis:
# best match
best_match_candidate = cc
best_match_x_dis = x_dis
best_match_y_dis = y_dis
return best_match_candidate
def check_bounds_overlap(bounds_1, bounds_2):
"""
Calculate the ratio of overlap
"""
left_1, top_1, right_1, bottom_1 = bounds_1[0], bounds_1[1], bounds_1[2], bounds_1[3]
left_2, top_2, right_2, bottom_2 = bounds_2[0], bounds_2[1], bounds_2[2], bounds_2[3]
width_1 = right_1 - left_1
height_1 = bottom_1 - top_1
width_2 = right_2 - left_2
height_2 = bottom_2 - top_2
overlap_width = width_1 + width_2 - (max(left_1 + width_1, left_2 + width_2) - min(left_1, left_2))
overlap_height = height_1 + height_2 - (max(top_1 + height_1, top_2 + height_2) - min(top_1, top_2))
if overlap_height <= 0 or overlap_width <= 0:
return False
overlap_area = overlap_height * overlap_width
bounds_1_area = width_1 * height_1
bounds_2_area = width_2 * height_2
ratio = overlap_area / (bounds_1_area + bounds_2_area - overlap_area)
return ratio
def find_component(ui_xml, bounds, rid):
"""
Find components with bounds and resource-id
"""
print(rid, bounds)
components = ui_xml.findAll(attrs={
'resource-id': rid
})
if len(components) == 0:
# Resource id mismatch, then find with bounds
components = ui_xml.findAll(attrs={
'bounds': bounds
})
def _sort(x):
if 'bounds' not in x.attrs:
return -1
return check_bounds_overlap(parse_bounds(x['bounds']), parse_bounds(bounds))
components.sort(key=_sort, reverse=True)
return components
def find_component_in_view_hierarchy(candidate, view_hierarchy):
components = view_hierarchy.findAll(attrs={
'resource_id': candidate['rid'],
'classname': candidate['classname'],
'view_id': candidate['vid'],
'address': candidate['address'],
'left': candidate['left'],
'right': candidate['right'],
'top': candidate['top'],
'bottom': candidate['bottom']
})
if len(components) == 0:
return None
else:
return components[0]
def uiautomator_dfs(tag, source_x, source_y, matched):
"""
Find components in depth-first manner
"""
bounds = tag['bounds']
v_left, v_top, v_right, v_bottom = parse_bounds(bounds)
is_visible = tag['visible-to-user'] == 'true'
is_enable = tag['enabled'] == 'true'
if is_visible and is_enable and v_left <= source_x <= v_right and v_top <= source_y <= v_bottom:
children_feedback = list()
for child in tag.children:
if isinstance(child, bs4.element.Tag):
children_feedback.append(uiautomator_dfs(child, source_x, source_y, matched))
if list(children_feedback) == 0:
# Leaf node
matched.append(tag)
return True
# Inner node
result = False
for fb in children_feedback:
result |= fb
if result:
# True, i.e., some child match, and add child itself
return True
else:
# No child match, then add tag itself
matched.append(tag)
return True
else:
return False
def xpath_soup(element, lxml_root):
"""
Generate xpath from BeautifulSoup4 element
:param element: BeautifulSoup4 element.
:type element: bs4.element.Tag or bs4.element.NavigableString
:return: xpath as string
:rtype: str
"""
components = []
child = element
xpath = None
for parent in child.parents:
"""
@type parent: bs4.element.Tag
"""
if 'class' in child.attrs:
siblings = parent.find_all(attrs={'text': child['text'], 'resource-id': child['resource-id'], 'package': child['package'], 'content-desc': child['content-desc']}, recursive=False)
if len(siblings) > 0:
components.append(
'%s[@text="%s" and @package="%s" and @resource-id="%s" and @content-desc="%s"]' % (child.name, child['text'].replace('"', '"') if '"' in child['text'] else child['text'], child['package'], child['resource-id'], child['content-desc']) if siblings == [child] else
'%s[@text="%s" and @package="%s" and @resource-id="%s" and @content-desc="%s"][%d]' % (child.name, child['text'].replace('"', '\\"') if '"' in child['text'] else child['text'], child['package'], child['resource-id'], child['content-desc'], 1 + siblings.index(child))
)
# Generalization
xpath = '//%s' % '/'.join(list(reversed(components)))
print(xpath)
matched = lxml_root.xpath(xpath)
if len(matched) == 1:
return xpath
else:
siblings = parent.find_all(child.name, recursive=False)
components.append(
child.name
if siblings == [child] else
'%s[%d]' % (child.name, 1 + siblings.index(child))
)
else:
siblings = parent.find_all(child.name, recursive=False)
components.append(
child.name
if siblings == [child] else
'%s[%d]' % (child.name, 1 + siblings.index(child))
)
child = parent
components.reverse()
xpath = '/%s' % '/'.join(components)
prefix = '/html/body'
assert xpath.startswith(prefix)
return xpath[len(prefix):]
def xpath_view_hierarchy(element, lxml_view_hierarchy):
"""
Generate xpath from BeautifulSoup4 element
:param element: BeautifulSoup4 element.
:type element: bs4.element.Tag or bs4.element.NavigableString
:return: xpath as string
:rtype: str
"""
components = []
child = element
xpath = None
for parent in child.parents:
"""
@type parent: bs4.element.Tag
"""
if 'resource_id' in child.attrs:
siblings = parent.find_all(attrs={'resource_id': child['resource_id']}, recursive=False)
if len(siblings) > 0:
components.append(
'%s[@resource_id="%s"]' % (child.name, child['resource_id']) if siblings == [child] else
'%s[@resource_id="%s"][%d]' % (child.name, child['resource_id'], 1 + siblings.index(child))
)
# Generalization
xpath = '//%s' % '/'.join(list(reversed(components)))
matched = lxml_view_hierarchy.xpath(xpath)
if len(matched) == 1:
return xpath
else:
siblings = parent.find_all(child.name, recursive=False)
components.append(
child.name
if siblings == [child] else
'%s[%d]' % (child.name, 1 + siblings.index(child))
)
else:
siblings = parent.find_all(child.name, recursive=False)
components.append(
child.name
if siblings == [child] else
'%s[%d]' % (child.name, 1 + siblings.index(child))
)
child = parent
components.reverse()
xpath = '/%s' % '/'.join(components)
prefix = '/html/body'
assert xpath.startswith(prefix)
return xpath[len(prefix):]
def get_first_scrollable(element):
"""
Get the first scrollable element
"""
for parent in element.parents:
if 'scrollable' not in parent.attrs:
return None
if parent['scrollable'] == 'true':
return parent
return None
def get_first_scrollable_in_view_hierarchy(element):
for parent in element.parents:
if 'scroll_verti' not in parent.attrs:
continue
if parent['scroll_verti'] == 'true':
return parent
return None
def check_cudes(tag):
if tag.has_attr('text') and tag.has_attr('resource-id') and tag.has_attr('content-desc'):
text = tag['text']
rid = tag['resource-id']
description = tag['content-desc']
if len(text) != 0 or len(rid) != 0 or len(description) != 0:
return True
return False
def generate_signature_from_children(matches, component):
"""
Try to derive signature from the children of component
"""
# Derive cudes from children
target_idx = -1
cudes = list()
for idx, node in enumerate(matches):
sub_components = node.findAll(check_cudes)
_cudes = list()
for sc in sub_components:
_cudes.append({
'text': sc['text'] if 'text' in sc.attrs else 'None',
'content-desc': sc['content-desc'] if 'content-desc' in sc.attrs else 'None',
'resource-id': sc['resource-id'] if 'resource-id' in sc.attrs else 'None',
'class': sc['class'] if 'class' in sc.attrs else 'None',
})
cudes.append(_cudes)
if node is component:
target_idx = idx
# Evaluate the effectiveness of each cude
target_component_cudes = cudes[target_idx]
del cudes[target_idx]
for cude in cudes:
to_be_remove = list()
for tc in target_component_cudes:
for c in cude:
if tc['text'] == c['text'] and tc['class'] == c['class'] and tc['content-desc'] == c['content-desc'] and tc['resource-id'] == c['resource-id']:
to_be_remove.append(tc)
break
for tc in to_be_remove:
target_component_cudes.remove(tc)
if len(target_component_cudes) == 0:
return None
return target_component_cudes[0]
def generate_signature_from_parent(matches, component):
"""
Assume that all these matches in the same level of ui tree
Lowest Common Ancestor
"""
# Find lowest common ancestor
target_idx = -1
parents = list()
for idx, node in enumerate(matches):
parents.append(list(node.parents)[::-1])
if node is component:
target_idx = idx
min_level = min([len(p) for p in parents])
level = -1
for level_idx in range(min_level):
for p_idx in range(1, len(parents)):
if parents[0][level_idx] is not parents[p_idx][level_idx]:
level = level_idx
break
if level != -1:
break
else:
# Not Found
print('Lowest Common ancestor not found')
return None
# Enumerate cudes for finding component
tags = [p[level] for p in parents]
cudes = list()
for tag in tags:
sub_components = tag.findAll(check_cudes)
_cudes = list()
for sc in sub_components:
_cudes.append({
'text': sc['text'] if 'text' in sc.attrs else 'None',
'content-desc': sc['content-desc'] if 'content-desc' in sc.attrs else 'None',
'resource-id': sc['resource-id'] if 'resource-id' in sc.attrs else 'None',
'class': sc['class'] if 'class' in sc.attrs else 'None',
})
cudes.append(_cudes)
# Evaluate the effectiveness of each cude
target_component_cudes = cudes[target_idx]
del cudes[target_idx]
for cude in cudes:
to_be_remove = list()
for tc in target_component_cudes:
for c in cude:
if tc['text'] == c['text'] and tc['class'] == c['class'] and tc['content-desc'] == c['content-desc'] and tc['resource-id'] == c['resource-id']:
to_be_remove.append(tc)
break
for tc in to_be_remove:
target_component_cudes.remove(tc)
if len(target_component_cudes) == 0:
return None
return target_component_cudes[0]
def generate_signature(component, scrollable_view):
own_signature = {
'class': component['class'],
'resource-id': component['resource-id'],
'text': component['text'],
'content-desc': component['content-desc']
}
matches = scrollable_view.findAll(attrs=own_signature)
assert len(matches) > 0
if len(matches) == 1:
print('Only one match! Yeah ~~')
return ('Own', own_signature)
# Multiple matches
valid_children = list()
for child in component.children:
if isinstance(child, bs4.element.Tag):
valid_children.append(child)
if len(valid_children) > 0:
# Try to generate signature from its children
signature = generate_signature_from_children(matches, component)
if signature is not None:
# Path from children to component
return ('Children', own_signature, signature)
signature = generate_signature_from_parent(matches, component)
if signature is None:
return None
# Path from parent to compoent
return ('Parent', own_signature, signature)
def transform_click(action_info, ui_xml, lxml_root, view_hierarchy, lxml_view_hierarchy):
print(action_info)
package_name = action_info['package']
view_type = action_info['info']['view_type']
click_x, click_y = action_info['info']['x'], action_info['info']['y']
should_find_signature = True
if view_type == 'Activity' and len(action_info['info']['candidate']) > 0:
# Get candidate
candidates = action_info['info']['candidate']
if 'instrumentation' not in action_info:
should_find_signature = False
if isinstance(candidates, list):
candidate = None
if 'instrumentation' not in action_info:
# Find closest
candidate = find_best_match_candidate(candidates, click_x, click_y)
else:
instrumentations = action_info['instrumentation']
matched_candidates = list()
for instru in instrumentations:
view = instru['view']
for can in candidates:
if can['address'] in view:
matched_candidates.append(can)
if len(matched_candidates) > 0:
# Assign higher priority to those candidates that have resource-id
for can in matched_candidates:
if len(can['rid']) > 0 and len(can['vid']) > 0:
candidate = can
break
else:
candidate = matched_candidates[0]
else:
# Find the first in view hierarchy
def _sort(x):
view = parse_view(x['view'])
if view is None:
return math.inf
left, top, right, bottom = int(view['left']), int(view['top']), int(view['right']), int(view['bottom'])
return abs(right - left) * abs(bottom - top)
instrumentations.sort(key=_sort)
print(instrumentations)
for instru in instrumentations:
view = parse_view(instru['view'])
if view is None:
continue
candidate = find_component_in_view_hierarchy({
'rid': view['resource_id'],
'classname': view['classname'],
'vid': view['view_id'],
'address': view['address'],
'left': view['left'],
'right': view['right'],
'top': view['top'],
'bottom': view['bottom']
}, view_hierarchy)
if candidate:
bounds = get_view_hierarchy_component_bounds(candidate)
abs_left, abs_top, abs_right, abs_bottom = parse_bounds(bounds)
candidate = {
'rid': candidate['resource_id'],
'vid': candidate['view_id'],
'classname': candidate['classname'],
'address': candidate['address'],
'left': int(candidate['left']),
'top': int(candidate['top']),
'right': int(candidate['right']),
'bottom': int(candidate['bottom']),
'is_visible': candidate['visible'] == 'true',
'is_enable': candidate['enabled'] == 'true',
'is_clickable': candidate['clickable'] == 'true',
'abs_left': abs_left,
'abs_top': abs_top,
'abs_right': abs_right,
'abs_bottom': abs_bottom,
'scroll_horiz': candidate['scroll_horiz'] == 'true',
'scroll_verti': candidate['scroll_verti'] == 'true',
}
break
if candidate is None:
candidate = find_best_match_candidate(candidates, click_x, click_y)
else:
candidate = candidates
print('Candidate: ', candidate)
assert not isinstance(candidate, list)
# Get component corresponds to the candidate
view_hierarchy_component = find_component_in_view_hierarchy(candidate, view_hierarchy)
bounds = get_bounds(candidate['abs_left'], candidate['abs_top'], candidate['abs_right'], candidate['abs_bottom'])
rid = replace_package_name(candidate['rid'], package_name)
components = find_component(ui_xml, bounds, rid)
print('# Components: ', len(components))
component = None
if len(components) > 0:
for com in components:
cleft, ctop, cright, cbottom = parse_bounds(com['bounds'])
if cleft <= click_x <= cright and ctop <= click_y <= cbottom:
component = com
break
if component is None or len(components) == 0:
# Fail to find component in ui xml, then find it in view hierarchy
print('View hierarchy component: ', view_hierarchy_component)
xpath = xpath_view_hierarchy(view_hierarchy_component, lxml_view_hierarchy)
cleft, ctop, cright, cbottom = candidate['abs_left'], candidate['abs_top'], candidate['abs_right'], candidate['abs_bottom']
if cleft <= click_x <= cright and ctop <= click_y <= cbottom:
x_offset_dp, y_offset_dp = px2dp(click_x - cleft, source_dpi), px2dp(click_y - ctop, source_dpi)
else:
# absolute position may be incorrect
transformed_x, transformed_y = transform_coordinate(click_x, click_y)
return 'perform_click_event_with_raw_coordinate("%s", %f, %f, %f)' % (action_info['info']['tap_type'], transformed_x, transformed_y, action_info['info']['duration'])
# assert cleft <= click_x <= cright and ctop <= click_y <= cbottom
# x_offset_dp, y_offset_dp = px2dp(click_x - cleft, source_dpi), px2dp(click_y - ctop, source_dpi)
return 'perform_click_event_with_view_hierarchy("""%s""", "%s", %f, %f, %f, %d)' % (xpath, action_info['info']['tap_type'], x_offset_dp, y_offset_dp, action_info['info']['duration'], target_dpi)
scrollable_element = get_first_scrollable(component)
if scrollable_element is None:
scrollable_element_in_view_hierarchy = get_first_scrollable_in_view_hierarchy(view_hierarchy_component)
if scrollable_element_in_view_hierarchy is not None:
bounds = get_view_hierarchy_component_bounds(scrollable_element_in_view_hierarchy)
rid = replace_package_name(scrollable_element_in_view_hierarchy['resource_id'], package_name)
scrollable_elements = find_component(ui_xml, bounds, rid)
print('# Scrollable Components: ', len(scrollable_elements))
if len(scrollable_elements) > 0:
scrollable_element = scrollable_elements[0]
# assert len(scrollable_elements) > 0
else:
scrollable_element = None
else:
# PopupWindow & Dialog
# Find closest component
root = ui_xml.find('hierarchy')
matched_nodes = list()
for child in root.children:
if isinstance(child, bs4.element.Tag):
uiautomator_dfs(child, click_x, click_y, matched_nodes)
if len(matched_nodes) == 0:
# No component, then just simply translate coordinate
transformed_x, transformed_y = transform_coordinate(click_x, click_y)
return 'perform_click_event_with_raw_coordinate("%s", %f, %f, %f)' % (action_info['info']['tap_type'], transformed_x, transformed_y, action_info['info']['duration'])
print('Dialog || Popup Window: ')
print(matched_nodes)
def _sort(x):
left, top, right, bottom = parse_bounds(x['bounds'])
return abs(right - left) * abs(bottom - top)
matched_nodes.sort(key=_sort)
# Order by distance
component = matched_nodes[0]
should_find_signature = False
scrollable_element = get_first_scrollable(component)
if scrollable_element is not None:
should_find_signature = True
cleft, ctop, cright, cbottom = parse_bounds(component['bounds'])
x_offset_dp, y_offset_dp = px2dp(click_x - cleft, source_dpi), px2dp(click_y - ctop, source_dpi)
# Generate xpath
if should_find_signature and scrollable_element:
signature = generate_signature(component, scrollable_element)
print('Signature: ', signature)
if signature is None:
print('Fail to find signature for component')
x_path = xpath_soup(component, lxml_root)
return 'perform_click_event_with_xpath("""%s""", "%s", %f, %f, %f, %d)' % (x_path, action_info['info']['tap_type'], x_offset_dp, y_offset_dp, action_info['info']['duration'], target_dpi)
else:
print(signature)
sig_type = signature[0]
own_signature = signature[1]
scrollable_element_xpath = xpath_soup(scrollable_element, lxml_root)
if sig_type == 'Own':
return 'perform_click_event_with_u2_own_signature("""%s""", %s, "%s", %f, %f, %f, %d)' % (scrollable_element_xpath, json.dumps(own_signature), action_info['info']['tap_type'], x_offset_dp, y_offset_dp, action_info['info']['duration'], target_dpi)
elif sig_type == 'Children':
# Children
child_signature = signature[2]
return 'perform_clcik_event_with_u2_child_signature("""%s""", %s, %s, "%s", %f, %f, %f, %d)' % (scrollable_element_xpath, json.dumps(own_signature), json.dumps(child_signature), action_info['info']['tap_type'], x_offset_dp, y_offset_dp, action_info['info']['duration'], target_dpi)
else:
# Parent
parent_signature = signature[2]
return 'perform_click_event_with_u2_parent_signature("""%s""", %s, %s, "%s", %f, %f, %f, %d)' % (scrollable_element_xpath, json.dumps(own_signature), json.dumps(parent_signature), action_info['info']['tap_type'], x_offset_dp, y_offset_dp, action_info['info']['duration'], target_dpi)
else:
x_path = xpath_soup(component, lxml_root)
return 'perform_click_event_with_xpath("""%s""", "%s", %f, %f, %f, %d)' % (x_path, action_info['info']['tap_type'], x_offset_dp, y_offset_dp, action_info['info']['duration'], target_dpi)
def transform_swipe(points):
if target_width == source_width and target_height == source_height and target_dpi == source_dpi:
return points
transformed_points = list()
for point in points:
new_x, new_y = transform_coordinate(point[0], point[1])
transformed_points.append([new_x, new_y])
# Get direction, vertical or horizonal
max_distance_x = 0
max_distance_y = 0
begin_x, begin_y = transformed_points[0][0], transformed_points[0][1]
for point in transformed_points[1:]:
curr_distance_x = abs(point[0] - begin_x)
curr_distance_y = abs(point[1] - begin_y)
max_distance_x = max(max_distance_x, curr_distance_x)
max_distance_y = max(max_distance_y, curr_distance_y)
if begin_x + max_distance_x <= target_width and begin_y + max_distance_y <= target_height:
return transformed_points
# Tune the points since they overflows
if begin_y + max_distance_y > target_height:
# Transition
max_delta = 0
for point in transformed_points:
y = point[1]
if y > target_height and y - target_height > max_delta:
max_delta = y - target_height
for point in transformed_points:
point[1] = max(1, point[1] - max_delta - 10)
if begin_x + max_distance_x > target_width:
# Simply drop those overflowed points
point_to_remove = list()
for point in transformed_points:
x = point[0]
if x > target_width:
point_to_remove.append(point)
for p in point_to_remove:
transformed_points.remove(p)
return transformed_points
def init_script():
code = """# coding=utf8
import os
import sys
import time
import json
import frida
import argparse
import uiautomator2 as u2
from script import util
xml = None
lxml = None
view_hierarchy = None
lxml_view_hierarchy = None
save_path = None
action_count = 0
sub_action_count = 0
current_popup_window = None
curr_webview_address = None
display_width = None
display_height = None
samsung = False
sessions = None
def log(desc):
global action_count
print('[WidgetReplayAction]-%d: ' % action_count, desc)
def preprocess_path():
global save_path
if save_path is None:
return False
if not os.path.exists(save_path):
os.mkdir(save_path)
else:
for file in os.listdir(save_path):
os.remove(os.path.join(save_path, file))
return True
def error_handler(func):
def wrapper(message, data):
if message['type'] == 'error':
print('[Func]: %s, [Error-msg]: %s' % (func.__name__, message))
print('[Func]: %s, [Error-des]: %s' % (func.__name__, message['description']))
print('[Func]: %s, [Error-sta]: %s' % (func.__name__, message['stack']))
print('[Func]: %s, [Error-dat]: %s' % (func.__name__, data))
return None
else:
return func(message, data)
return wrapper
def post_action(custom_interval):
global xml
global lxml_xml
global d
global action_count
global save_path
global view_hierarchy
global lxml_view_hierarchy
global sub_action_count
print('[WidgetReplayTimeInterval]-%d: %s' % (action_count, json.dumps({'interval': custom_interval})))
if action_count > 0:
# time.sleep(1)
if custom_interval > 0:
time.sleep(custom_interval)
xml = d.dump_hierarchy()
lxml_xml = util.parse_lxml_xml(xml)
xml = util.parse_xml(xml)
screenshot_filename = os.path.join(save_path, '_'.join(['screenshot', str(action_count)]) + '.jpg')
xml_filename = os.path.join(save_path, '_'.join(['ui', str(action_count)]) + '.xml')
view_hierarchy_filename = os.path.join(save_path, '_'.join(['view_hierarchy', str(action_count)]) + '.xml')
d.screenshot(screenshot_filename)
util.save_xml(xml, xml_filename)
view_hierarchy, lxml_view_hierarchy = util.dump_view_hierarchy(d, view_hierarchy_filename)
action_count += 1
sub_action_count = 0
def update_dump():
global xml
global lxml_xml
global d
global action_count
global save_path
global view_hierarchy
global lxml_view_hierarchy
global sub_action_count
xml = d.dump_hierarchy()
lxml_xml = util.parse_lxml_xml(xml)
xml = util.parse_xml(xml)
screenshot_filename = os.path.join(save_path, '_'.join(['screenshot', str(action_count), str(sub_action_count)]) + '.jpg')
xml_filename = os.path.join(save_path, '_'.join(['ui', str(action_count), str(sub_action_count)]) + '.xml')
view_hierarchy_filename = os.path.join(save_path, '_'.join(['view_hierarchy', str(action_count), str(sub_action_count)]) + '.xml')
d.screenshot(screenshot_filename)
util.save_xml(xml, xml_filename)
view_hierarchy, lxml_view_hierarchy = util.dump_view_hierarchy(d, view_hierarchy_filename)
sub_action_count += 1
def set_text(text, package_name):
global xml
focused = d(focused=True)
if focused.count > 0:
if focused.info['packageName'] != package_name:
d.shell('input text "%s"' % text)
else:
focused.set_text(text)
else:
d.shell('input text "%s"' % text)
log('[set_text]-%s' % json.dumps({'text': text}))
def press_soft_keyboard(key_name):
global xml
index = util.find_soft_key(key_name, xml, is_samsung)
if index is None:
raise Exception('Key ' + key_name + ' does not exist')
key_x, key_y = index[0], index[1]
d.click(key_x, key_y)
log('[press_key]-%s' % json.dumps({'key_name': key_name}))
def hide_soft_keyboard():
global xml
if util.check_soft_keyboard(xml):
print('Hide soft keyboard')
d.press('back')
log('[hide_keyboard]')
def record_popup_window():
global current_popup_window
current_popup_window = util.get_current_window(d)
log('[record_popup_window]-%s' % json.dumps({'window': current_popup_window}))
def close_popup_window():
global current_popup_window
if current_popup_window is not None:
window = util.get_current_window(d)
if window == current_popup_window:
d.press('back')
log('[hide_popup_window]-%s' % json.dumps({'window': current_popup_window}))
current_popup_window = None
def perform_click_event_with_xpath(xpath, tap_type, x_offset, y_offset, duration, target_dpi):
global lxml_xml
component = lxml_xml.xpath(xpath)
assert len(component) > 0
component = component[0]
x, y = util.get_target_coordinate(component.attrib['bounds'], x_offset, y_offset, target_dpi)
# log('[click]-%s' % json.dumps({
# 'tap_type': tap_type,
# 'x': x,
# 'y': y,
# 'duration': duration,
# 'component': {
# 'class': component.attrib['class'] if 'class' in component.attrib else '',
# 'rid': component.attrib['resource-id'] if 'resource-id' in component.attrib else '',
# 'bounds': component.attrib['bounds'] if 'bounds' in component.attrib else '',
# 'text': component.attrib['text'] if 'text' in component.attrib else '',
# 'desc': component.attrib['content-desc'] if 'content-desc' in component.attrib else '',
# }
# }))
if tap_type == 'LongTap':
d.long_click(x, y, duration)
elif tap_type == 'Tap':
d.long_click(x, y, duration)
elif tap_type == 'DoubleTap':
d.double_click(x, y, 0.1)