【问题标题】:Pressing buttons by 'text' on Android app using Culebra使用 Culebra 在 Android 应用程序上按“文本”按钮
【发布时间】:2017-02-11 04:17:15
【问题描述】:

我有一个 Android 应用程序,我正在尝试使用 culebra 进行测试。代码如下所示。

'''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`等的按钮。

我在这里做错了什么。

【问题讨论】:

  • 看来b.getXY() 没有返回视图的坐标。检查b 是否包含您期望的内容。
  • 另外,使用culebraculebra -G 为一种情况生成触摸,然后您可以将其变成循环。这样你就会有正确的语法。
  • @DiegoTorresMilano - 如何在 python 脚本中使用 culbera -G?
  • 抱歉,不够清楚。我不是说你可以在你的脚本中使用culebra -Ginside,但你可以运行它,然后剪切并粘贴新生成的代码。

标签: android python monkeyrunner androidviewclient


【解决方案1】:

运行

culebra -Gu -o myscript.py --scale=0.5

你会看到一个代表你的设备的窗口,很像

然后你点击按钮(我在这里运行 ApiDemos)和culebra 生成

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)

然后您可以手动将其变成

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()

然后您可以复制并粘贴到您的原始脚本中。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-21
  • 2012-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多