uiautomator的简单应用
Uiautomator一直想找时间去学习,但是不得空。来了一个十一假,单身狗没有约会,只能默默在家待着了,在android的sdk目录下的tools目录中有这么一个特别好用的工具,这个事它的主界面:
然后我们可以通过adb指令去获取到这个界面的xml布局文件:对应XML文件中的信息,我们就可以根据控件节点的属性找到文件的范围,根据控件的范围可以计算出中心坐标点。
最后一条adb指令就可以点击这个控件了:adb shell input tap x y
简单介绍之后的实力示例:# coding=utf-8
import os
import re
import time
import subprocess
import xml.etree.cElementTree as ET
PATH = lambda p: os.path.abspath(
os.path.join(os.path.dirname(__file__), p)
)
def Script(Script):
return os.popen(Script).read()
def GetActivitXml(FileName):
adbscript='adb devices -l'
Script('adb shell uiautomator dump /data/local/tmp/'+FileName+'.xml')
Script('adb pull /data/local/tmp/'+FileName+'.xml ./')
Script('adb shell rm -r /data/local/tmp/'+FileName+'.xml')å
def Tapxy(x,y):
Script('adb shell input tap '+str(x)+' '+str(y))
def putNodeType(Element):
# print Element.attrib['index']
# print Element.attrib['text']
print Element.attrib['resource-id']
# print Element.attrib['class']
# print Element.attrib['package']
# print Element.attrib['content-desc']
# print Element.attrib['checkable']
# print Element.attrib['checked']
# print Element.attrib['clickable']
# print Element.attrib['enabled']
# print Element.attrib['focusable']
# print Element.attrib['focused']
# print Element.attrib['scrollable']
# print Element.attrib['long-clickable']
# print Element.attrib['password']
# print Element.attrib['selected']
# print Element.attrib['bounds']
def GetElementXY(FileName,TypeName,TypeValue):
print u"当前界面寻找【"+TypeName+u"】为【"+TypeValue+u"】的元素"
GetActivitXml(FileName)
tree = ET.ElementTree(file=PATH(FileName+".xml")) #加载XML文件
ElementList = tree.iter(tag="node") #获取所有node节点
Xpoint=0;
Ypoint=0;
for Element in ElementList:#节点遍历
if Element.attrib == TypeValue :
bounds = Element.attrib["bounds"]
# 通过正则获取坐标列表
pattern = re.compile(r"\d+")
bound = pattern.findall(bounds)
# print bound
Xpoint=(int(bound)+int(bound))/2
Ypoint=(int(bound)+int(bound))/2
return (Xpoint, Ypoint)
def TestCase():
ADElement=GetElementXY("TestTemp","text",u"浏览器")
Tapxy(ADElement,ADElement)
time.sleep(0.5)
ADElement=GetElementXY("TestTemp","resource-id","com.android.browser:id/windows")
Tapxy(ADElement,ADElement)
time.sleep(0.5)
ADElement=GetElementXY("TestTemp","resource-id",u"com.android.browser:id/multi_window_add")
Tapxy(ADElement,ADElement)
time.sleep(0.5)
ADElement=GetElementXY("TestTemp","resource-id","com.android.browser:id/windows")
Tapxy(ADElement,ADElement)
time.sleep(0.5)
ADElement=GetElementXY("TestTemp","text",u"关闭全部")
Tapxy(ADElement,ADElement)
time.sleep(0.5)
ADElement=GetElementXY("TestTemp","text",u"取消")
Tapxy(ADElement,ADElement)
time.sleep(0.5)
ADElement=GetElementXY("TestTemp","text",u"关闭全部")
Tapxy(ADElement,ADElement)
time.sleep(0.5)
ADElement=GetElementXY("TestTemp","text",u"关闭")
Tapxy(ADElement,ADElement)
time.sleep(0.5)
ADElement=GetElementXY("TestTemp","resource-id",u"com.android.browser:id/menu_wrapper")
Tapxy(ADElement,ADElement)
time.sleep(0.5)
ADElement=GetElementXY("TestTemp","resource-id",u"com.android.browser:id/quit")
Tapxy(ADElement,ADElement)
time.sleep(0.5)
if __name__ == '__main__':
for x in xrange(1,100):
print "===============第"+str(x)+"遍测试============="
TestCase()
页:
[1]