Android必会的自动化测试

简介: monkey测试 adb shell  monkey -p com.slicejobs.ailinggong -v 500monkey running-------------------------命令自己写1 一条条命令 adb install xxx.

monkey测试 

adb shell  monkey -p com.slicejobs.ailinggong -v 500


monkey running

-------------------------命令自己写

1 一条条命令


	adb install xxx.apk	安装apk文件
	adb shell am start -an com.xxx.xxx/.MainActivity 启动APP
	adb shell am force-stop com.xxx.xxx 停止该APP
	adb shell input keyevent KEYCODE_HOME 模拟Android的HOME按键
	adb -s emulator-5554 shell input text test_to_input 针对特定的一个模拟器进行操作
	adb shell input tap x y 模拟屏幕touch操作
	adb shell input swipe x1 y1 x2 y2 模拟屏幕滑动操作
	adb devices 查看所有在线的Android设备.

启动activity   

adb shell am start -n com.slicejobs.ailinggong/com.slicejobs.ailinggong.ui.activity.SplashActivity

遇到SecurityException异常

  1. <activity  
  2.     android:name="com.siveco.bluebee.core.activity.SplashActivity"  
  3.     android:label="@string/title_activity_get_server_address"  
  4.     android:exported="true" >  
  5. </activity>


2 用python执行脚本

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sys
from com.android.monkeyrunner import MonkeyRunner as mr  
from com.android.monkeyrunner import MonkeyDevice as md  
from com.android.monkeyrunner import MonkeyImage as mi

#连接设备(等待时间,设备号)
device = mr.waitForConnection(1.0,'dea1887f')
if not device:
print >> sys.stderr,"fail"
sys.exit(1)
#定义要启动的activity
componentName='com.slicejobs.ailinggong/com.slicejobs.ailinggong.ui.activity.SplashActivity'
device.startActivity(component=componentName)
#等待4秒
mr.sleep(3.0) 


#1通过坐标找到控件
#device.touch(250, 450, 'DOWN_AND_UP')


#device.type('helloworld')


#device.press('KEYCODE_ENTER') 


#device.press('KEYCODE_BACK') 


#2.1通过id来操作,更容易找到元素,(必须是开发模式的手机才能用)
# from com.android.monkeyrunner.easy import EasyMonkeyDevice, By
# ease_device = EasyMonkeyDevice(device)
# easy_device.touch(By.id('id/btn_login'), easy_device.DOWN_AND_UP)


#2.2--------提取UI界面的元素(必须是开发模式的手机才能用)
# from com.android.chimpchat.hierarchyviewer import HierarchyViewer
# hViewer = device.getHierarchyViewer() # 对当前UI视图进行解析
# content = hViewer.findViewById('id/btn_login')  # 通过id查找对应元素
# print content.getText();


#2.3有些时侯碰到解析不出来情况是用
#github下载https://github.com/dtmilano/AndroidViewClient然后配置一下环境,增加ANDROID_VIEW_CLIENT_HOME(其实不配置也OK)
# from com.dtmilano.android.viewclient import ViewClient
# vc = ViewClient(device=device, serialno="emulator-5554")
# content = hViewer.findViewById('id/btn_login') 

连接真机,使用monkeyrunner自动化测试工具运行脚本-提示脚本easy_device=EasyMonkeyDevice(device1)附近could not connect to view server


原因:

EasyMonkeyDevice模块用来通过ID访问控件,而EasyMonkeyDevice和这里没用到的Hierarchy Viewer只能连接开发版手机或模拟器,我们普通的商业手机是无法连上的

老版本的Hierarchy Viewer可以),所以出现了以上错误。

这句错误意思是:不能连接到View Server,View Server需要模拟机或开发机,还有可能是开发机没有开启这项服务。

可以试试下面:

a.检验一台手机是否开启了View Server '

adb shell service call window 3

b.打开View Server

adb shell service call window 1 i32 4939

如果还不行就果断换设备吧。

------------------命令自动录制,直接按照下面步骤做

这才是真正实用的功能,直接看代码,创建一个recoder.py:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #!/usr/bin/env monkeyrunner  
  2. # Copyright 2010, The Android Open Source Project  
  3. #  
  4. # Licensed under the Apache License, Version 2.0 (the "License");  
  5. # you may not use this file except in compliance with the License.  
  6. # You may obtain a copy of the License at  
  7. #  
  8. #     http://www.apache.org/licenses/LICENSE-2.0  
  9. #  
  10. # Unless required by applicable law or agreed to in writing, software  
  11. # distributed under the License is distributed on an "AS IS" BASIS,  
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  13. # See the License for the specific language governing permissions and  
  14. # limitations under the License.  
  15.   
  16. from com.android.monkeyrunner import MonkeyRunner as mr  
  17. from com.android.monkeyrunner.recorder import MonkeyRecorder as recorder  
  18.   
  19. device = mr.waitForConnection()  
  20. recorder.start(device)  

