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  






目录
相关文章
|
2月前
|
监控 安全 Android开发
【新手必读】Airtest测试Android手机常见的设置问题
【新手必读】Airtest测试Android手机常见的设置问题
|
6月前
|
测试技术 API 开发工具
在Python中实现安卓手机自动化
在Python中实现安卓手机自动化
495 0
|
2天前
|
Linux Shell Android开发
自动化脚本之GPIO/LED相关适用于Android/Linux
自动化脚本之GPIO/LED相关适用于Android/Linux
13 0
|
2天前
|
Java API Android开发
Android 11 修改libcore Cipher AS测试
Android 11 修改libcore Cipher AS测试
13 1
|
1月前
|
存储 Android开发 C++
【Android 从入门到出门】第五章:使用DataStore存储数据和测试
【Android 从入门到出门】第五章:使用DataStore存储数据和测试
34 3
|
1月前
|
存储 SQL 数据库
【Android 从入门到出门】第六章:使用Room数据库并测试
【Android 从入门到出门】第六章:使用Room数据库并测试
29 4
|
2月前
|
Java 测试技术 API
安卓APP和iOS APP在测试上的区别是什么?
安卓APP和iOS APP在测试上的区别是什么?
|
4月前
|
Linux 网络安全 开发工具
Android APP入门之Android的简介以及Android Studio开发环境的搭建和测试(2022最新 图文解释 简单易懂)
Android APP入门之Android的简介以及Android Studio开发环境的搭建和测试(2022最新 图文解释 简单易懂)
62 0
|
4月前
|
Java 测试技术 持续交付
百度搜索:蓝易云【NetMock简介:简化 Java,Android和Kotlin多平台中的HTTP请求测试?】
使用NetMock,您可以在单元测试、集成测试和端到端测试中轻松地模拟和验证HTTP请求和响应,而无需实际发送请求到外部服务。这样可以提高测试的可靠性和可控性,并加快测试执行的速度。无论是在开发过程中还是在持续集成环境中,NetMock都可以帮助您更轻松地进行HTTP请求测试,提高代码质量和稳定性。
41 1
|
4月前
|
移动开发 Shell 测试技术
Realtek蓝牙Android10.0移植结束后的基本测试和常见问题分析
Realtek蓝牙Android10.0移植结束后的基本测试和常见问题分析

热门文章

最新文章