1. 程式人生 > >uiautomator/Android Test Python example scripts Continue

uiautomator/Android Test Python example scripts Continue

Refer to 

Setup

https://blog.csdn.net/hushui/article/details/82896181

Example

https://blog.csdn.net/hushui/article/details/82855839

Official web 

 https://github.com/xiaocong/uiautomator

1.   Python header!!!

#-*- coding:UTF-8 -*-
# author:Lake Hu :2018/9
## Note as uiautomator is imported, this py file should NOT

be named as uiautomator.py!!!!
# -*- coding: utf-8 -*-
import os
import time
 
print("Start")
from uiautomator import device as d
 

2.  GetSystemInfo

def GetSystemInfo():
    print("Print info directly")
    Phone=d.info 
    print(Phone) 
    h=Phone['displayHeight']
    w=Phone['displayWidth']
    productName=Phone['productName']
    print "Phone info's ProductName is "+productName

 

 

3.  Uninstall/install  apk through adb 

 

def CleanXXX_apk():
    print('Uninstall XXX  apk now')    
    os.system('adb shell pm clear com.XXXX')     ### name is got through "adb shell dumpsys package packagename"
    os.system('adb shell pm uninstall com.XXXXX')
    os.system('adb shell rm -Rf /sdcard/data')   ##### optinal 

def InstallXXXX_apk():
    print('install APP')
    os.system('adb push /home/lake/Android/com.XXX.apk  /sdcard/')
    os.system('adb shell pm install /sdcard/com.XXXX.apk')

 

4. StartXXX_apk

def StartXXX_apk():
    print('install APP')
    #get activity name throuhgh adb shell dumpsys package yourpackagename
    os.system('adb shell am start com.XXXX/main.activity.MainActivity')
    if d(resourceId="com.XXXXX:id/action_bar_root").wait.exists(timeout=5000):
        #Check any item in app first GUI page 
        print("APP Starts OK")
    else:
        print("APP ANR: time > 5000 ms")

    

5. Auto login 

def XXXX_TryLogin():
    print('am start activity')
    #get activity name throuhgh adb shell dumpsys package yourpackagename
    os.system('adb shell am start com.XXXX/main.activity.MainActivity')

    d.wait.idle()    
    if d(resourceId="com.XXXX:id/bottom_controller_my").wait.exists(timeout=8000):
        #Check any item in app first GUI page 
        print("Find My button ")
        d(resourceId="com.XXXX:id/bottom_controller_my").click()
    else:
        print("Swipe to skip advivertiment ")
        d.swipe(0,500,500,500,100)
        d.swipe(0,500,500,500,100)
        d.swipe(0,500,500,500,100)
        
    if d(resourceId="com.XXXX:id/view_load_login").wait.exists(timeout=2000):
        #Check any item in app first GUI page 
        print("Click to login now ")
        d(resourceId="com.XXXX:id/view_load_login").click()
    else:
        print("Still wait ...  ")
        
    if d(resourceId="com.XXXX:id/login").wait.exists(timeout=2000):
        #Check any item in app first GUI page
        d(resourceId="com.XXXX:id/phone_num").set_text("18XXXXXX");
        d(resourceId="com.XXXX:id/password").set_text("YourPassword");
        d(resourceId="com.XXXX:id/login").click()
    else:
        print("Still wait ...  ")
 
    d.wait.idle()

 

6.  Utility function.  click button, wait for next page in Android UI 


def page_wait_switch(wait_name,wait_time):
    if d(resourceId=wait_name).wait.exists(timeout=wait_time):
        print("Click..  "+wait_name)
        d(resourceId=wait_name).click.wait()
        

7. swipe 

<  up down ( portail  left right )
d.swipe(0,500,800,500,100)

d.swipe(0,500,1000,500,100)

d.swipe(0,500,2000,500,200)

d.swipe(0,1000,2000,1000,200)


< left  right    (portail  up  down)
d.swipe(500,0,500,500,100)