0

我正在尝试使用 AndroidviewClient 切换蓝牙。下面是部分代码。我能够“找到蓝牙”并获得 ID 和文本。然后我想获得 ON/OFF 的视图来切换。当我在下面的 for 循环中打印时,我得到 3 个视图,其中一个是基于当前状态的 ON 或 OFF 视图。如何在 for 循环中检查此视图以切换它?谢谢你的帮助,

BR斯里尼

view = vc.findViewWithText('Bluetooth')
print "Bluetooth id :", view.getId()
print "Blutooth Text    :", view.getText()

for i in view.parent.parent.children:
    print str(i)
4

1 回答 1

1

解决方案是获取“蓝牙”视图祖父母,然后在该子树中搜索 ToggleButton。为了在这种情况下找到视图,我们使用模式' ON|OFF ',因此状态并不重要,因为切换它是我们的目标:

parent = vc.findViewWithTextOrRaise('Bluetooth').getParent().getParent()
vc.findViewWithTextOrRaise(re.compile('ON|OFF'), root=parent).touch()

这可能是最好的解决方案。但是,还有一种替代方案也可能适合您的需求,并且更容易获得。

运行culebra生成脚本模板:

$ culebra -i off -t on -d on -C -j on -o ~/tmp/bluetooth.py

然后,您会在脚本中看到类似的内容(可能因 API 级别和设备而异):

# class=android.widget.TextView text="Bluetooth"
no_id26 = vc.findViewWithTextOrRaise('Bluetooth')

# class=android.widget.Switch text="OFF"
no_id27 = vc.findViewWithTextOrRaise('OFF')

所以,如果你只是追加

no_id27.touch()

到脚本时,它会在您每次运行时切换蓝牙。

于 2013-05-09T06:07:51.823 回答