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