sample.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import uiautomator2 as u2
  2. # 一堆常用的API操作
  3. d = u2.connect("--serial-here--") # 只有一个设备也可以省略参数
  4. d = u2.connect() # 一个设备时, read env-var ANDROID_SERIAL
  5. # 信息获取
  6. print(d.info)
  7. print(d.device_info)
  8. width, height = d.window_size()
  9. print(d.wlan_ip)
  10. print(d.serial)
  11. ## 截图
  12. d.screenshot() # Pillow.Image.Image格式
  13. d.screenshot().save("current_screen.jpg")
  14. # 获取hierarchy
  15. d.dump_hierarchy() # str
  16. # 设置查找元素等待时间,单位秒
  17. d.implicitly_wait(10)
  18. d.app_current() # 获取前台应用 packageName, activity
  19. d.app_start("io.appium.android.apis") # 启动应用
  20. d.app_start("io.appium.android.apis", stop=True) # 启动应用前停止应用
  21. d.app_stop("io.appium.android.apis") # 停止应用
  22. app = d.session("io.appium.android.apis") # 启动应用并获取session
  23. # session的用途是操作的同时监控应用是否闪退,当闪退时操作,会抛出SessionBrokenError
  24. app.click(10, 20) # 坐标点击
  25. # 无session状态下操作
  26. d.click(10, 20) # 坐标点击
  27. d.long_click(10, 10)
  28. d.double_click(10, 20)
  29. d.swipe(10, 20, 80, 90) # 从(10, 20)滑动到(80, 90)
  30. d.swipe_ext("right") # 整个屏幕右滑动
  31. d.swipe_ext("right", scale=0.9) # 屏幕右滑,滑动距离为屏幕宽度的90%
  32. d.drag(10, 10, 80, 80)
  33. d.press("back") # 模拟点击返回键
  34. d.press("home") # 模拟Home键
  35. d.long_press("volume_up")
  36. d.send_keys("hello world") # 模拟输入,需要光标已经在输入框中才可以
  37. d.clear_text() # 清空输入框
  38. d.screen_on() # wakeUp
  39. d.screen_off() # sleep screen
  40. print(d.orientation) # left|right|natural|upsidedown
  41. d.orientation = 'natural'
  42. d.freeze_rotation(True)
  43. print(d.last_toast) # 获取显示的toast文本
  44. d.clear_toast() # 重置一下
  45. d.open_notification()
  46. d.open_quick_settings()
  47. d.open_url("https://www.baidu.com")
  48. d.keyevent("HOME") # same as: input keyevent HOME
  49. # 执行shell命令
  50. output, exit_code = d.shell("ps -A", timeout=60) # 执行shell命令,获取输出和exitCode
  51. output = d.shell("pwd").output # 这样也可以
  52. exit_code = d.shell("pwd").exit_code # 这样也可以
  53. # Selector操作
  54. sel = d(text="Gmail")
  55. sel.wait()
  56. sel.click()
  57. # XPath操作
  58. # 元素操作
  59. d.xpath("立即开户").wait() # 等待元素,最长等10s(默认)
  60. d.xpath("立即开户").wait(timeout=10) # 修改默认等待时间
  61. # 常用配置
  62. d.settings['wait_timeout'] = 20 # 控件查找默认等待时间(默认20s)
  63. d.xpath("立即开户").click() # 包含查找等待+点击操作,匹配text或者description等于立即开户的按钮
  64. d.xpath("//*[@text='私人FM']/../android.widget.ImageView").click()
  65. d.xpath('//*[@text="私人FM"]').get().info # 获取控件信息
  66. for el in d.xpath('//android.widget.EditText').all():
  67. print("rect:", el.rect) # output tuple: (left_x, top_y, width, height)
  68. print("bounds:", el.bounds) # output tuple: (left, top, right, bottom)
  69. print("center:", el.center())
  70. el.click() # click operation
  71. print(el.elem) # 输出lxml解析出来的Node
  72. # 监控弹窗(在线程中监控)
  73. d.watcher.when("跳过").click()
  74. d.watcher.start()
  75. # 3. 执行模拟按键操作的命令,类似 adb shell input keyevent 26(按下电源键)
  76. d.shell('input keyevent 26')
  77. # 1. 执行查看设备系统版本的命令,类似 adb shell getprop ro.build.version.release
  78. version_result = d.shell('getprop ro.build.version.release')
  79. print(f"设备系统版本: {version_result.output.strip()}")
  80. # 2. 执行查看设备当前内存使用情况的命令,类似 adb shell dumpsys meminfo
  81. meminfo_result = d.shell('dumpsys meminfo')
  82. print(f"设备内存使用信息: {meminfo_result.output.strip()}")