环境配置参考官方文档https://macacajs.github.io/zh/environment-setup
启动服务器
# 普通用法,添加了 --verbose 可以看到详细的信息
$ macaca server --verbose
# 设置端口号
$ macaca server -p 3456
检查当前 Macaca 的安装环境
$ macaca doctor
元素查看器
安装
$ npm i app-inspector -g
用法
# 直接 -u + 设备的 udid 即可
$ app-inspector -u xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
测试代码
[python]
from macaca import WebDriver
import time
server_url = {
'hostname': '127.0.0.1',
'port': 3456
}
desired_caps = {
'platformName': 'Android', # iOS, Android.
'deviceName': '69T7N1622*******',
'platformVersion': '5.1.1',
'reuse': '3',#0: 启动并安装 app。1 (默认): 卸载并重装 app。 2: 仅重装 app。3: 在测试结束后保持 app 状态。
'package':'com.tencent.mm',#微信
#'activity':'com.tencent.mm.ui.LauncherUI'
}
driver = WebDriver(desired_caps,server_url)
driver.init()
#需要登录密码时使用
#driver.wait_for_element('xpath','//*[@resource-id="com.tencent.mm:id/bfn"]',timeout=60).send_keys("keys")
#driver.wait_for_element('xpath','//*[@resource-id="com.tencent.mm:id/bfo"]',timeout=60).touch('tap')
driver.touch('tap', { 'x': 637, 'y': 1638})#根据坐标值定位元素
driver.wait_for_element('xpath','//*[@text="朋友圈"]',timeout=60).touch('tap')#根据xpath定位元素
text=driver.source#获取文本
print(text)
#拖动10次
for i in range(10):
driver.touch('drag', { 'fromX': 530,
'fromY': 1700,
'toX': 800,
'toY': 300,
'duration': 1})
time.sleep(random.uniform(12, 16))
def switch_to_webview():
contexts = self.driver.contexts
driver.context = contexts[-1]
return driver
def switch_to_native(self):
contexts = self.driver.contexts
self.driver.context = contexts[0]
return driver