0

I am trying to toggle Bluetooth using AndroidviewClient. Below is part of the code. I am able to "find Bluetooth" and get the id and text also. Then I want to get get the view for ON/OFF to toggle. When I print in the below for loop, I get 3 views and one of them is the view for ON or OFF based on the current state. How to check in the for loop for this view to toggle it? Thanks for your help,

BR Srini

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

for i in view.parent.parent.children:
    print str(i)
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134

1 Answers1

1

The solution is to get the 'Bluetooth' View grandparent and then search for the ToggleButton in that subtree. To find the View in this case we use the pattern 'ON|OFF', so it doesn't matter the state as toggling it is our objective:

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

This is probably the best solution. However there is an alternative that may also suit your needs and it's much easier to obtain.

Run culebra to generate a script template:

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

then, you see in the script something like (may vary depending on the API level and device):

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

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

so, if you just append

no_id27.touch()

to the script it will toggle Bluetooth every time you run it.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134