1. 程式人生 > 實用技巧 >Appium模擬器新app安裝後首次進入的滑動操作

Appium模擬器新app安裝後首次進入的滑動操作

手機app全新安裝,首次進入app,一般會顯示一個可以左右滑動的,展示新功能的宣傳頁面。

在安卓手機的設計,原點位於左上角處,水平方向x 垂直方向為y

appium獲取當前手機顯示的寬和高

driver.get_window_size()['width']
driver.get_window_size()['height']

使用python程式設計控制模擬手指滑動的函式如下

# coding=utf-8
# 模擬移動端首次安裝手指滑動操作

from appium import webdriver


def get_driver():
    capabilities = {
        
"platformName": "Android", "platformVersion": "6.0.1", # "deviceName": "bd619e047cf3", "deviceName": "127.0.0.1:7555", "app": "D:\\Android\\open_mooc_701_.apk", # "appPackage":新版Appium1.19.1無需手動配置 # "appActivity":新版Appium1.19.1無需手動配置 "appPackage":"cn.com.open.mooc
", "appWaitActivity": "com.imooc.component.imoocmain.splash.GuideActivity" } driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", capabilities) return driver # driver.swipe(x,y,x1,y1) # driver.swipe(500,400,50,400) #水平從右向左滑動 # 獲取螢幕畫素的寬和高 # size = driver.get_window_size() # width = size['width']
# height = size['height'] # 獲取螢幕畫素寬和高,方法封裝 def get_size(): # driver = get_driver() size = driver.get_window_size() width = size['width'] height = size['height'] return width, height # 手指向左滑動,方法封裝 def swipe_left(): x = get_size()[0] / 10 * 9 y1 = get_size()[1] / 2 x1 = get_size()[0] / 10 # driver = get_driver() driver.swipe(x, y1, x1, y1) # 手指向右滑動,方法封裝 def swipe_right(): x = get_size()[0] / 10 y1 = get_size()[1] / 2 x1 = get_size()[0] / 10 * 9 # driver = get_driver() driver.swipe(x, y1, x1, y1) # 手指向上滑動,方法封裝 def swipe_up(): x = get_size()[0] / 2 y = get_size()[1] / 10 * 9 y1 = get_size()[1] / 10 x1 = get_size()[0] / 2 # driver = get_driver() driver.swipe(x, y, x1, y1) # 手指向下滑動,方法封裝 def swipe_down(): x = get_size()[0] / 2 y = get_size()[1] / 10 y1 = get_size()[1] / 10 * 9 x1 = get_size()[0] / 2 # driver = get_driver() driver.swipe(x, y, x1, y1) # 進一步封裝,滑動的方法 def swipe_on(direction): if direction == 'up': swipe_up() elif direction == 'down': swipe_down() elif direction == 'left': swipe_left() else: swipe_right() if __name__ == '__main__': driver = get_driver() #獲取一個全域性變數 swipe_on('left') swipe_on('left')

獲取當前開啟著的appActivity

adb shell dumpsys window|findstr mCurrentFocus

連線mumu模擬器

adb connect 127.0.0.1:7555

執行測試模擬器,發現可正確響應。