Skip to content

Commit 1201a5c

Browse files
committed
Make kato use window_hierarchy_to_selector_list
1 parent be809aa commit 1201a5c

4 files changed

Lines changed: 275 additions & 183 deletions

File tree

docs/.doctrees/environment.pickle

0 Bytes
Binary file not shown.

src/com/dtmilano/android/kato/kato.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,38 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Copyright (C) 2012-2022 Diego Torres Milano
4+
Created on Nov 7, 2022
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
@author: Diego Torres Milano
19+
"""
20+
121
import sys
222
from collections import OrderedDict
323

424
from culebratester_client import Selector
525
from culebratester_client.rest import ApiException
626

727
from com.dtmilano.android.distance import levenshtein_distance
28+
from com.dtmilano.android.uiautomator.utils import window_hierarchy_to_selector_list
829

930
#
1031
# https://en.wikipedia.org/wiki/Kato_(The_Green_Hornet)
1132
#
1233

1334
DEBUG = False
1435

15-
ANYTHING = 'Pattern:^.*$'
16-
1736

1837
class Kato:
1938
def __init__(self):
@@ -25,11 +44,13 @@ def __init__(self):
2544
def kato(func):
2645
"""
2746
Kato decorator.
28-
@param func:the function to invoke
29-
@type func:
30-
@return: the wrapper
31-
@rtype:
47+
48+
:param func: the function to invoke
49+
:type func: function
50+
:return: the wrapper
51+
:rtype:
3252
"""
53+
3354
def wrapper(*args, **kwargs):
3455
try:
3556
return func(*args, **kwargs)
@@ -57,18 +78,18 @@ def find_me_the_selectors(e: ApiException, *args, **kwargs):
5778
print('find_me_the_selectors', args, kwargs, file=sys.stderr)
5879
helper = args[0].uiAutomatorHelper
5980
msg = ''
60-
_d = dict()
6181
if helper.kato.enabled:
6282
if e.status == 404:
6383
distance = kwargs['distance_func']
6484
mapper = kwargs['distance_func_argument_mapper']
65-
helper.kato.selectors = list(map(lambda oid: helper.ui_object2.dump(oid),
66-
map(lambda obj_ref: obj_ref.oid,
67-
helper.ui_device.find_objects(body={'text': ANYTHING}))))
68-
for n, s in enumerate(filter(lambda _s: helper.ui_device.has_object(body=_s), helper.kato.selectors)):
85+
selector = Selector(**kwargs['body'])
86+
helper.kato.selectors = window_hierarchy_to_selector_list(
87+
helper.ui_device.dump_window_hierarchy(_format='JSON'))
88+
_d = dict()
89+
for n, s in enumerate(helper.kato.selectors):
6990
if n == 0:
7091
msg += 'Kato: selector distances:\n'
71-
d = distance(mapper(Selector(**kwargs['body'])), mapper(s))
92+
d = distance(mapper(selector), mapper(s))
7293
_d[d] = s
7394
msg += f'{d} -> {s}\n'
7495
helper.kato.distances = OrderedDict(sorted(_d.items(), reverse=False))
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Copyright (C) 2012-2022 Diego Torres Milano
4+
Created on Nov 7, 2022
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
@author: Diego Torres Milano
19+
"""
20+
21+
from __future__ import print_function
22+
23+
from typing import Union
24+
25+
from culebratester_client import WindowHierarchyChild, Selector, WindowHierarchy
26+
27+
28+
def window_hierarchy_child_to_selector(window_hierarchy_child: WindowHierarchyChild) -> Selector:
29+
"""
30+
Converts a WindowHierarchyChild to a Selector.
31+
32+
:param window_hierarchy_child: the WindowHierarchyChild to convert
33+
:type window_hierarchy_child: WindowHierarchyChild
34+
:return: the Selector
35+
:rtype: Selector
36+
"""
37+
sel = Selector()
38+
sel.checkable = window_hierarchy_child.checkable
39+
sel.checked = window_hierarchy_child.checked
40+
sel.clazz = window_hierarchy_child.clazz
41+
sel.clickable = window_hierarchy_child.clickable
42+
sel.depth = None
43+
sel.desc = window_hierarchy_child.content_description if window_hierarchy_child.content_description else None
44+
sel.pkg = window_hierarchy_child.package if window_hierarchy_child.package else None
45+
sel.res = window_hierarchy_child.resource_id if window_hierarchy_child.resource_id else None
46+
sel.scrollable = window_hierarchy_child.scrollable
47+
sel.text = window_hierarchy_child.text if window_hierarchy_child.text else None
48+
sel.index = None # child.index (could be 0)
49+
sel.instance = None
50+
return sel
51+
52+
53+
def window_hierarchy_to_selector_list(node: Union[WindowHierarchy, WindowHierarchyChild], selector_list=None) -> list[
54+
Selector]:
55+
"""
56+
Converts a WindowHierarchy (obtained via ``window_hierarchy_dump()``) to a Selector list.
57+
58+
:param node: the node being processed
59+
:type node: Union[WindowHierarchy, WindowHierarchyChild]
60+
:param selector_list: the list, could be ``None``
61+
:type selector_list: list[Selector]
62+
:return: the list
63+
:rtype: list[Selector]
64+
"""
65+
if selector_list is None:
66+
selector_list = []
67+
if node.id != 'hierarchy':
68+
selector_list.append(window_hierarchy_child_to_selector(node))
69+
for ch in node.children:
70+
window_hierarchy_to_selector_list(ch, selector_list)
71+
return selector_list

0 commit comments

Comments
 (0)