自动化测试之Android视图树节点查找(优化版)

def find_node_item(json_file, search_key, match_result):
    """
      节点查找,从hierarchy之后开始查找
    :param json_file: json文件
    :param search_key: 需要匹配的key
    :param match_result: 需要匹配的value
    :return:
    """
    result = None

    with open(json_file) as f:
        # 将数据转换为json
        json_s = json.loads(f.read())

    def exec_node_search(json, search_key, match_result):
        """
        执行节点查找
        :param json: json
        :param search_key:  需要匹配的key
        :param match_result: 需要匹配的value
        :return:
        """
        try:
            type_json = type(json)
            print("do search type(json): %s" % (type_json))
            if type(json) is dict:
                node_ = json['node']
                if node_ is None:
                    print("忽略")
                elif isinstance(node_, dict):
                    if node_ is not None:
                        print("exec dict exec_node_search")
                        if utils.has_match_node(node_, search_key, match_result):
                            print("找到了需要的元素, 停止循环,停止dict递归")
                            return node_
                        else:
                            ret = exec_node_search(node_, search_key, match_result)
                            if ret is not None:
                                return ret
                elif isinstance(node_, list):
                    print("处理列表数据")
                    for node_item in node_:
                        if node_item is not None:
                            print("exec list exec_node_search item: %s" % node_item)
                            res_id = node_item[search_key]
                            print("res_id: %s" % res_id)
                            if utils.has_match_node(node_item, search_key, match_result):
                                print("找到了需要的元素, 停止循环,停止list递归")
                                return node_item
                            else:
                                ret = exec_node_search(node_item, search_key, match_result)
                                print("ret = %s" % ret)
                                if ret is not None:
                                    return ret
        except Exception as e:
            print("监听到了错误: %s" % (e.__str__()))
            pass

    result = exec_node_search(json_s['hierarchy'], search_key, match_result)
    if result is not None:
        print("find_node_item = %s" % result)
    return result
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值