Question

I have an app, for which i wanna run Monkeyrunner (using Android ViewClient)

I am trying to define my view as

Vc = ViewClient(device, serialno)
vc.dump
touchProject = vc.findViewByIdorRaise('id/projectNewGallery')
touchProject.touch()

But I am getting error : ->"com.dtmilano.android.viewclient.ViewNotFoundException: Couldn't find View with ID='id/projectNewGallery' in tree with root=ROOT"

How can i set root ? like this ?

  touchProject = vc.findViewByIdorRaise('id/projectNewGallery','id/projectMain')

? ///////////////////////////////////////////////////////////////////////////////////

I am trying to use culebra tool, But I am getting following error.

$ java -jar androidviewclient-2.3.16.jar culebra
ERROR: monkeyrunner was not found and Windows 7 does not support shebang in scripts.    Aborting.

I also tried this way (Non-Shebang OS -> Windows 7 )

$ /cygdrive/c/android-sdk/tools/monkeyrunner.bat -plugin /cygdrive/c/Android_Resources_Hassan/MonkeyRunner_KIneMaster/AndroidViewClient-master/AndroidViewClient/bin/androidviewclient-2.3.22.jar culebra myscript.py
Plugin file doesn't exist
Usage: monkeyrunner [options] SCRIPT_FILE

-s      MonkeyServer IP Address.
-p      MonkeyServer TCP Port.
-v      MonkeyServer Logging level (ALL, FINEST, FINER, FINE, CONFIG, INFO, WARNING, SEVERE, OFF)

Ok i was able to run it Windows CMD :

C:\android-sdk\tools>monkeyrunner -plugin C:\Android_Resources_Hassan\MonkeyRunner_KIneMaster\androidviewclient-2.3.24.jar test3_py.py

but I got following error again :

130619 14:41:15.725:S [MainThread] [com.android.monkeyrunner.MonkeyRunnerOptions]    Script terminated due to an exception
130619 14:41:15.725:S [MainThread]  [com.android.monkeyrunner.MonkeyRunnerOptions]Traceback (most recent call last):
File "C:\android-sdk\tools\test3_py.py", line 71, in <module>
touchProject = vc.findViewByIdOrRaise('id/projectNewGallery')
File "C:\Android_Resources_Hassan\MonkeyRunner_KIneMaster\AndroidViewClient- master\AndroidViewClient\src\com\dtmilano\android\viewclient.py", line 1919, in  findViewById
OrRaise raise ViewNotFoundException("ID", viewId, root)
com.dtmilano.android.viewclient.ViewNotFoundException: Couldn't find View with    ID='id/projectNewGallery' in tree with root=ROOT

my script file --------------------

#! /usr/bin/env monkeyrunner
'''
Copyright (C) 2012  Diego Torres Milano
Created on Feb 3, 2012

@author: diego
'''


import re
import sys
import os

# this must be imported before MonkeyRunner and MonkeyDevice,
# otherwise the import fails
try:
ANDROID_VIEW_CLIENT_HOME = os.environ['ANDROID_VIEW_CLIENT_HOME']
except KeyError:
print >>sys.stderr, "%s: ERROR: ANDROID_VIEW_CLIENT_HOME not set in environment" %    __file__
sys.exit(1)
sys.path.append(ANDROID_VIEW_CLIENT_HOME + '/src')

from com.dtmilano.android.viewclient import ViewClient


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

#starting script
print "start"

# Connects to the current device, returning a MonkeyDevice object
device, serialno = ViewClient.connectToDeviceOrExit()


#device connected
print "connection started"

# sets a variable with the package's internal name
package = 'com.example.app.myApp'

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

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

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

#device set to sleep for 4 seconds 
print "wait for 4 seconds"

 # first screen shot event
 MonkeyRunner.sleep(4);


 vc = ViewClient(device, serialno)
 vc.dump()
 root = vc.getRoot()

#touchProject = vc.findViewWithTextOrRaise('', root)
touchProject = vc.findViewByIdOrRaise('projectNewGallery')
touchProject.touch()


# wait for screenshot to save
MonkeyRunner.sleep(2);


# Takes a screenshot
result1 = device.takeSnapshot()
        result1.writeToFile('/myPath/shot1.png','png')


# wait for 3 seconds
MonkeyRunner.sleep(3);

is ID same as defined in XML android:id="+id/newProjectGallery" ?

////////////////////// AFTER adding vc.traverse() in code///////////////////

I am getting following error

130620 10:07:43.775:S [MainThread] [com.android.monkeyrunner.MonkeyRunnerOptions]    Script terminated due to an exception
130620 10:07:43.775:S [MainThread]   [com.android.monkeyrunner.MonkeyRunnerOptions]Traceback (most recent call last):
 File "C:\android-sdk\tools\test.py", line 71, in <module>
    ViewClient(*ViewClient.connectToDeviceOrExit()).traverse(transform=ViewClient.TRAVERSE_CIT)
  File "C:\Android_Resources_Hassan\MonkeyRunner\AndroidViewClient-  master\AndroidViewClient\src\com\dtmilano\android\viewclient.py", line 1687, in traverse
   print >>stream, "%s%s" % (indent, s)
 LookupError: unknown encoding 'ms949'

Moreover I ran dump-simple.py code also. But I am getting again the same error :

130620 10:07:43.775:S [MainThread] [com.android.monkeyrunner.MonkeyRunnerOptions]   Script terminated due to an exception
130620 10:07:43.775:S [MainThread]   [com.android.monkeyrunner.MonkeyRunnerOptions]Traceback (most recent call last): 
File "C:\android-sdk\tools\dump-simple.py", line 30, in <module>
  ViewClient(*ViewClient.connectToDeviceOrExit()).traverse(transform=ViewClient.TRAVERSE_CIT)
File "C:\Android_Resources_Hassan\MonkeyRunner\AndroidViewClient-  master\AndroidViewClient\src\com\dtmilano\android\viewclient.py", line 1687, in traverse
print >>stream, "%s%s" % (indent, s)
LookupError: unknown encoding 'ms949'

what is causing it ? Please help me ~

Thanks

Was it helpful?

Solution

I see a couple of errors in your snippet (check this corrected version):

vc = ViewClient(device, serialno)
vc.dump()
touchProject = vc.findViewByIdOrRaise('id/projectNewGallery')
touchProject.touch()

to simplify the process, you can just use culebra to generate the correct script template that you can, later on, adapt to your needs: When the device screen contains the desired Views:

$ culebra -VC -o myscript.py

edit myscript.py to add the call to the touch() method and run

$ myscript.py

The use of verbose comments (-C) simplifies the identification of Views if IDs are not available.

Update

  1. You were almost there in your first example, I guess the problem should be fixed by my corrected snippet before. The code run, the only problem may be you are expecting IDs that are not there. Remember, there are no IDs if the back-end is UiAutomator, which is the default back-end for API >= 16.

  2. The java runner expects monkeyrunner to be in the PATH.

  3. It seems /cygdrive/c/Android_Resources_Hassan/MonkeyRunner_KIneMaster/AndroidViewClient-master/AndroidViewClient/bin/androidviewclient-2.3.22.jar does not exist. At least this is what monkeyrunner thinks. Perhaps you should use \ in Windows paths.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top