1

我有一个应用程序,我想为它运行 Monkeyrunner(使用 Android ViewClient)

我试图将我的观点定义为

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

但我收到错误:->"com.dtmilano.android.viewclient.ViewNotFoundException: Couldn't find View with ID='id/projectNewGallery' in tree with root=ROOT"

我怎样才能设置根?像这样 ?

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

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

我正在尝试使用 culebra 工具,但出现以下错误。

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

我也尝试过这种方式(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)

好的,我能够运行它 Windows CMD :

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

但我再次收到以下错误:

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

我的脚本文件--------

#! /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

4

1 回答 1

0

我在您的代码段中看到了几个错误(检查这个更正的版本):

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

为了简化过程,您可以使用culebra生成正确的脚本模板,以后可以根据需要进行调整: 当设备屏幕包含所需的视图时:

$ culebra -VC -o myscript.py

编辑myscript.py以添加对touch()方法的调用并运行

$ myscript.py

-C如果 ID 不可用,使用详细注释 ( ) 可以简化视图的识别。

更新

  1. 您在第一个示例中几乎就在那里,我想问题应该由我之前更正的代码段解决。代码运行,唯一的问题可能是您期望的 ID 不存在。请记住,如果后端是 UiAutomator,则没有 ID,这是 API >= 16 的默认后端。

  2. java runner 期望monkeyrunner在 PATH 中。

  3. 似乎/cygdrive/c/Android_Resources_Hassan/MonkeyRunner_KIneMaster/AndroidViewClient-master/AndroidViewClient/bin/androidviewclient-2.3.22.jar不存在。至少这是monkeyrunner这么想的。也许您应该\在 Windows 路径中使用。

于 2013-06-18T03:10:42.687 回答