Monkey Runner throws a broken pipe exception socket on touuch

Sometimes I see an error below when running monkeyrunner scripts. 140501 17: 01: 58.950: S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice] Error sending touch event: 500 515 DOWN_AND_UP 140501 17: 01: 58.950: S [MainThread] [com.android.chimpchat.adb. AdbChimpDevice] java.net.SocketException: idle pipe 140501 17: 01: 58.950: S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice] in java.net.SocketOutputStream.socketWrite0 (native method) 140501 17: 01: 58.950 : S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice] on java.net.SocketOutputStream.socketWrite (SocketOutputStream.java:92)

How do I catch them? There is a message suggesting to use SocketException from java.net import SocketException

However this does not work

+2
source share
3 answers

This may not be the most elegant solution, but here's what I came up with.

Since the problem is that when you kill the monkey script, the process on the Android device does not clean up properly, so when you try to reconnect, you get a problem with the channel. You can kill the -9 monkey process on the device itself and you will no longer run into it.

I have two scenarios that work around the problem here. This is python:

3 from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice 4 from time import sleep 5 import sys 6 import signal 7 import subprocess 8 9 device = None 10 11 def execute(): 12 device = MonkeyRunner.waitForConnection() 13 #my code here 17 18 def exitGracefully(signum, frame): 19 print "Exiting Gracefully..." 20 subprocess.call(['./killmonkey.sh']) 21 sys.exit(1) 22 23 if __name__ == '__main__': 24 signal.signal(signal.SIGINT, exitGracefully) 25 execute() 

And the killer monkey script:

 #!/bin/bash var=$(adb shell ps | grep monkey | awk '{print $2}') echo $var adb shell kill -9 $var 

When I ctrl + c monkey python script, it calls killmonkey, which kills the process on the connected Android device.

This specifically works only if only one device is connected, as it does not indicate the device identifier. Not very elegant, but I find it effective.

+2
source

It is impossible to catch him as an exception. You can't even use stderr .

If you really want to try to catch it even improperly, you need to get into the Java log:

 from java.util.Logging import Level, Logger, StreamHandler, SimpleFormatter from java.io import ByteArrayOutputStream device = # your code to get the device object errors = ByteArrayOutputStream(100) logger = Logger.getLogger('com.android.chimpchat.adb.AdbChimpDevice') logger.addHandler(StreamHandler(errors, SimpleFormatter())) device.touch(120, 120, 'DOWN_AND_UP') if errors.size() > 0: # any code you want raise YourException 

To catch any other Java error in MonkeyRunner, you must change the TAG in Logger.getLogger(TAG) to a class that prints a log.

To really get around this problem, you can also try: How to catch SocketExceptions in MonkeyRunner?

0
source

I really like Brian’s answer, but I didn’t like supporting another file just for this, so I ran the commands directly. Seems to work so far.

 from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice import sys import signal import subprocess device = None def execute(): device = MonkeyRunner.waitForConnection() #my code here def exitGracefully(signum, frame): """ Kill monkey on the device or future connections will fail """ print "Exiting Gracefully..." try: subprocess.call("adb shell kill -9 $(adb shell ps | grep monkey | awk '{print $2}')", shell=True) except Exception, e: print(e) sys.exit(1) if __name__ == '__main__': signal.signal(signal.SIGINT, exitGracefully) execute() 
0
source

Source: https://habr.com/ru/post/924235/


All Articles