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 的设备,选择以后就可以运行了


↙↙↙阅读原文可查看相关链接,并与作者交流