【问题标题】:Monkeyrunner on Ubuntu 16.04 to install android apk fileMonkeyrunner 在 Ubuntu 16.04 上安装 android apk 文件
【发布时间】:2017-02-08 22:26:26
【问题描述】:

我在 Ubuntu 16.04 上安装了 Android SDK,它在我的笔记本电脑上的 Virtual Box 上运行。我有一个连接到笔记本电脑的三星 Galaxy Tab。虚拟机可以访问该设备。我尝试安装我的安卓应用 (apk file) 使用monkeyrunner。

我使用的python脚本如下

# Imports the monkeyrunner modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice

# Connects to the current device, returning a MonkeyDevice object
device = MonkeyRunner.waitForConnection()

# Installs the Android package. Notice that this method returns a boolean, so you can test
# to see if the installation worked.
device.installPackage('myproject/bin/MyApplication.apk')

# sets a variable with the package's internal name
package = 'com.example.android.myapplication'

# sets a variable with the name of an Activity in the package
activity = 'com.example.android.myapplication.MainActivity'

# sets the name of the component to start
runComponent = package + '/' + activity

# Runs the component
device.startActivity(component=runComponent)

# Presses the Menu button
device.press('KEYCODE_MENU', MonkeyDevice.DOWN_AND_UP)

# Takes a screenshot
result = device.takeSnapshot()

# Writes the screenshot to a file
result.writeToFile('myproject/shot1.png','png')

我以

身份运行脚本
monkeyrunner -v ALL ~/Workspace/monkey_runner_sample.py

我得到以下输出,但没有任何反应。程序有点挂起。

170209 11:19:27.878:I [main] [com.android.chimpchat.ChimpManager] Monkey Command: wake.
170209 11:19:31.146:S [pool-1-thread-1] [com.android.chimpchat.adb.AdbChimpDevice] Error starting command: monkey --port 12345
170209 11:19:31.146:S [pool-1-thread-1] [com.android.chimpchat.adb.AdbChimpDevice]com.android.ddmlib.TimeoutException
170209 11:19:31.146:S [pool-1-thread-1] [com.android.chimpchat.adb.AdbChimpDevice]  at com.android.ddmlib.AdbHelper.read(AdbHelper.java:766)
170209 11:19:31.146:S [pool-1-thread-1] [com.android.chimpchat.adb.AdbChimpDevice]  at com.android.ddmlib.AdbHelper.read(AdbHelper.java:735)
170209 11:19:31.146:S [pool-1-thread-1] [com.android.chimpchat.adb.AdbChimpDevice]  at com.android.ddmlib.AdbHelper.readAdbResponse(AdbHelper.java:221)
170209 11:19:31.146:S [pool-1-thread-1] [com.android.chimpchat.adb.AdbChimpDevice]  at com.android.ddmlib.AdbHelper.executeRemoteCommand(AdbHelper.java:455)
170209 11:19:31.146:S [pool-1-thread-1] [com.android.chimpchat.adb.AdbChimpDevice]  at com.android.ddmlib.AdbHelper.executeRemoteCommand(AdbHelper.java:381)
170209 11:19:31.146:S [pool-1-thread-1] [com.android.chimpchat.adb.AdbChimpDevice]  at com.android.ddmlib.AdbHelper.executeRemoteCommand(AdbHelper.java:352)
170209 11:19:31.146:S [pool-1-thread-1] [com.android.chimpchat.adb.AdbChimpDevice]  at com.android.ddmlib.Device.executeShellCommand(Device.java:604)
170209 11:19:31.146:S [pool-1-thread-1] [com.android.chimpchat.adb.AdbChimpDevice]  at com.android.chimpchat.adb.AdbChimpDevice$1.run(AdbChimpDevice.java:105)
170209 11:19:31.146:S [pool-1-thread-1] [com.android.chimpchat.adb.AdbChimpDevice]  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
170209 11:19:31.146:S [pool-1-thread-1] [com.android.chimpchat.adb.AdbChimpDevice]  at java.util.concurrent.FutureTask.run(FutureTask.java:266)
170209 11:19:31.146:S [pool-1-thread-1] [com.android.chimpchat.adb.AdbChimpDevice]  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
170209 11:19:31.146:S [pool-1-thread-1] [com.android.chimpchat.adb.AdbChimpDevice]  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
170209 11:19:31.146:S [pool-1-thread-1] [com.android.chimpchat.adb.AdbChimpDevice]  at java.lang.Thread.run(Thread.java:745)

应用程序MyApplication 未安装在平板电脑上。

错误如下所示

170209 11:27:07.692:S [pool-1-thread-1] [com.android.chimpchat.adb.AdbChimpDevice] Error starting command: monkey --port 12345
170209 11:27:07.692:S [pool-1-thread-1] [com.android.chimpchat.adb.AdbChimpDevice]com.android.ddmlib.TimeoutException

谁能帮我弄清楚发生了什么?

【问题讨论】:

    标签: android python adb apk monkeyrunner


    【解决方案1】:

    您可以使用AndroidViewClient/culebra,只需对原始monkeyrunner 脚本进行一些更改,即可运行它。

    基本脚本由culebra 生成,这始终是一个很好的起点。

    #! /usr/bin/env python
    # -*- coding: utf-8 -*-
    '''
    Copyright (C) 2013-2016  Diego Torres Milano
    Created on 2017-02-08 by Culebra v12.5.3
                          __    __    __    __
                         /  \  /  \  /  \  /  \ 
    ____________________/  __\/  __\/  __\/  __\_____________________________
    ___________________/  /__/  /__/  /__/  /________________________________
                       | / \   / \   / \   / \   \___
                       |/   \_/   \_/   \_/   \    o \ 
                                               \_____/--<
    @author: Diego Torres Milano
    @author: Jennifer E. Swofford (ascii art snake)
    '''
    
    
    import re
    import sys
    import os
    
    
    from com.dtmilano.android.viewclient import ViewClient
    from com.dtmilano.android.adb.adbclient import DOWN_AND_UP
    
    kwargs1 = {'ignoreversioncheck': False, 'verbose': False, 'ignoresecuredevice': False}
    device, serialno = ViewClient.connectToDeviceOrExit(**kwargs1)
    kwargs2 = {'forceviewserveruse': False, 'useuiautomatorhelper': False, 'ignoreuiautomatorkilled': True, 'autodump': False, 'startviewserver': True, 'compresseddump': True}
    vc = ViewClient(device, serialno, **kwargs2)
    
    # Installs the Android package. Notice that this method returns a boolean, so you can test
    # to see if the installation worked.
    vc.installPackage('myproject/bin/MyApplication.apk')
    
    # sets a variable with the package's internal name
    package = 'com.example.android.myapplication'
    
    # sets a variable with the name of an Activity in the package
    activity = 'com.example.android.myapplication.MainActivity'
    
    # sets the name of the component to start
    runComponent = package + '/' + activity
    
    # Runs the component
    device.startActivity(component=runComponent)
    
    # Presses the Menu button
    device.press('KEYCODE_MENU', DOWN_AND_UP)
    
    # Writes the screenshot to a file (you can use a plain filename or use these placeholders)
    vc.writeImageToFile('/tmp/${serialno}-${focusedwindowname}-${timestamp}.png', 'PNG')
    

    【讨论】:

    猜你喜欢
    • 2017-04-12
    • 2018-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多