使用MonkeyRunner时,我经常会遇到如下错误:
120830 18:39:32.755:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice] Unable to get variable: display.density
120830 18:39:32.755:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]java.net.SocketException: Connection reset
根据我的阅读,有时adb连接变坏,你需要重新连接。唯一的问题是,我无法抓住SocketException
。我会像这样包装我的代码:
try:
density = self.device.getProperty('display.density')
except:
print 'This will never print.'
但是这个例外显然没有一直提到调用者。我已经验证了MonkeyRunner / jython可以按照我期望的方式捕获Java异常:
>>> from java.io import FileInputStream
>>> def test_java_exceptions():
... try:
... FileInputStream('bad mojo')
... except:
... print 'Caught it!'
...
>>> test_java_exceptions()
Caught it!
如何处理这些套接字异常?
答案 0 :(得分:7)
每次启动MonkeyRunner都会出现错误,因为当脚本停止时,设备上的monkey --port 12345
命令不会停止。这是猴子的一个错误。
解决此问题的一种更好方法是在SIGINT
发送到您的脚本时(当您ctrl+c
时)杀死猴子。换句话说:$ killall com.android.commands.monkey
。
快速做到这一点:
from sys, signal
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
device = None
def execute():
device = MonkeyRunner.waitForConnection()
# your code
def exitGracefully(self, signum, frame):
signal.signal(signal.SIGINT, signal.getsignal(signal.SIGINT))
device.shell('killall com.android.commands.monkey')
sys.exit(1)
if __name__ == '__main__'
signal.signal(signal.SIGINT, exitGracefully)
execute()
编辑: 作为附录,我还发现了一种注意Java错误的方法:Monkey Runner throwing socket exception broken pipe on touuch
答案 1 :(得分:2)
以下是我最终使用的解决方法。任何可能遭受adb故障的功能只需要使用以下装饰器:
from subprocess import call, PIPE, Popen
from time import sleep
def check_connection(f):
"""
adb is unstable and cannot be trusted. When there's a problem, a
SocketException will be thrown, but caught internally by MonkeyRunner
and simply logged. As a hacky solution, this checks if the stderr log
grows after f is called (a false positive isn't going to cause any harm).
If so, the connection will be repaired and the decorated function/method
will be called again.
Make sure that stderr is redirected at the command line to the file
specified by config.STDERR. Also, this decorator will only work for
functions/methods that take a Device object as the first argument.
"""
def wrapper(*args, **kwargs):
while True:
cmd = "wc -l %s | awk '{print $1}'" % config.STDERR
p = Popen(cmd, shell=True, stdout=PIPE)
(line_count_pre, stderr) = p.communicate()
line_count_pre = line_count_pre.strip()
f(*args, **kwargs)
p = Popen(cmd, shell=True, stdout=PIPE)
(line_count_post, stderr) = p.communicate()
line_count_post = line_count_post.strip()
if line_count_pre == line_count_post:
# the connection was fine
break
print 'Connection error. Restarting adb...'
sleep(1)
call('adb kill-server', shell=True)
call('adb start-server', shell=True)
args[0].connection = MonkeyRunner.waitForConnection()
return wrapper
因为这可能会创建新连接,所以您需要将当前连接包装在Device对象中,以便可以更改它。这是我的Device类(大多数类是为了方便,唯一需要的是connection
成员:
class Device:
def __init__(self):
self.connection = MonkeyRunner.waitForConnection()
self.width = int(self.connection.getProperty('display.width'))
self.height = int(self.connection.getProperty('display.height'))
self.model = self.connection.getProperty('build.model')
def touch(self, x, y, press=MonkeyDevice.DOWN_AND_UP):
self.connection.touch(x, y, press)
关于如何使用装饰器的示例:
@check_connection
def screenshot(device, filename):
screen = device.connection.takeSnapshot()
screen.writeToFile(filename + '.png', 'png')