51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1744|回复: 2
打印 上一主题 下一主题

基于Python的Android自动化测试脚本

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2018-5-21 16:15:39 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
基于Python的Android自动化测试脚本

由于工作需要,需要完成很多大量的重复工作去测试软件的性能问题,例如这次完成的人脸识别的优化,
为了测试效率和内存使用情况,需要不停的重复添加,选择,输入姓名,点击注册等等操作,虽然无脑,
但是做多了让人很头疼的。所以我写了下面这个脚本。

需求

首先我们需要明确下需求目标,我们的需求很简单,就是使用python脚本完成需要我们自己完成的很多
重复的操作,即便中间存在一定的重复操作之外,但是整体操作是完全重复和符合规则的。

设计思路

我的设计思路很简单,就是用Python使用 ADB 命令,模拟人为的点击输入等操作。

流程

1.进入主界面后,点击添加按钮

2.进入图片选择界面后判断是否要向下滑动

3.如果需要滑动 则滑动一定距离后再判断

4.顺序点击需要注册的图片

5.如果检测到有人脸,会弹出弹窗,则输入用户名,点击注册

6.如果未检测到人脸,点击重新选择按钮,重复操作。

实现

  1. import os
  2. import time

  3. count_select = 0
  4. count_swipe = 0


  5. # 点击事件
  6. def click(x, y):
  7.     cmd = "adb shell input tap {x1} {y1}".format(
  8.         x1=x,
  9.         y1=y
  10.     )
  11.     os.system(cmd)


  12. # 添加点击事件 只在主界面时点击添加按钮进行人脸添加
  13. def click_add():
  14.     click(1218, 30)
  15.     time.sleep(1)
  16.     swipe()


  17. # 确认按钮添加,即在识别到人脸后,进行的操作
  18. def click_ok():
  19.     click(400, 640)
  20.     # 睡眠2秒 防止添加人脸时进行比对的人脸过多导致的延迟等
  21.     time.sleep(2)
  22.     main()


  23. # 点击重新注册
  24. def click_re():
  25.     click(1152, 785)
  26.     time.sleep(1)  # 等待一秒使图片加载完成
  27.     swipe()


  28. # 输入用户名
  29. def input_name():
  30.     os.system("adb shell input text 1")
  31.     click_ok()


  32. # 点击相册中对应的图片进行注册操作
  33. def click_register():
  34.     global count_swipe
  35.     global count_select
  36.     if count_select == 0:  # 第一个
  37.         count_select = count_select + 1
  38.         click(160, 150)
  39.     elif count_select == 1:
  40.         count_select = count_select + 1
  41.         click(500, 150)
  42.     elif count_select == 2:
  43.         count_select = count_select + 1
  44.         click(810, 150)
  45.     elif count_select == 3:
  46.         count_select = count_select + 1
  47.         click(1125, 150)
  48.     elif count_select == 4:
  49.         count_select = count_select + 1
  50.         click(160, 475)
  51.     elif count_select == 5:
  52.         count_select = count_select + 1
  53.         click(500, 475)
  54.     elif count_select == 6:
  55.         count_select = count_select + 1
  56.         click(810, 475)
  57.     elif count_select == 7:
  58.         count_select = count_select + 1
  59.         click(1125, 475)
  60.     elif count_select == 8:
  61.         count_select = 0
  62.         count_swipe = count_swipe + 1
  63.         # 一屏幕结束,进行下一次循环操作
  64.         swipe()
  65.     if count_select != 0:
  66.         screen_xml()


  67. # 滑动屏幕  根据屏幕数判断需要滑动的次数
  68. def swipe():
  69.     count_swipe_finish = count_swipe
  70.     if count_swipe_finish > 0:
  71.         for i in range(0, count_swipe_finish):  # 循环滑动
  72.             os.system("adb shell input swipe 100 410 100 5")
  73.     click_register()


  74. # 获取当前名目的所有控件布局 并写入到xml文件中
  75. def screen_xml():
  76.     os.system("adb shell uiautomator dump /sdcard/ui.xml")
  77.     time.sleep(3)
  78.     read_xml()


  79. # 读取xml文件,判断是否存在"重新选择"的按钮,如果不存在 则表明有人脸
  80. def read_xml():
  81.     os.system("adb pull /sdcard/ui.xml .")
  82.     f = open("./ui.xml", "r", encoding="UTF-8")
  83.     s = f.read()
  84.     if s.find("id/res_tv_to_gallery") == -1:
  85.         print("有人脸")
  86.         input_name()

  87.     else:
  88.         print("无人脸")
  89.         click_re()


  90. def main():
  91.     click_add()


  92. main()
复制代码






分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1
回复

使用道具 举报

该用户从未签到

3#
发表于 2018-5-30 13:24:30 | 只看该作者
用像素定位能准确么
回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-4-27 13:07 , Processed in 0.063440 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表