Search code examples
androidpythonshelladbandroid-uiautomator

python script does not work but individual shell command does


I am trying to run this piece of python code to learn how to use uiautomator to do android testing.

from uiautomator import device as d
d(index = 2, className='android.widget.TextView').click() #menu button
print 'inside menu button'
d(text='People', className='android.widget.TextView').click() #contacts
print 'inside contacts'
#loop to add a contact  
d(resourceId= 'com.android.contacts:id/menu_add_contact', className='android.widget.TextView').click()

This piece of code does not work when i run it as a python script python add_contact.py

however, when i run it sequentially like this.

python #enters the shell 
from uiautomator import device as d
d(index = 2, className='android.widget.TextView').click() #menu button
d(text='People', className='android.widget.TextView').click() #contacts
d(resourceId= 'com.android.contacts:id/menu_add_contact', className='android.widget.TextView').click()

Basically what i done is to enter the python shell and enter the commands one by one instead of running it as a script. The outcome is that the contacts book icon is not pressed in the script but entering it manually it would work.


Solution

  • Apparently i have solved this via this code.

    from uiautomator import device as d
    d(index = 2, className='android.widget.TextView').click() #menu button
    print 'inside menu button'
    d(index='15', className='android.widget.TextView').click() #contacts
    print 'inside contacts'
    #loop to add a contact  
    d(resourceId= 'com.android.contacts:id/menu_add_contact', className='android.widget.TextView').click()
    

    It might be a lag issue

    by changing

    d(text='People', className='android.widget.TextView').click() #contacts
    

    to this

    d(index='15', className='android.widget.TextView').click() #contacts