使用Culebra在Android应用上通过“文本”按下按钮 [英] Pressing buttons by 'text' on Android app using Culebra
问题描述
我有一个Android应用,正在尝试使用 culebra
进行测试。代码如下所示。
I have a Android App that I am trying to test using culebra
. The code is shown below.
'''reated on 2017-02-08 by Culebra v12.5.3
__ __ __ __
/ \ / \ / \ / \
____________________/ __\/ __\/ __\/ __\_____________________________
___________________/ /__/ /__/ /__/ /________________________________
| / \ / \ / \ / \ \___
|/ \_/ \_/ \_/ \ o \
\_____/--<
@author: Diego Torres Milano
@author: Jennifer E. Swofford (ascii art snake)
'''
import re
import sys
import os
from com.dtmilano.android.viewclient import ViewClient
from com.dtmilano.android.adb.adbclient import DOWN_AND_UP
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}
vc = ViewClient(device, serialno, **kwargs2)
# Installs the Android package. Notice that this method returns a boolean, so you can test
# to see if the installation worked.
vc.installPackage('Abc App.Android.Abc App.Android-Signed.apk')
# sets a variable with the package's internal name
package = 'Abc App.Android.Abc App.Android'
# sets a variable with the name of an Activity in the packag
activity = 'md591ecfcc00ede54e89ae8714.MainActivity'
# sets the name of the component to start
runComponent = package + '/' + activity
# Runs the component
device.startActivity(component=runComponent)
vc.sleep(5)
#vc = ViewClient(device)
vc.dump()
for bt in [ 'PRO', 'FIE', 'DIA']:
b = vc.findViewWithAttribute('text:mText', bt)
if b:
(x, y) = b.getXY()
print >>sys.stderr, "clicking b%s @ (%d,%d) ..." % (bt, x, y)
b.touch()
time.sleep(7)
# Presses the Menu button
# device.press('KEYCODE_MENU', DOWN_AND_UP)
# Writes the screenshot to a file (you can use a plain filename or use these placeholders)
vc.writeImageToFile('/tmp/${serialno}-${focusedwindowname}-${timestamp}.png', 'PNG')
当我以
$ python test_culebra.py
我收到以下错误。
[100%] /data/local/tmp/AbcApp.Android.AbcApp.Android-Signed.apk
pkg: /data/local/tmp/AbcApp.Android.AbcApp.Android-Signed.apk
Success
Traceback (most recent call last):
File "monkey_runner_culebra.py", line 53, in <module>
print >>sys.stderr, "clicking b%s @ (%d,%d) ..." % (bt, x, y)
NameError: name 'x' is not defined
它正在安装和加载该应用程序。但是,找不到带有文本 PRO',
FIE ,
DIA`等的按钮。
It is installing and loading the app. However it is not able to find the button with the text PRO',
FIE,
DIA` etc.
我在这里做错了什么。
What am I doing wrong here.
推荐答案
运行
culebra -Gu -o myscript.py --scale=0.5
您将看到一个代表您的窗口设备,就像
you'll see a window representing your device, much like
然后单击按钮(我在这里运行ApiDemos)和 culebra
生成
then you click on the buttons (I'm running ApiDemos here) and culebra
generates
vc.dump(window=-1)
vc.findViewWithTextOrRaise(u'NORMAL').touch()
vc.sleep(_s)
vc.dump(window=-1)
vc.findViewWithTextOrRaise(u'SMALL').touch()
vc.sleep(_s)
vc.dump(window=-1)
vc.findViewWithTextOrRaise(u'OFF').touch()
vc.sleep(_s)
vc.dump(window=-1)
,然后您可以将其手动转换为
which you can then manually turn into
for t in ['NORMAL', 'SMALL', 'OFF']:
b = vc.findViewWithTextOrRaise(t)
print >> sys.stderr, "clicking", b, "@", b.getXY()
b.touch()
甚至
for t in ['NORMAL', 'SMALL', 'OFF']:
vc.findViewWithTextOrRaise(t).touch()
这是假设屏幕没有单击按钮时更改,是否需要再次调用 vc.dump()
。
this is assuming the screen does not change when you click your buttons, if it does you need to call vc.dump()
again.
然后您可以复制并粘贴到原始脚本中。
Then you can copy and paste to your original script.
这篇关于使用Culebra在Android应用上通过“文本”按下按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!