• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# Copyright (C) 2024 Huawei Device Co., Ltd.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import os
17import zipfile
18import subprocess
19import re
20import time
21import json
22
23OUTPUT_PATH = "testModule/output"
24PID_INDEX = 7
25
26
27def get_path_by_attribute(tree, key, value):
28    attributes = tree['attributes']
29    if attributes is None:
30        print("tree contains no attributes")
31        return None
32    path = []
33    if attributes.get(key) == value:
34        return path
35    for index, child in enumerate(tree['children']):
36        child_path = path + [index]
37        result = get_path_by_attribute(child, key, value)
38        if result is not None:
39            return child_path + result
40    return None
41
42
43def get_element_by_path(tree, path):
44    if len(path) == 1:
45        return tree['children'][path[0]]
46    return get_element_by_path(tree['children'][path[0]], path[1:])
47
48
49def get_location_by_text(tree, text):
50    path = get_path_by_attribute(tree, "text", text)
51    if path is None or len(path) == 0:
52        print("text not found in layout file")
53    element = get_element_by_path(tree, path)
54    locations = element['attributes']['bounds'].replace('[', '').replace(']', ' ').replace(',', ' ').strip().split()
55    return int((int(locations[0]) + int(locations[2])) / 2), int((int(locations[1]) + int(locations[3])) / 2)
56
57
58def touch(dx, dy):
59    output = subprocess.check_output(f"hdc shell uitest uiInput click {dx} {dy}")
60
61
62def get_layout_tree():
63    output = subprocess.check_output("hdc shell uitest dumpLayout", text=True)
64    path = output.strip().split(":")[-1]
65    subprocess.check_output(f"hdc file recv {path} .\..\inputfiles\layout.json")
66    subprocess.check_output("hdc shell rm " + path)
67    with open(".\..\inputfiles\layout.json", encoding="utf-8") as f:
68        tree = json.load(f)
69    return tree
70
71
72def touch_button(text):
73    layout_tree = get_layout_tree()
74    location = get_location_by_text(layout_tree, text)
75    touch(location[0], location[1])
76
77
78def get_pid(name):
79    subprocess.check_output("hdc shell ps -ef | grep " + name + " > /data/local/tmp/pids.txt")
80    subprocess.check_output(f"hdc file recv /data/local/tmp/pids.txt .\..\outputfiles\ ", text=True, encoding="utf-8")
81    with open(r'.\..\outputfiles\pids.txt', 'r') as file:
82        lines = file.readlines()
83        for line in lines:
84            if line.split()[PID_INDEX] == name:
85                return line.split()[1]
86    return 0
87