python-2.7androidviewclient

Is there a replacement for time.sleep() in the given script?


The below script connects the android device to Wi-Fi and register with existing account.

import re
import sys
import time
import os

from PIL import Image

from com.dtmilano.android.viewclient import ViewClient
device, serialno = ViewClient.connectToDeviceOrExit()
vc = ViewClient(device=device, serialno=serialno)

vc.dump()
vc.findViewWithTextOrRaise(u'Continue').touch()##this line will click on Continue button.
print 'Continue button found and clicked'
vc.dump()
vc.findViewWithTextOrRaise(u'ABCCC').touch()##this line will click WiFi ABCCC SSID.
print 'SSID found and clicked'
vc.dump()
device.shell('input text *********')
vc.dump()
vc.findViewWithTextOrRaise(u'Connect').touch()##connect to wifi
time.sleep(20) <<<<< This line here
vc.dump()
device.shell('input text *********')##enter username
device.shell('input keyevent 61')
device.shell('input text *****')##enter password

Here sleep time is given as 20 seconds because it takes 10-20 seconds buffering time to prompt the next page (registration page) after connecting to Wi-Fi. For example at some instance if it takes just 10 seconds for this, the remaining 10 seconds is wasted and the script resumes only after that time. So is there any way to identify that "okay the registration page is prompted, its time to execute the next line of code without wasting any time".


Solution

  • Assuming there is a View, presumably an EditText, where you enter the username and also assuming such View appears after you press the Connect Button, you can do (you should find and use the specific id for your case)

    vc.findViewWithTextOrRaise(u'Connect').touch()##connect to wifi
    u = None
    while u is None:
        vc.dump()
        u = vc.findViewById('id/no_id/n')
    #device.shell('input text *********')##enter username
    u.type('**********')