appium之Android keycode實現模擬按鍵


來源

Android是自帶一些按鍵操作,即使現在的手機使用從底部滑動,或者左滑返回,它的原始的按鍵如home,返回鍵都是存在的,可以直接調用

使用場景

模擬手機硬件信號,如電源、音量、明暗、鍵盤

原理

根據Android keycode對應的數字,調用Android密鑰代碼,實現模擬按鍵。

如何使用?

使用函數keyevent()或者函數press_keycode()

#導包
from appium import webdriver
import time
#准備自動化配置信息
desired_caps={
    'platformName':'Android',
    'plathformVersion':'10',
    'deviceName':'MIUI12.0.1',
    'appPackage':'com.hpbr.bosszhipin',  
    'appActivity':'.module.launcher.WelcomeActivity',  
    'noReset':True,
    'newCommandTimeout':6000,
    'automationName':'UiAutomator2',
    'skipServerInstallation':True
}
driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps)
driver.implicitly_wait(10)

#模擬打開通知欄
driver.open_notifications()

time.sleep(5)
driver.keyevent(4)  #關閉通知欄--模擬發送返回鍵
# driver.press_keycode(4)  效果同上
time.sleep(2)
driver.keyevent(3)   #返回到主屏幕,home鍵
input('按任意字符退出程序')

driver.quit()

下面網址是Android官網對於keyevent的介紹,可以找到按鍵對應的數字,不過需要FQ才能訪問。

http://developer.android.com/reference/android/view/KeyEvent.html

因為獲取官網keycode需要FQ比較麻煩,分享一篇別人總結好的比較詳細的Android keycode的總結:

https://www.cnblogs.com/bluestorm/p/4886662.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM