如何使用AndroidViewClient触摸/按下按钮/查看?

时间:2013-06-17 07:02:48

标签: android view androidviewclient

我有一个应用程序,为此我想运行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"

我如何设置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.

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

与XML中定义的ID相同:id =“+ id / newProjectGallery”?

////////////////////// 在代码中添加vc.traverse()后 ///////// //////////

我收到以下错误

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'

此外,我还运行了dump-simple.py代码。但我又得到了同样的错误:

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'
是什么导致它? 请帮帮我〜

由于

1 个答案:

答案 0 :(得分: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

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

更新

  1. 你的第一个例子几乎就在那里,我想这个问题应该由我之前纠正的片段修复。代码运行,唯一的问题可能是你期望不存在的ID。 请记住,如果后端是UiAutomator,则没有ID,这是API&gt; = 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路径中使用\