Appium Mac Appium 自动识别 iPhone 真机测试

雅婷 · 2016年09月27日 · 最后由 朱亚文 回复于 2018年06月20日 · 1545 次阅读

Mac Appium 自动识别 iPhone 真机测试

每次连接真机测试的时候,一旦更换手机,就需要重新在测试代码里面更改 udid,devicename,deviceplatform,实在是太麻烦。
于是为了偷懒,写了一段自动获取真机的信息的 python 代码,如果连接多台机器,可以选择测试设备。如果有错误,希望指出~谢谢了~

deviceInfo.py

#-*- coding:utf-8 -*-
import commands
import re
def deviceinfo():
    global deviceName,platformVersion,udid 
    deviceName,platformVersion,udid = 0,0,0
    deviceUdid = commands.getoutput('system_profiler SPUSBDataType | grep "Serial Number:.*" | sed s#".*Serial Number: "##')
    deviceNumlist = deviceUdid.split('\n')
    alldevice = commands.getoutput('instruments -s devices') 
    #print len(deviceUdid)
    if len(deviceUdid)==0:
        print "无设备连接,请连接设备"
    elif len(deviceUdid)==40:
        print "连接一台设备,开始测试"
        name =re.findall(r'(?<=\n).*'+deviceUdid,alldevice)[0]
        deviceName =re.findall(r'.*(?=\(9)',name)[0]
        platformVersion =re.findall(r'(?<=\().*(?=\))',name)[0]
        udid = re.findall('(?<=\[).*',name)[0]

    elif  len(deviceUdid)==81:
        print "连接2台设备,请选择测试设备,\n选择"+deviceNumlist[0]+"请输入0,\n选择",deviceNumlist[1],"请输入1"
        num =input()
        name =re.findall(r'(?<=\n).*'+deviceNumlist[num],alldevice)[0]
        print name
        deviceName =re.findall(r'.*(?=\(9)',name)[0]
        platformVersion =re.findall(r'(?<=\().*(?=\))',name)[0]
        udid = re.findall('(?<=\[).*',name)[0]
    else:
        print "最多连接两台设备"

deviceinfo()

在 RunAppiumTest.py 中

#-*- coding:utf-8 -*-
import deviceInfo
deviceName= deviceInfo.deviceName
platformVersion=deviceInfo.platformVersion
udid =deviceInfo.udid
class RunAppiumTest(unittest.TestCase):
    def setUp(self):
        deviceInfo.deviceinfo()
        app = os.path.join(os.path.dirname(__file__),
                            '/Users/test/.test/jobs/test/workspace/XLDownloadkit/build/Release-iphoneos',
                            'test.app')
        app = os.path.abspath(app)
        self.driver = webdriver.Remote(
            command_executor='http://127.0.0.1:4723/wd/hub',
            desired_capabilities ={
                'app' : app,
                'platformName' : 'iOS',
                'platformVersion' :"'"+platformVersion+"'",
                'deviceName' : "'"+deviceName+"'",
                'app':'com.test.test',
                'udid':"'"+udid+"'"
            })
        self._values = []

执行测试用例,如果有一台设备连接 Mac,会自动在该设备上运行测试用例,如果有两台设备连接 Mac,会提示让你选择对应 udid 的设备,选择以后就可以运行了

共收到 12 条回复 时间 点赞

我以前是用 idevice_id -l 来判断的,如果只有一个设备直接就用那个设备执行。

请问你是用 Python 直接启动 appium 再执行代码吗?

#3 楼 @304764691不是呀,我是在保持 appium 允许的情况下,直接在命令行执行 python 的测试用例

#1 楼 @chenhengjie123 恩恩,这个也可以,但是我没有装这个

好吧 我升级到 10.11.6 系统了 运行不了 appium

#2 楼 @xdf 🌼 🌼 🌼 ~等我晚上有时间再看下

#6 楼 @304764691 好像 appium 还没有支持 ios10 的,我之前也是运行不了

不错不错

alldevice = commands.getoutput('instruments -s devices') 执行这一句时只打印出 Known Devices: 这一句,我用 java 写的,然后在 appium 里调用。

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册