MonkeyRunner_采用MonkeyRunner自动化测试

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 16:33   1959   0

转载自:http://blog.csdn.net/vrix/article/details/6893944

既然是自动化测试,而且又需要写脚本,那是不是可以自动生成测试脚本呢?带着这个问题,我找到了下面的代码 monkeyrecoder.py

  1. #!/usr/bin/envmonkeyrunner
  2. # Copyright 2010, TheAndroid Open Source Project
  3. #
  4. # Licensed under theApache License, Version 2.0 (the "License");
  5. # you may not usethis file except in compliance with the License.
  6. # You may obtain acopy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required byapplicable law or agreed to in writing, software
  11. # distributed underthe License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIESOR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License forthe specific language governing permissions and
  14. # limitations underthe License.
  15. fromcom.android.monkeyrunner import MonkeyRunner as mr
  16. fromcom.android.monkeyrunner.recorder import MonkeyRecorder as recorder
  17. device =mr.waitForConnection()
  18. recorder.start(device)

首先,连接你已经打开调试模式的ANDROID设备,然后运行上面的脚本 monkeyrunner.bat monkeyrecoder.py

执行下面的代码后,将运行录制脚本的程序,截图如下:

这个软件就可以生成脚本。但是这个软件生成的脚本不是monkeyrunner可以直接运行的

TOUCH|{'x':243,'y':746,'type':'downAndUp',}
TOUCH|{'x':244,'y':763,'type':'downAndUp',}
DRAG|{'start':(384,320),'end':(76,320),'duration':1.0,'steps':10,}

这种脚本需要另外一个monkeyrunner的脚本来解释执行。monkeyplayback.py

  1. #!/usr/bin/envmonkeyrunner
  2. # Copyright 2010, TheAndroid Open Source Project
  3. #
  4. # Licensed under theApache License, Version 2.0 (the "License");
  5. # you may not usethis file except in compliance with the License.
  6. # You may obtain acopy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required byapplicable law or agreed to in writing, software
  11. # distributed underthe License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIESOR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License forthe specific language governing permissions and
  14. # limitations underthe License.
  15. import sys
  16. fromcom.android.monkeyrunner import MonkeyRunner
  17. # The format of thefile we are parsing is very carfeully constructed.
  18. # Each linecorresponds to a single command. Theline is split into 2
  19. # parts with a |character. Text to the left of the pipedenotes
  20. # which command torun. The text to the right of the pipeis a python
  21. # dictionary (it canbe evaled into existence) that specifies the
  22. # arguments for thecommand. In most cases, this directlymaps to the
  23. # keyword argumentdictionary that could be passed to the underlying
  24. # command.
  25. # Lookup table to mapcommand strings to functions that implement that
  26. # command.
  27. CMD_MAP = {
  28. 'TOUCH': lambda dev, arg: dev.touch(**arg),
  29. 'DRAG': lambda dev, arg: dev.drag(**arg),
  30. 'PRESS': lambda dev, arg: dev.press(**arg),
  31. 'TYPE': lambda dev, arg: dev.type(**arg),
  32. 'WAIT': lambda dev, arg:MonkeyRunner.sleep(**arg)
  33. }
  34. # Process a singlefile for the specified device.
  35. def process_file(fp,device):
  36. for line in fp:
  37. (cmd, rest) = line.split('|')
  38. try:
  39. # Parse the pydict
  40. rest = eval(rest)
  41. except:
  42. print 'unable to parse options'
  43. continue
  44. if cmd not in CMD_MAP:
  45. print 'unknown command: ' + cmd
  46. continue
  47. CMD_MAP[cmd](device, rest)
  48. def main():
  49. file = sys.argv[1]
  50. fp = open(file, 'r')
  51. device = MonkeyRunner.waitForConnection()
  52. process_file(fp, device)
  53. fp.close();
  54. if __name__ =='__main__':
  55. main()

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP