Android自动测试之monkeyrunner工具(2)

发表于:2012-04-16来源:不详作者:杨点击数: 标签:Android
110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at com.android.monkeyrunner.adb.AdbMonkeyDevice$1.run(AdbMonkeyDevice.java:89) 110307 15:33:22.718:S [pool-1-thre

  110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at com.android.monkeyrunner.adb.AdbMonkeyDevice$1.run(AdbMonkeyDevice.java:89)

  110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at java.util.concurrent.Executors$RunnableAdapter.call(UnknownSource)

  110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)

  110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at java.util.concurrent.FutureTask.run(Unknown Source)

  110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)

  110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at java.util.concurrent.ThreadPoolExecutor$Worker.run(UnknownSource)

  110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at java.lang.Thread.run(UnknownSource)

  110307 15:33:57.437:I [main] [com.android.monkeyrunner.MonkeyManager] Monkey Command: press KEYCODE_MENU.

  110307 15:33:59.171:I [main] [com.android.monkeyrunner.MonkeyManager] Monkey Command: quit.

  注:里面exception的提示我们可以忽略,因为我们可以看见 Monkey Command: press KEYCODE_MENU已经执行成功。

  4、 可以Android_Root\tools下查看生成的shot1.png的截图。

  六、实例扩展

  因为ApiDemos首页上按下MENU键没有菜单出现,为了更加形象化,在实例五的基础上继续试验:

  1、 在$Android_Root\tools下新建一个monkeyrunnerprogram1.py文件,里面内容为:

  1 # Imports the monkeyrunner modules used by this program 2 3 from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage 4 5 # Connects to the current device, returning a MonkeyDevice object 6 7 device = MonkeyRunner.waitForConnection() 8 9 # Takes a screenshot10 11 result = device.takeSnapshot()12 13 # Writes the screenshot to a file14 15 result.writeToFile('./shotbegin.png','png')16 17 # Presses the Down button18 19 device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')20 21 device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')22 23 device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')24 25 device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')26 27 device.press('KEYCODE_DPAD_DOWN','DOWN_AND_UP')28 29 # Takes a screenshot30 31 result = device.takeSnapshot()32 33 # Writes the screenshot to a file34 35 result.writeToFile('./shotend.png','png')

  2、 将画面定位在Apidemos的首页,并将光标定位在第一项上。

  3、 在$Android_Root\tools目录下运行一下命令:

  monkeyrunner monkeyrunnerprogram1.py

  4、在运行过程中我们可以看见光标不断向下移动,并且可以在当前目录下我们自定义的截图:

  运行前:shotbegin.png

  运行后(做了五次下移操作):shotend.png

原文转自:http://www.ltesting.net