第四个模块,uiautomator2点击方法封装

第四个模块,uiautomator2点击方法封装

1.uiautomator2的模拟点击方法classname,resourceId,test,xpath,坐标
2.我们把这些元素给封装成一个class

import time

import uiautomator2 as u2
import os
import subprocess
import logging

class UiClick(object):
    def __init__(self):
        logging.basicConfig(level=logging.DEBUG, format='%(asctime)s-%(levelname)s-%(message)s')
        """
        out是执行成功返回0
        info是执行后控制面板输出
        :return:
        """
        out, info = subprocess.getstatusoutput("adb shell pm list packags uiautomator")
        if out == 0:
            if "Error" not in info:
                logging.info("初始化OK")
            else:
                os.system("python -m uiautomator2 init")
        self.d = u2.connect()

    def ElementMethod(self, element_name):
        Method = self.Element.get(element_name)
        if Method is not None:
            if "resourceId" in Method:
                if self.d(resourceId=Method.get("resourceId")).exists:
                    self.d(resourceId=Method.get("resourceId")).click()
                    logging.info("使用", Method.get("resourceId"), "点击成功")
                    return Method.get("resourceId")
            elif "text" in Method:
                if self.d(text=Method.get("text")).exists:
                    self.d(text=Method.get("text")).click()
                    logging.info("使用", Method.get("text"), "点击成功")
                    return Method.get("text")
            elif "xpath" in Method:
                if self.d.xpath(Method.get("xpath")).exists:
                    self.d.xpath(Method.get("xpath")).click()
                    logging.info("使用", Method.get("xpath"), "点击成功")
                    return Method.get("xpath")
            elif "coord" in Method:
                # if self.d(xpath=Method.get("coord")).exists:
                self.d.click(Method.get("coord")[0], Method.get("coord")[1])
                logging.info("使用", Method.get("coord"), "点击成功")
                return Method.get("coord")[0], Method.get("coord")[1]
            else:
                logging.info("未找到元素,请检查")
                return None
        else:
            print("未找名字", element_name)
            return element_name

3.这样我们读取通过传入element_name,代码再通过我们读取到元素.json表格找到对应的element_name的元素,然后完成点击操作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

车载testing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值