Appium Python 自动化测试二
capability Android 参数说明
1、platformName: 连接设备的平台名称 Android、iOS
2、platformVersion: 连接设备的 Android 版本
3、deviceName: 连接设备的名称
4、appPackage : 被测试包名
5、automationName: Android版本大于7.0 使用uiAutomator2
6、autoGrantPermissions : 自动获取权限
7、unicodeKeyboard: True 支持中文输入
8、resetKeyboard: 复位键盘
9、udid: 连接设备的唯一识别号, 获取方式请查看第一篇文章
10、noReset:
11、newCommandTimeout:命令超时时间
Android 手机如何连接一个Appium server
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2020/9/6 20:52
# @Author : dorom
# @File : device_connect.py
# @Software: PyCharm
from appium import webdriver
class RemoteDriver(object):
def __init__(self,capabilitiesData):
self.capabilitiesData = capabilitiesData
platformName = capabilitiesData["platformName"]
self.Capabilities = {}
self.Capabilities["platformName"] = platformName
self.Capabilities["platformVersion"] = capabilitiesData["platformVersion"]
self.Capabilities["deviceName"] = capabilitiesData["deviceName"]
if platformName.upper() =="ANDROID":
self.Capabilities["appPackage"] = capabilitiesData["appPackage"]
self.Capabilities["appActivity"] = capabilitiesData["appActivity"]
self.Capabilities["automationName"] = "uiAutomator2"
self.Capabilities["autoGrantPermissions"] = True # 自动获取需要哪些权限
self.Capabilities["unicodeKeyboard"] = True # 使用Appium 自带的输入法
self.Capabilities["resetKeyboard"] = True # 输入法复位
else:
raise Exception("{0} 的手机类型不存在,只支持Android、iOS".format(platformName))
self.Capabilities["udid"] = capabilitiesData["udid"]
self.Capabilities["noReset"] = True
self.Capabilities["newCommandTimeout"] = 72000
def remoteDriver(self):
"""
手机连接Appium server
:return: Driver
"""
remoteUrl = "http://{0}:{1}/wd/hub".format(str(self.capabilitiesData["host"]),str(self.capabilitiesData["port"]))
driver = webdriver.Remote(remoteUrl,self.Capabilities)
return driver
启动服务并连接手机
from start_appium_server import AppiumServer
def run(capabilitiesData):
appium_server = AppiumServer("127.0.0.1", 4723, "udid")
appium_server.start_appium_server()
RemoteDriver(capabilitiesData).remoteDriver()
if __name__ == '__main__':
capabilitiesData = {"platformName":"Android","platformVersion":"7.1",
"deviceName":"小米1","appPackage":"com.tencent.mm",
"appActivity":"com.tencent.mm.plugin.sns.ui.SnsTimeLineUI",
"udid":"SJE5T17619002517"}
run(capabilitiesData)