0

I'm beginner in monkeyrunner and python, I want to read logcat logs and after processing the logs, send feedback to app by calling press or touch methods, the reading section and press sections work fine separately, but when they comes together problem appears!

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
import commands
import sys
import string
import subprocess

device = MonkeyRunner.waitForConnection(10,'emulator-5554')
proc = subprocess.Popen(['adb -e logcat'],shell=True, stdout=subprocess.PIPE)
i = 0
while 1:
    for line in proc.stdout:       
        #print "test:", line.rstrip()
        if line.find('Corona')!=-1:     
            print line
            device.press('KEYCODE_BACK', MonkeyDevice.DOWN_AND_UP)

error:

    151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice] Er
ror sending press event: KEYCODE_BACK DOWN_AND_UP
151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]jav
a.net.SocketException: Connection reset by peer: socket write error
151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]
at java.net.SocketOutputStream.socketWrite0(Native Method)
151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]
at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]
at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)
151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]
at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:291)
151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]
at sun.nio.cs.StreamEncoder.implFlush(StreamEncoder.java:295)
151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]
at sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:141)
151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]
at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:229)
151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]
at java.io.BufferedWriter.flush(BufferedWriter.java:254)
151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]
at com.android.chimpchat.ChimpManager.sendMonkeyEventAndGetResponse(ChimpManager
.java:187)
151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]
at com.android.chimpchat.ChimpManager.sendMonkeyEvent(ChimpManager.java:235)
151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]
at com.android.chimpchat.ChimpManager.press(ChimpManager.java:135)
151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]
at com.android.chimpchat.adb.AdbChimpDevice.press(AdbChimpDevice.java:327)
151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]
at com.android.monkeyrunner.MonkeyDevice.press(MonkeyDevice.java:196)
151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]
at sun.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)
151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.
java:43)
151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]
at java.lang.reflect.Method.invoke(Method.java:497)
151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]
at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:186)
151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]
at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:204)
151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]
at org.python.core.PyObject.__call__(PyObject.java:422)
151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]
at org.python.core.PyObject.__call__(PyObject.java:426)
151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]
at org.python.core.PyMethod.__call__(PyMethod.java:139)
151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]
at org.python.pycode._pyx0.f$0(C:\adt\adt\sdk\tools\test11111.py:13)
151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]
at org.python.pycode._pyx0.call_function(C:\adt\adt\sdk\tools\test11111.py)
151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]
at org.python.core.PyTableCode.call(PyTableCode.java:165)
151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]
at org.python.core.PyCode.call(PyCode.java:18)
151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]
at org.python.core.Py.runCode(Py.java:1275)
151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]
at org.python.core.__builtin__.execfile_flags(__builtin__.java:522)
151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]
at org.python.util.PythonInterpreter.execfile(PythonInterpreter.java:225)
151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]
at com.android.monkeyrunner.ScriptRunner.run(ScriptRunner.java:116)
151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]
at com.android.monkeyrunner.MonkeyRunnerStarter.run(MonkeyRunnerStarter.java:77)

151224 02:36:53.349:S [MainThread] [com.android.chimpchat.adb.AdbChimpDevice]
at com.android.monkeyrunner.MonkeyRunnerStarter.main(MonkeyRunnerStarter.java:18
9)

3 Answers3

1

I always got this error message when I'd close the CMD window that's running the batch file, that's calling the monkeyrunner python script. If I let the bat file fully finish every time, the next time I'd run the script I wouldn't see this issue.

I found another way to avoid that initial "(...)Error sending press event: (...)" error. And it works every time.

In my .bat file, if I CALL a dummy file first (i.e. prep.py), before my actual script, the adb connection issue sorts itself out.

cd C:\Users\%USERNAME%\AppData\Local\Android\sdk\tools
CALL monkeyrunner.bat C:\Users\%USERNAME%\Desktop\MonkeyRunner\prep.py
CALL monkeyrunner.bat C:\Users\%USERNAME%\Desktop\MonkeyRunner\monkeyRunnerScript1.py

What I mean by "dummy file" (prep.py), is a file that has only the monkeyrunner essentials. Like this...

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice

device = MonkeyRunner.waitForConnection()
device.installPackage('myproject/bin/MyApplication.apk')
package = 'com.example.android.myapplication'
activity = 'com.example.android.myapplication.MainActivity'
runComponent = package + '/' + activity
device.startActivity(component=runComponent)
device.press('KEYCODE_MENU', MonkeyDevice.DOWN_AND_UP)

monkeyrunner.sleep(1)
device.press("KEYCODE_HOME", MonkeyDevice.DOWN_AND_UP) #HOME button
print("Pressed HOME button.")
monkeyrunner.sleep(1)
device.drag((800,1300),(800,300),0.15,5) #swipe up to scroll down Routes list
print("Swiped Up to open Tablet.")
monkeyrunner.sleep(1)
device.press("KEYCODE_POWER", MonkeyDevice.DOWN_AND_UP) #Power button
print("Fake script is done.")
print("Now turning OFF tablet for actual script.")
print("Please standby...")
0

You can try AndroidViewClient/culebra and with barely no changes your script should work.

You can do as I did to test it, run

$ culebra -uo myscript.py

Edit myscript.py to remove what's not needed (i.e. vc) and add your code at the end.

#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
Copyright (C) 2013-2014  Diego Torres Milano
Created on 2015-12-24 by Culebra v11.0.9
                      __    __    __    __
                     /  \  /  \  /  \  /  \ 
____________________/  __\/  __\/  __\/  __\_____________________________
___________________/  /__/  /__/  /__/  /________________________________
                   | / \   / \   / \   / \   \___
                   |/   \_/   \_/   \_/   \    o \ 
                                           \_____/--<
@author: Diego Torres Milano
@author: Jennifer E. Swofford (ascii art snake)
'''


import re
import sys
import os
import subprocess


try:
    sys.path.insert(0, os.path.join(os.environ['ANDROID_VIEW_CLIENT_HOME'], 'src'))
except:
    pass

from com.dtmilano.android.viewclient import ViewClient

TAG = 'CULEBRA'

_s = 5
_v = '--verbose' in sys.argv


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}

proc = subprocess.Popen(['adb -e logcat'],shell=True, stdout=subprocess.PIPE)
i = 0
while 1:
    for line in proc.stdout:
        if line.find('Corona')!=-1:     
            print line
            device.press('KEYCODE_BACK')

However, I think you should start a new thread to read from logcat.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • I used 2 thread at first and I think multithreading causes the problem, so for test I write the code snippet in the question but nothing changes. I consider to use AndroidViewClient/culebra but I cant find an easy way to send touch to x,y coordinate of screen(the app is Corona(openGL) and has only one View!) – Saeid Moemen Dec 24 '15 at 20:03
0

I found a way to get logs from logcat without using adb and problem solved.

  • install udplogcat app on device, set (ip,port) and start service.
  • recieve logs in python(monkeyrunner) from udp socket