Android UiAutomator: http://developer.android.com/tools/help/uiautomator/index.html
$ [sudo] pip install uiautomator
from uiautomator import device as d
d.screen.on()
d(resourceId="android:id/glow_pad_view").swipe.down()
d(text="Camera").click()
d.press.back()
d.press.power()
在原生Android 4.3 锁屏界面运行如上代码得到如下结果:
from uiautomator import device as d
d.info
获得如下信息:
{ u'displayRotation': 0,
u'displaySizeDpY': 640,
u'displaySizeDpX': 360,
u'currentPackageName': u'com.android.launcher',
u'productName': u'takju',
u'displayWidth': 720,
u'sdkInt': 18,
u'displayHeight': 1184,
u'naturalOrientation': True
}
from uiautomator import device as d
# 点亮屏幕
d.screen.on()
# 关闭屏幕
d.screen.off()
类似于:
d.wakeup() # screen on
d.sleep() # screen off
from uiautomator import device as d
# 点击屏幕坐标点(x, y)
d.click(x, y)
from uiautomator import device as d
# 从屏幕坐标点(sx, sy)托拽到(ex, ey)
d.drag(sx, sy, ex, ey)
# 按 Home 键
d.press.home()
# 下面与上面等价
d.press("home")
下面是所有按键的定义:
# 获得 orientation
d.orientation
# 设置 Orientation
d.orientation = "left" # or "right", "natural"
# 冻结/解除冻结屏幕rotation
d.freeze_rotation()
d.freeze_rotation(False)
支持的Orientation包括:
截屏
# 截屏并保存为本地 home.png
d.screenshot("home.png")
Dump
# Dump hierarchy 到本地文件 home.xml
d.dump("home.xml")
# 打开通知
d.open.notification()
# 打开快速设置
d.open.quick_settings()
# 注册watcher
d.watcher("CLOSE-ANR-DIALOG") \
.when(text="ANR") \
.click(className="android.widget.Button", text="Force Close")
# 注册watcher, 当满足条件是按back和home键
d.watcher("CLOSE-ANR-DIALOG") \
.when(text="ANR") \
.press.back.home()
# 选择text为 Settings的UI对象
d(text="Settings")
# 选择标准Button UI对象
d(className="android.widget.Button")
# UI对象属性
d(className="android.widget.Button").info
常用的选择器参数
from uiautomator import device as d
# 点击text为 Settings的UI对象
d(text="Settings").click()
# 长点击text为 Settings的UI对象
d(text="Settings").long_click()
from uiautomator import device as d
# 获得text属性
d(className="android.widget.EditText").text
# 设置可编辑文本
d(text="Email").set_text("xiaocong@gmail.com")
# 清除可编辑文本
d(text="xiaocong@gmail.com").clear_text()
from uiautomator import device as d
# 推拽一个UI对象到另外一个UI对象
d(text="Clock").drag.to(text="Calculator")
# 推拽一个UI对象到坐标(x, y)
d(text="Clock").drag.to(x, y)
# 向右滑动
d(resourceId="com.android.launcher:id/workspace").swipe.right()
# 或者这样写
d(resourceId="com.android.launcher:id/workspace").swipe("left")
支持4个方向的滑动:left, right, up, down
# 水平向前滚动
d(scrollable=True).scroll.horiz.forward()
# 垂直滚动到开始
d(scrollable=True).scroll.vert.toBeginning()
# 水平滚动,直到目标UI对象出现
d(scrollable=True).scroll.horiz.to(text="Clock")
支持2个维度的滚动:horiz和vert
支持5种的滚动目标:forward, backward, toBeginning, toEnd, to
# 水平向前快速滚动
d(scrollable=True).fling.horiz.forward()
# 垂直快速滚动到开始
d(scrollable=True).fling.vert.toBeginning()
支持2个维度的滚动:horiz和vert
支持4种的滚动目标:forward, backward, toBeginning, toEnd
from uiautomator import device as d, point
# 双指挤入
d(resourceId="com.android.gallery3d:id/gl_root_view") \
.pinch.In()
# 双指挤开
d(resourceId="com.android.gallery3d:id/gl_root_view") \
.pinch.In()
# 双点手势
d(resourceId="com.android.gallery3d:id/gl_root_view") \
.gesture(point(sx1, sy1), point(sx2, sy2)) \
.to(point(ex1, ey1), point(ex2, ey2))
# 存在与否
d(text="Clock").exists
# 或者
d.exists(text="Clock")
# 等待直到出现
d(text="Clock").wait.exists(timeout=5000)
# 等待直到消失
d(text="Clock").wait.gone(timeout=5000)
Python unittest: http://docs.python.org/2/library/unittest.html
Nose: https://github.com/nose-devs/nose
Lettuce: http://lettuce.it/
Android uiautomator: http://developer.android.com/tools/help/uiautomator/index.html
Github Repo: https://github.com/xiaocong/uiautomator
Use a spacebar or arrow keys to navigate