UiautoMator2源码分析基本配置和操作:
⾸先从details = ua.connect(‘81299603’)开始:
def connect(addr=None):
if not addr or addr == '+':
#在环境变量中获取设备
addr = os.getenv('ANDROID_DEVICE_IP'):
if _is_wifi_addr(addr):
#通过统⼀wifi⽹络连接
return connect_wifi(addr)
//通过usb连接
return connect_usb(addr)
我使⽤的是usb连接,通过设备的serial号进⾏关联:
def connect_usb(serial=None):
"""
serial (str): android序列号可以通过adb devices获取
返回值:UIAutomatorServer
"""
adb = adbutils.AdbClient()
if not serial:
# 通过遍历设备列表,到并判断是否存在设备,或者是否存在唯⼀设备
device = adb.must_one_device()
else:
device = adbutils.AdbDevice(adb, serial)
# adb = adbutils.Adb(serial)
lport = device.forward_port(7912)
d = connect_wifi('127.0.0.1:' + str(lport))
if not d.agent_alive:
# 获取设备服务信息,⼀定要打开ATX 中的启动UiautoMator服务
warnings.warn("backend atx-agent is not alive, start again ...",
RuntimeWarning)
device.shell_output("/data/local/tmp/atx-agent", "server", "-d")
deadline = time.time() + 3
while time.time() < deadline:
if d.alive:
break
elif not d.alive:
warnings.warn("backend uiautomator2 is not alive, start again ...",
RuntimeWarning)
return d
details.device_info:
获取的是android system info⽬录下的信息
def app_start(self,
pkg_name,
activity=None,
extras={},
wait=True,
stop=False,
unlock=False):
if activity:
args = [
'am', 'start', '-a', 'android.intent.action.MAIN', '-c',
'android.intent.category.LAUNCHER'
]
details.press(‘home’):
def press(self, *keys):
"""
Android系统中关键码定义[\[Android关键码\]](developer.android/reference/android/view/KeyEvent.html)    key (str): on of
("home", "back", "left", "right", "up", "down", "center",
"search", "enter", "delete", "del", "recent", "volume_up",
"menu", "volume_down", "volume_mute", "camera", "power")
"""
obj.isterPressKeyskWatcher(
name, self.__selectors, keys)
asp查看源码配置ui
details.unlock():
def unlock(self):
# 启动com.github.uiautomator/.IdentifyActivity'
self.open_identify()
# 回到主界⾯
self._default_session.press("home")
⼊门级别的⼩测试:
import uiautomator2 as u2
from PIL import Image
import time
details = u2.connect('serial')
sess = details.session("bileqq")
sess.running()
# 会存在性能问题,多次登录进⼊退出,会有加载延迟,所以采⽤延时函数待画⾯加载完成后在进⾏点击
while True:
#定位到输⼊密码控件
details(resourceId="bileqq:id/password").clear_text()
details(resourceId="bileqq:id/password").set_text("139********")
details.set_fastinput_ime(False)
details(resourceId="bileqq:id/login").click()
#点击通知圆点并实现拖拽
details(className="android.widget.TabWidget", resourceId="android:id/tabs")\
.child(className="android.widget.RelativeLayout")\
.
child(className="android.widget.TextView", resourceId="bileqq:id/name").drag_to(259,1299)
#向上滑动10
# details(resourceId="bileqq:id/recent_chat_list").scroll(steps=10)
#点击头像个⼈中⼼,但是此时⼦线程还没有处理结束画⾯还没有绘制
time.sleep(3.0)
details(resourceId="bileqq:id/conversation_head").click()
# details.click(83,155)
#点击设置
time.sleep(3.0)
details(resourceId="bileqq:id/name", className="android.widget.ImageView", instance=21).click()
time.sleep(3.0)
#点击账号管理
details(resourceId="bileqq:id/account_switch").click()
time.sleep(3.0)
#点击退出登录
details(resourceId="bileqq:id/logoutBtn").click()
time.sleep(2.0)
#点击确认退出
details(resourceId="bileqq:id/dialogRightBtn").click()
#清空密码栏
details(resourceId="bileqq:id/password").clear_text()

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。