命令行下运行:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. monkeyrunner monkey_recorder.py  

这时会弹出这样的界面:


按钮以及一些功能说明:

Button Description
Wait 等待时间
Press a Button 发送,MENU,HOME,or SEARCH 按钮.Press,Down,or Up事件
Type Something 发送一些字符串
Fling 用来操作虚拟键盘 
image
Export Action 将我们的脚本导出来
Refresh Display 刷新当前界面

自己随心所以创建一些事件脚本,想做什么就可以做什么,通过MonkeyRecorder这个工具来操作设备界面,事件编辑完后选择Export Actions,导出到我们tools目录下命名为:action.mr

我们看一下工具生成的action.mr脚本,如下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. TOUCH|{'x':297,'y':533,'type':'downAndUp',}  
  2. WAIT|{'seconds':2.0,}  
  3. TOUCH|{'x':136,'y':278,'type':'downAndUp',}  
  4. WAIT|{'seconds':2.0,}  
  5. TOUCH|{'x':123,'y':356,'type':'downAndUp',}  
  6. WAIT|{'seconds':10.0,}  
  7. PRESS|{'name':'HOME','type':'downAndUp',}  
  8. WAIT|{'seconds':2.0,}  
  9. TOUCH|{'x':235,'y':720,'type':'downAndUp',}  
  10. WAIT|{'seconds':2.0,}  
  11. TOUCH|{'x':303,'y':630,'type':'downAndUp',}  
  12. WAIT|{'seconds':2.0,}  
  13. TOUCH|{'x':16,'y':71,'type':'downAndUp',}  
  14. WAIT|{'seconds':2.0,}  
  15. TOUCH|{'x':244,'y':735,'type':'downAndUp',}  



然后需要制作一个运行这一系列动作的脚本:monkey_playback.py,保存到tools目录下:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #!/usr/bin/env monkeyrunner  
  2. # Copyright 2010, The Android Open Source Project  
  3. #  
  4. # Licensed under the Apache License, Version 2.0 (the "License");  
  5. # you may not use this file except in compliance with the License.  
  6. # You may obtain a copy of the License at  
  7. #  
  8. #     http://www.apache.org/licenses/LICENSE-2.0  
  9. #  
  10. # Unless required by applicable law or agreed to in writing, software  
  11. # distributed under the License is distributed on an "AS IS" BASIS,  
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  13. # See the License for the specific language governing permissions and  
  14. # limitations under the License.  
  15.   
  16. import sys  
  17. from com.android.monkeyrunner import MonkeyRunner  
  18.   
  19. # The format of the file we are parsing is very carfeully constructed.  
  20. # Each line corresponds to a single command.  The line is split into 2  
  21. # parts with a | character.  Text to the left of the pipe denotes  
  22. # which command to run.  The text to the right of the pipe is a python  
  23. # dictionary (it can be evaled into existence) that specifies the  
  24. # arguments for the command.  In most cases, this directly maps to the  
  25. # keyword argument dictionary that could be passed to the underlying  
  26. # command.   
  27.   
  28. # Lookup table to map command strings to functions that implement that  
  29. # command.  
  30. CMD_MAP = {  
  31.     'TOUCH': lambda dev, arg: dev.touch(**arg),  
  32.     'DRAG': lambda dev, arg: dev.drag(**arg),  
  33.     'PRESS': lambda dev, arg: dev.press(**arg),  
  34.     'TYPE': lambda dev, arg: dev.type(**arg),  
  35.     'WAIT': lambda dev, arg: MonkeyRunner.sleep(**arg)  
  36.     }  
  37.   
  38. # Process a single file for the specified device.  
  39. def process_file(fp, device):  
  40.     for line in fp:  
  41.         (cmd, rest) = line.split('|')  
  42.         try:  
  43.             # Parse the pydict  
  44.             rest = eval(rest)  
  45.         except:  
  46.             print 'unable to parse options'  
  47.             continue  
  48.   
  49.         if cmd not in CMD_MAP:  
  50.             print 'unknown command: ' + cmd  
  51.             continue  
  52.   
  53.         CMD_MAP[cmd](device, rest)  
  54.   
  55.   
  56. def main():  
  57.     file = sys.argv[1]  
  58.     fp = open(file, 'r')  
  59.   
  60.     device = MonkeyRunner.waitForConnection()  
  61.       
  62.     process_file(fp, device)  
  63.     fp.close();  
  64.       
  65.   
  66. if __name__ == '__main__':  
  67.     main()  


