1. 程式人生 > >UIAutomator2 一個簡單的練手demo,自動刷東方頭條

UIAutomator2 一個簡單的練手demo,自動刷東方頭條

UIAutomator2 一個簡單的練手demo,自動刷東方頭條

相容不是很好,還有情況沒有寫

import uiautomator2 as u2
import time

n = 0

#連線裝置
# if u2.connect_usb():
#     print("已連線USB裝置")
# d=u2.connect_usb()
d=u2.connect_wifi("192.168.2.237")
# elif u2.connect_wifi("192.168.1.105"):
#     print("連線WiFi裝置")
# d=u2.connect_wifi("192.168.1.105")
# else:
#     print("未能連線裝置")
print(d.info)
#獲取視窗大小

print("解析度是:"+ str(d.window_size()))
'''解鎖'''
def screen_on():
    d.screen_on()
    d.unlock()
    d.swipe(0.5, 0.9, 0.5, 0.6, 0.05)
    time.sleep(1)
    d.swipe_points([(0.228, 0.525),(0.005, 0.525),(0.766, 0.525),(0.5, 0.677),(0.228, 0.828),(0.497, 0.828),(0.761, 0.828)],0.05)
screen_info=d.info.get("screenOn")
if screen_info==False:
    screen_on()
else:
    print("無需解鎖")

'''啟動app'''
def star_app(pkg):
    d.toast.show("正在啟動app")
    d.app_start(pkg)
    time.sleep(2)
star_app("com.songheng.eastnews")

'''登入'''
def login():
    d(resourceId="com.songheng.eastnews:id/y6").click_exists(timeout=15)
    d(resourceId="com.songheng.eastnews:id/la").click_exists(timeout=15)
    d(resourceId="com.songheng.eastnews:id/a8l").click_exists(timeout=15)
    
    
    #判斷是否需要登入
    if d(text=u"未登入"):
        d(text=u"未登入").click_exists(timeout=15)
        d(text=u"點選登入").click_exists(timeout=15)
        d(text=u"賬號密碼登入").click_exists(timeout=15) 
        d(className="android.widget.EditText").clear_text()
        d(className="android.widget.EditText").click()
        d.shell("input text 15021501530") 
        d(resourceId="yzm", text=u"xxxx", className="android.widget.EditText").set_text("xxxx") #使用者名稱和密碼填寫自己的
        time.sleep(1)   
        d(text=u"點選領取紅包", className="android.view.View").click()
        d(resourceId="com.songheng.eastnews:id/a8l", className="android.widget.ImageView").click_gone(1,2)
        d(resourceId="com.songheng.eastnews:id/fi").click_exists(timeout=10)

    elif d(resourceId="com.songheng.eastnews:id/afo"):
         print ("已經登入,無需登入")
'''獲取使用者名稱'''
def get_username(username,password):
    d(className="android.widget.EditText").clear_text()
    d(text=u"請輸入手機號")
    d(text=u"請輸入手機號").set_text(username)
    d(text=u"請輸入密碼").set_text(password)
    d.press("back")
    time.sleep(2)

'''閱讀新聞'''
def read_news():
    
    while True:
        d(scrollable=True).scroll(steps=35)
        # d(resourceId="com.songheng.eastnews:id/vf", text=u"取  消", className="android.widget.Button").click_gone(1,1)
        time.sleep(0.5)  
        if d(text=u"展開全文"):
            time.sleep(1)

            if d(text=u"展開全文"):
                d(text=u"展開全文").click()
                # d(resourceId="com.songheng.eastnews:id/vf", text=u"取  消", className="android.widget.Button").click_gone(1,1)
            else:
                d(scrollable=True).scroll(steps=35)
                # d(resourceId="com.songheng.eastnews:id/vf", text=u"取  消", className="android.widget.Button").click_gone(1,1)
                d(text=u"展開全文").click()
                
            # else:
            #     if d(text=u"猜你喜歡 ", className="android.view.View"):
            #         d(scrollable=True).scroll(steps=35)
            #         d.press("back")
            #         n+=1
            #         # print("第{}次瀏覽新聞".format(n))
            #         get_news()
            #     time.sleep(1)
                
                while True:
                    d(scrollable=True).scroll(steps=35)
                    
                    if d(text=u"猜你喜歡 ", className="android.view.View"):

                        d(scrollable=True).scroll(steps=35)
                        
                        d.press("back")
                        break
                        get_news()
                        
        elif d(resourceId="wrapper", className="android.view.View"):
            d(scrollable=True).scroll(steps=50)
            
            d(scrollable=True).scroll(steps=50)
            d.press("back")
            break
            get_news()
        elif d(text=u"百度一下"):
            d.press("back")
            break
            get_news()
            

'''點選新聞'''
def click_news(n):
    n+=1
    # d(resourceId="com.songheng.eastnews:id/vf", text=u"取  消", className="android.widget.Button").click_gone(1,1)
    d(resourceId="com.songheng.eastnews:id/gk").click_gone(1,2)
    d(resourceId="com.songheng.eastnews:id/mc").click_gone(1,2)
    code= d(resourceId="com.songheng.eastnews:id/o0").get_text()
    print(n,"開始閱讀:"+ code)
    d(resourceId="com.songheng.eastnews:id/o0").click()
    return n
  
    
    
'''獲取新聞'''
def get_news():

    # d(resourceId="com.songheng.eastnews:id/l2").click_exists(timeout=30)
    while True:
        
        d.swipe(0.4, 0.3, 0.4, 0.9, 0.3)
        # if d(resourceId="com.songheng.eastnews:id/gk"):
        #     time.sleep(1)
        #     click_news()
        #     read_news()

        if d(text="下拉重新整理"):
            time.sleep(1)
            click_news(n)
            read_news()
        elif d(text="鬆開立即重新整理"):
            time.sleep(1)
            click_news(n)
            read_news()
        elif d(text="重新整理中"):
            time.sleep(1)
            click_news(n)
            read_news()
        
login()
get_news()


    

# d(text=u"賬號密碼登入").click()
# d(className="android.widget.EditText").clear_text()
# d(text=u"請輸入手機號")
# d(text=u"請輸入手機號").set_text("15021501530")
# print("輸入賬號")
# d(text=u"請輸入密碼").set_text("happyforever1")
# print("輸入密碼")
# time.sleep(1)
# d.press("back")
# time.sleep(2)
# d(text=u"點選領取紅包").click()