appium模擬操作安卓手機按鍵
APPium自動化過程中,可能會需要模擬操作手機按鍵,如返回鍵,home鍵,音量鍵等等。
要模擬按鍵操作得用到 keyevent方法,參數如下
keyevent(keycode, metastate=None)
metastate:默認值不用填
操作手機音量鍵和返回鍵的代碼如下:
1 from appium import webdriver 2 import time 3 from appium.webdriver.common.mobileby import MobileBy as MB 4 from selenium.webdriver.support.wait import WebDriverWait 5 from selenium.webdriver.support import expected_conditions as EC 6 7 # 連接設備、啟動APP的相關參數 8 caps = { 9 "platformName":"Android", # 平台類型 10 "platformVersion":"7.1.1", #平台版本 11 "deviceName":"SWCUJFGULNVCGIEI", #設備名 12 "appPackage":"com.tencent.wework", #APP的包名 13 "appActivity":"com.tencent.wework.launch.LaunchSplashActivity", #APP的啟動名 14 "noReset":"True", #是否重置APP 15 "automationName":"UiAutomator2", 16 "noCanmandTime":"6000" , #超時時長 17 "chromedriverExecutable":"D:\\work\\Chromedriver\\65_67\\chromedriver.exe" 18 } 19 20 driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', caps) 21 # 設置隱式等待 22 driver.implicitly_wait(30) 23 # 音量減 24 driver.keyevent(25) 25 time.sleep(1) 26 # 返回鍵 27 driver.keyevent(4)
Android的keycode鍵值
官方keyevent文檔
地址: https://developer.android.com/reference/android/view/KeyEvent.html