|
在网上看了下使用monkey runner的步骤教程
都极不详细啊,在没人指导的情况下搞了很久还是弄不明白,现在这里求助各位大大,我看的步骤如下
1,安装好eclipse,python,sdk等,这一步没有问题
2,准备好测试例子(应该是需要工程源文件和apk文件吧,按教程,准备好sdk下sample下的ApiDemos;以及生成的ApiDemos.apk)
3,打开模拟器,(我的做法是在eclipse里面,打开ApiDemos这个项目,然后run as in andriod application,那么可以看到模拟器运行以来了)
现在疑问问题就在这里了,就这样打开模拟器就行了吗,monkey runner怎么知道我打开的这个模拟器就是它要去测试的呢?是不是应该有一步关联之类的操作
4,编写一个py文件,py代码在下面
5,进入命令行,切换到sdk下的tools目录,monkey runnerpy运行那个文件
预期结果是会生成一张截图在当前目录,我也没看到,始终感觉是第三步,连接模拟器的那一步不对,恳求各位大大指点,谢谢
参考的例子教程在下面
运行monkeyrunner
您可以直接使用一个代码文件运行monkeyrunner,抑或在交互式对话中输入monkeyrunner语句。不论使用哪种方式,您都需要调用SDK目录的tools子目录下的monkeyrunner命令。如果您提供一个文件名作为运行参数,则monkeyrunner将视文件内容为Python程序,并加以运行;否则,它将提供一个交互对话环境。
monkeyrunner的命令语法为:
monkeyrunner -plugin <plugin_jar> <program_filename> <program_options>
五、实例
以sample中的ApiDemos为例,先将其生成ApiDemos.apk。
前提:已有device连接
1、 将ApiDemos.apk放在$Android_Root\tools下。
2、 在$Android_Root\tools下新建一个monkeyrunnerprogram.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 # Installs the Android package. Notice that this method returns a boolean, so you can test10 11 # to see if the installation worked.12 13 device.installPackage('./ApiDemos.apk')14 15 16 # Runs the component17 18 device.startActivity(component='com.example.android.apis/.ApiDemos')19 20 21 # Presses the Menu button22 23 device.press('KEYCODE_MENU','DOWN_AND_UP')24 25 26 # Takes a screenshot27 28 result = device.takeSnapshot()29 30 31 # Writes the screenshot to a file32 33 result.writeToFile('./shot1.png','png')复制代码
注意:SDK上的例子有些错误,不可直接复制,否则执行命令时会发生错误。具体可与我的上面这段代码对照。
3、 打开命令行转到Android_Root\tools目录下运行一下命令:
monkeyrunner monkeyrunnerprogram.py
110307 15:33:19.625:I [main] [com.android.monkeyrunner.MonkeyManager] Monkey Command: wake.
110307 15:33:20.625:I [main] [com.android.monkeyrunner.MonkeyManager] Monkey Command: wake.
110307 15:33:21.625:I [main] [com.android.monkeyrunner.MonkeyManager] Monkey Command: wake.
110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] Error starting command: monkey --port 12345
110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice]com.android.ddmlib.ShellCommandUnresponsiveException
110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at com.android.ddmlib.AdbHelper.executeRemoteCommand(AdbHelper.java:408)
110307 15:33:22.718:S [pool-1-thread-1] [com.android.monkeyrunner.adb.AdbMonkeyDevice] at com.android.ddmlib.Device.executeShellCommand(Device.java:276)
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的截图。 |
|