appium模拟安卓手机按键

APPium自动化过程中,可能会需要模拟操作手机按键,如返回键,home键,音量键等等。

要模拟按键操作得用到 keyevent方法,参数如下

keyevent(keycode, metastate=None)

metastate:默认值不用填

操作手机音量键和返回键的代码如下:

from appium import webdriver
import time
from appium.webdriver.common.mobileby import MobileBy as MB
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 连接设备、启动APP的相关参数
caps = {
    "platformName":"Android",   # 平台类型
    "platformVersion":"7.1.1",  #平台版本
    "deviceName":"SWCUJFGULNVCGIEI",    #设备名
    "appPackage":"com.tencent.wework",   #APP的包名
    "appActivity":"com.tencent.wework.launch.LaunchSplashActivity",  #APP的启动名
    "noReset":"True",   #是否重置APP
    "automationName":"UiAutomator2",
    "noCanmandTime":"6000" ,     #超时时长
    "chromedriverExecutable":"D:\\work\\Chromedriver\\65_67\\chromedriver.exe"
}

driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', caps)
# 设置隐式等待
driver.implicitly_wait(30)
# 音量减
driver.keyevent(25)
time.sleep(1)
# 返回键
driver.keyevent(4)

Android的keycode键值

官方keyevent文档

地址: https://developer.android.com/reference/android/view/KeyEvent.html

转载链接:https://www.cnblogs.com/htx18/p/11905768.html

posted @   simon_T  阅读(57)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!
点击右上角即可分享
微信分享提示