接下来运行我们的保存的脚本,然后,你就看到真机(模拟器),进行你刚才一样的操作~

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. E:\android-sdk-windows\tools>monkeyrunner monkey_playback.py action.mr  






目录
相关文章
|
21天前
|
Java 测试技术 Android开发
Android性能测试——发现和定位内存泄露和卡顿
本文详细介绍了Android应用性能测试中的内存泄漏与卡顿问题及其解决方案。首先,文章描述了使用MAT工具定位内存泄漏的具体步骤,并通过实例展示了如何分析Histogram图表和Dominator Tree。接着,针对卡顿问题,文章探讨了其产生原因,并提供了多种测试方法,包括GPU呈现模式分析、FPS Meter软件测试、绘制圆点计数法及Android Studio自带的GPU监控功能。最后,文章给出了排查卡顿问题的四个方向,帮助开发者优化应用性能。
70 4
Android性能测试——发现和定位内存泄露和卡顿
|
4月前
|
Android开发 iOS开发
android自动化编译
android自动化编译
|
21天前
|
测试技术 Shell Android开发
Android 性能测试初探 (六)
本节聊聊性能测试的最后一项- 流量,当然我所指的性能测试是针对大部分应用而言的,可能还有部分应用会关注网速、弱网之类的测试,但本系列文章都不去一一探讨了。
37 6
|
21天前
|
JavaScript 测试技术 Android开发
Android 性能测试初探 (四)
本文介绍了GPU在移动端性能测试中的重要性,并详细解释了过度绘制、帧率和帧方差的概念。针对GPU测试,文章列举了三项主要测试内容:界面过度绘制、屏幕滑动帧速率和平滑度。其中,过度绘制测试需遵循特定标准,而帧速率和平滑度测试则可通过软件或硬件方法实现。在软件测试中,使用Systrace插件和高速相机是两种常用手段。对于不同机型,帧率及帧方差的测试标准也需相应调整。
32 5
|
21天前
|
测试技术 Shell Android开发
Android 性能测试初探 (三)
本文承接《Android性能测试初探(二)》,深入探讨CPU与内存测试。介绍了移动端内存测试的重要性及其测试目标,并详细列举了不同状态下应用内存消耗情况的测试项目。此外,还提供了多种内存测试方法,包括使用`procrank`等工具的具体操作步骤。最后,文章也简要提及了CPU测试的相关内容,帮助读者更好地理解Android性能测试的关键要素。
36 5
|
21天前
|
测试技术 Shell 定位技术
Android 性能测试初探 (五)
聊聊大家不常关注的测试项- 功耗
36 3
|
21天前
|
算法 测试技术 Android开发
Android 性能测试初探 (二)
上回大体介绍了下在 android 端的性能测试项,现在我们就细节测试项做一些阐述(包括如何自己 DIY 测试)
31 4
|
21天前
|
测试技术 API Android开发
Android 性能测试初探 (一)
Android 性能测试,跟pc性能测试一样分为客户端及服务器,但在客户端上的性能测试分为 2 类: 一类为 rom 版本的性能测试;一类为应用的性能测试。
39 3
|
1月前
|
Android开发
Android学习 —— 测试init.rc中的条件触发的处理顺序
Android学习 —— 测试init.rc中的条件触发的处理顺序
|
5月前
|
安全 物联网 测试技术
构建未来:Android与IoT设备的无缝交互深入探索软件自动化测试的未来趋势
【5月更文挑战第30天】在物联网(IoT)技术快速发展的当下,Android系统因其开放性和广泛的用户基础成为了连接智能设备的首选平台。本文将探讨如何通过现代Android开发技术实现智能手机与IoT设备的高效、稳定连接,并分析其中的挑战和解决方案。我们将深入挖掘Android系统的底层通信机制,提出创新的交互模式,并通过实例演示如何在Android应用中集成IoT控制功能,旨在为开发者提供一套可行的指导方案,促进IoT生态系统的进一步发展。
下一篇
无影云桌面