How to touch/press a button/View using AndroidViewClient? - android

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

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
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.
The java runner expects monkeyrunner to be in the PATH.
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.

Related

Androidviewclient valueError while running

I have generated the template using command $ culebra -U -o mytest.py and using for com.csr.csrmeshdemo:id/powerSwitch to turn on and off the switch.
It worked fine for first time but after restarting system for another issue I am unable to run my python script. getting error like this:
*root#parameshwar:~/Desktop/monkey/adt-bundle-linux-x86_64-20131030/sdk/CSR_script# adb devices
List of devices attached
T00940ZEIM device
root#parameshwar:~/Desktop/monkey/adt-bundle-linux-x86_64-20131030/sdk/CSR_script# python t6.py
E
======================================================================
ERROR: testSomething (__main__.CulebraTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "t6.py", line 44, in testSomething
self.vc.dump(window='-1')
File "/usr/local/lib/python2.7/dist-packages/androidviewclient-11.5.6-py2.7.egg/com/dtmilano/android/viewclient.py", line 3270, in dump
self.setViewsFromUiAutomatorDump(received)
File "/usr/local/lib/python2.7/dist-packages/androidviewclient-11.5.6-py2.7.egg/com/dtmilano/android/viewclient.py", line 2927, in setViewsFromUiAutomatorDump
self.__parseTreeFromUiAutomatorDump(received)
File "/usr/local/lib/python2.7/dist-packages/androidviewclient-11.5.6-py2.7.egg/com/dtmilano/android/viewclient.py", line 3109, in __parseTreeFromUiAutomatorDump
raise ValueError("received does not contain valid XML data")
ValueError: received does not contain valid XML data
----------------------------------------------------------------------
Ran 1 test in 2.508s
FAILED (errors=1)*
Please help me with this..
Latest AndroidViewClient/culebra 11.5.7 adds more verbose information about the exception and would let you trace the root cause easily.

Android Nexus 10 AOSP build AndroidViewClient

I finally managed to successfully build a Nexus 10 image (full_manta-userdebug) with Google Mobile Services installed but unfortunately, I'm unable to use my MonkeyRunner script that uses AndroidViewClient. It crashes out of the script with the following:
/system/bin/sh: uiautomator: not found
130628 14:12:41.242:S [MainThread] [com.android.monkeyrunner.MonkeyRunnerOptions] Script terminated due to an exception
130628 14:12:41.242:S [MainThread] [com.android.monkeyrunner.MonkeyRunnerOptions]Traceback (most recent call last):
File "/home/allen/projects/cts-scripts/4.2/cts-setup.py", line 361, in <module>
main()
File "/home/allen/projects/cts-scripts/4.2/cts-setup.py", line 339, in main
vc = ViewClient(device, serialno)
File "/home/allen/projects/AndroidViewClient/AndroidViewClient/src/com/dtmilano/android/viewclient.py", line 1188, in __init__
self.dump()
File "/home/allen/projects/AndroidViewClient/AndroidViewClient/src/com/dtmilano/android/viewclient.py", line 1780, in dump
self.setViewsFromUiAutomatorDump(received)
File "/home/allen/projects/AndroidViewClient/AndroidViewClient/src/com/dtmilano/android/viewclient.py", line 1530, in setViewsFromUiAutomatorDump
self.__parseTreeFromUiAutomatorDump(received)
File "/home/allen/projects/AndroidViewClient/AndroidViewClient/src/com/dtmilano/android/viewclient.py", line 1688, in _ViewClient__parseTreeFromUiAutomatorDump
self.root = parser.Parse(receivedXml)
File "/home/allen/projects/AndroidViewClient/AndroidViewClient/src/com/dtmilano/android/viewclient.py", line 988, in Parse
parserStatus = parser.Parse(uiautomatorxml, 1)
File "/home/allen/android/android-sdks/tools/lib/jython-standalone-2.5.4-rc1.jar/Lib/xml/parsers/expat.py", line 212, in Parse
xml.parsers.expat.ExpatError: Content is not allowed in prolog.
Do I need to install the ViewServer for this to work or did I forget to include the uiautomator backend when I built the image?
Try uiautomator from command line to find out if it's there or not:
$ adb shell uiautomator
if your output is similar to
Usage: uiautomator <subcommand> [options]
Available subcommands:
help: displays help message
...
then you have uiautomator installed. Otherwise if the output is
/system/bin/sh: uiautomator: not found
then you forgot to include it in the image.
You don't need ViewServer if UiAutomator is used as AndroidViewClient back-end.

Android MonkeyRunner exception

I'm trying to run MonkeyRunner Android test tool from Eclipse (4.2).
I installed Eclipse PyDev plugin, as well as Jython 2.5.1.
My PyDev interpreter is set to Jython / 2.5.
I created a Jython project in Eclipse with a simple jython file:
# 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()
Now when I launch it from Eclipse (project > run as JythonProject), I'me getting the following exception:
Traceback (most recent call last):
File "C:\svnrepository\trunk\JythonProject\src\main.py", line 10, in <module>
device = MonkeyRunner.waitForConnection()
at com.android.monkeyrunner.MonkeyRunner.waitForConnection(MonkeyRunner.java:75)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
java.lang.NullPointerException: java.lang.NullPointerException
Any idea what can be wrong?
There is a post of Diego Torres Milano how to configure PyDev to use it with monkeyrunner. I followed his steps and in my case everything works fine.
In brief, at first you need to change your Android installation. You need to rename your monkeyrunner tool to something monkeyrunner-original. Then you need to write in the same folder shell script (name it monkeyrunner) that will invoke monkeyrunner-original in the specific manner.
Script for Linux:
# /bin/bash
if [ "$1" = '-u' ]
then
shift
fi
exec /opt/android-sdk-linux_86/tools/monkeyrunner-original "$#"
Script for Windows:
REM
#echo off if("%1")==("-u") shift "C:\Program Files\Android\android-sdk\tools"/monkeyrunner-original %1 %2 %3 %4 %5 %6 %7 %8
Then in Eclipse, you should define new Python interpreter (not Jython) and point it to the created script.

why is monkeyrunner not working when run from a remote machine?

I need to run a monkeyrunner script in a remote machine. I'm using python to to automate it and RPyC so that I could connect to other machines, everything is running in CentOS.
written below is the command that I used:
import rpyc
import subprocess
conn = rpyc.classic.connect("192.XXX.XXX.XXX",XXXXX)
conn.execute ("print 'Hello'")
subprocess.Popen("/opt/android-sdk/tools/monkeyrunner -v ALL
/opt/android-sdk/tools/MYSCRIPT.py", shell=True)
and this is the result:
can't open specified script file
Usage : monkeyrunner [option] script_file
-s MonkeyServer IP Address
-p MonkeyServer TCP Port
-v MonkeyServer Logging level
And then I realized that if you use the command below, it is running the command in your machine. (example: the command inside the Popen is "ls" the result that it will give you is the list of files and directories in the current directory of the LOCALHOST) hence, the command is wrong.
subprocess.Popen("/opt/android-sdk/tools/monkeyrunner -v ALL
/opt/android-sdk/tools/MYSCRIPT.py", shell=True)
and so I replaced the code with this
conn.modules.subprocess.Popen("/opt/android-sdk/tools/monkeyrunner -v ALL
/opt/android-sdk/tools/MYSCRIPT.py", shell=True)
And give me this error message
======= Remote traceback ======= Traceback (most recent call last): File
"/usr/lib/python2.4/site-packages/rpyc-3.2.2-py2.4.egg/rpyc/core/protocol.py",
line 300, in _dispatch_request
res = self._HANDLERS[handler](self, *args) File "/usr/lib/python2.4/site-packages/rpyc-3.2.2-py2.4.egg/rpyc/core/protocol.py",
line 532, in _handle_call
return self._local_objects[oid](*args, **dict(kwargs)) File "/usr/lib/python2.4/subprocess.py", line 542, in init
errread, errwrite) File "/usr/lib/python2.4/subprocess.py", line 975, in _execute_child
raise child_exception OSError: [Errno 2] No such file or directory
======= Local exception ======== Traceback (most recent call last): File "", line 1, in ? File
"/usr/lib/python2.4/site-packages/rpyc-3.2.2-py2.4.egg/rpyc/core/netref.py",
line 196, in call
return syncreq(_self, consts.HANDLE_CALL, args, kwargs) File "/usr/lib/python2.4/site-packages/rpyc-3.2.2-py2.4.egg/rpyc/core/netref.py",
line 71, in syncreq
return conn.sync_request(handler, oid, *args) File "/usr/lib/python2.4/site-packages/rpyc-3.2.2-py2.4.egg/rpyc/core/protocol.py",
line 438, in sync_request
raise obj OSError: [Errno 2] No such file or directory
I am thinking that it cannot run the file because I don't have administrator access (since I didn't supply the username and password of the remote machine)?
Help!
As i see you are already connected
conn = rpyc.classic.connect("192.XXX.XXX.XXX",XXXXX)
conn.execute ("print 'Hello'")
Try to use next command:
subprocess.Popen("/opt/android-sdk/tools/monkeyrunner
/opt/android-sdk/tools/MYSCRIPT.py", shell=True)
instead of:
subprocess.Popen("/opt/android-sdk/tools/monkeyrunner -v ALL
/opt/android-sdk/tools/MYSCRIPT.py", shell=True)
Using this function to run monekyrunner doesn't work although running ls, pwd works fine.
conn.modules.subprocess.Popen("/opt/android-sdk/tools/monkeyrunner -v
ALL
/opt/android-sdk/tools/MYSCRIPT.py", shell=True)
The chunk of code below solved my problem :
import rpyc
import subprocess , os
conn = rpyc.classic.connect("192.XXX.XXX.XXX",XXXXX)
conn.execute ("print 'Hello'")
conn.modules.os.popen("monkeyrunner -v ALL MYSCRIPT.py",)
Hope this helps to those who are experiencing the same problem as mine.

android monkey runner scripts

I was trying to run a a sample python program using monkey runner but unfortunately throwing an error of this type :
Can't open specified script file
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)
Exception in thread "main" java.lang.NullPointerException
so any one can guide me how to resolve this and how to use monkey runner to execute this type of things
I've found that making the path to the script absolute helped monkeyrunner.
I am invoking the runner from a Python script where a helper class has a startMonkey method:
def startMonkey(self, monkeyScript):
command = [ self.env + '\\tools\\monkeyrunner.bat' ]
command.append( os.path.abspath( monkeyScript ) )
return startProcess( command )
This seems to be working for me.
Script file should be a full path file name try below monkeyrunner c:\test_script\first.py
So go to the folder \ sdk \ tools
and press shift and right click to open command prompt and type monkeyrunner c:\test_script\python_file_name.py

Categories

Resources