appium -ios 真机连接 环境搭建
补充点一:
安装ios-deploy
npm install -g ios-deploy 
安装不了报错。!
后来找了找,发现sudo npm install -g ios-deploy --unsafe-perm=true可以成功安装
补充点二:
使用命令安装appium的 WebDriverAgent的位置如下:
位置: /usr/local/lib/node_modules/appium/node_modules/appium-xcuitest-driver/WebDriverAgent
使用安装包安装的appium的WebDriverAgent的位置如下:
位置:/Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/appium-xcuitest-driver/WebDriverAgent
补充点三:
追加的一个重要的问题:
要使用Inspector来定位元素,要使用命令行安装 : brew install ios-webkit-debug-proxy
其中要使用appium自带的Inspector来定位元素
点击桌面版appium的🔍进入Start Inspector Session,设置好信息见教程
最后自己搭建的环境:
qppium V1.7.2(桌面版)
Version 9.2
macOS 10.12.6
下面是 参考的文章内容(亲测ok)
环境搭建
1. Xcode
必须承认,我是Mac OS新手,有Linux的基础,用了几天Mac OS,算是基本会用了……
本文的系统版本为Mac OS 10.12.5,由于陪测的iOS用的是10.3.2,所以Xcode必须要装上新的8.3.2(不然没有SDK),Xcode在App Store里安装就好了。
2. Appium
Appium向来有命令行版的和GUI版的——我选择后者,到官网下载安装最新的Appium Desktop 1.0.2的dmg,里面带了1.6.4的Appium。
3. Appium客户端库
Python、Ruby、Java、Javascript、PHP、C#等,任君选择,去官网下载。
例如我用Python,就安装Appium-Python-Client,在终端运行
| 1 2 | sudo easy_install pip   # 系统自带easy_install pip install Appium-Python-Client --user  # 加上--user是因为Mac下有权限的问题 | 
4. Homebrew
Homebrew相当于Linux下的apt-get、yum,要用它来安装node,在终端运行
| 1 2 | /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" brew -v      # 显示版本,如Homebrew 1.2.1 | 
5. node
| 1 2 | brew install node node -v    # e.g. v7.10.0 | 
6. appium-doctor相关
用来检测Appium相关环境有没有装好的工具:
| 1 2 3 4 5 6 7 8 | npm install -g appium-doctor # 装好之后 检测一下iOS的环境有没有配置好 如果不加--ios 则检测Android和iOS appium-doctor --ios # 它提示我缺少Xcode Command Line Tools和Carthage,那就补上 xcode-select --install brew install carthage | 
7. 还有一些库
| 1 2 | brew install libimobiledevice --HEAD npm install -g ios-deploy   # for iOS 10+ | 
8. WebDriverAgent相关(大坑)
iOS 10+使用的是XCUITest,Appium使用的模块是appium-xcuitest-driver,其中引用了Facebook提供的WDA方案来驱动iOS的测试。装Appium Desktop的时候,它里面带了一个WebDriverAgent,但是这个自带的是有问题的!会造成不能使用Inspector,卡了很久!从Facebook那里自己clone一份才是王道:
| 1 2 3 4 5 6 7 8 9 10 11 | # cd ~ # git clone https://github.com/facebook/WebDriverAgent.git(已在我的百度云里面有保存这个文件) # cd WebDriverAgent # mkdir -p Resources/WebDriverAgent.bundle # ./Scripts/bootstrap.sh    # 开始下载并编译 编译不应该报错 # cd /Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/appium-xcuitest-driver/ cd /Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/  # 上面的目录可能改成了这个 # mv WebDriverAgent WebDriverAgent2  # 把自带的改名 # ln -s ~/WebDriverAgent WebDriverAgent  # 用facebook的原版替换回去 | 
经过了baidu和google,用以上方法解决了不能Inspect的问题。
在使用Appium时,需要把WDA装到真机上,然后又会遇到证书的问题,我也不是很明白,总之跟provisioning profile有关。
用Xcode打开目录上面第9行的目录下的WebDriverAgent.xcodeproj,对于WebDriverAgentLib 和 WebDriverAgentRunner,勾选“Automatically manage signing”,把Team改成公司的,Bundle Identifier改成公司的证书可以接受的名字,具体可以参考官方文档操作,不懂的找开发同学协助。 (我们app的我已放到我的百度云盘里了)
然后就可以把WebDriverAgentLib和WebDriverAgentRunner都编译到真机运行一下了。正常来说,会在桌面生成一个没图标的WebDriverAgentRunner,点开之后不会有什么反应,这就对了。
终于把环境搭好了,感动啊。
写测试脚本
1. Appium server capabilities
要让App跑起来,还需要了解Appium server capabilities,它告诉Appium服务器很多信息,例如开哪个App、手机的系统/版本、在哪台设备上跑(真机还是模拟器等)等。
给出我用到的一些参数(in Python),其他capabilities请参考官方文档。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | # -*- coding: utf-8 -*-  from time import sleep from appium import webdriver desired_caps = {} desired_caps['automationName'] = 'XCUITest' # Xcode8.2以上无UIAutomation,需使用XCUITest desired_caps['platformName'] = 'iOS' desired_caps['platformVersion'] = '10.3.2' desired_caps['deviceName'] = 'iPhone 7 Plus' desired_caps['bundleId'] = '需要启动的bundle id, 去问开发者' desired_caps['udid'] = '真机的udid 可在Xcode或iTunes里查看' desired_caps['newCommandTimeout'] = 3600  # 1 hour # 打开Appium服务器,start server后,尝试启动被测App driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps) sleep(60) driver.quit() | 
如果能跑起来,就是正常的,不然看一下报什么错。
2. Inspector
能跑起来只是第一步,更重要的是如何定位元素。
Inspector的使用方法很简单,之前运行driver = webdriver.Remote(‘http://127.0.0.1:4723/wd/hub’, desired_caps)之后,连接就已经建立好了,只需在浏览器进入http://localhost:8100/inspector即可,之后就可以使用熟悉的driver.find_element_by_xxx方法来定位元素啦。
后记
Selenium的坑
后来又遇到了一点坑,例如使用send_keys方法时,报
Message: Parameters were incorrect. We wanted {“required”:[“value”]} and you sent [“text”,”sessionId”,”id”,”value”]
错误,google了一下发现是selenium新版导致的问题,降级后解决:
| 1 2 | pip uninstall selenium pip install selenium==3.3.1 | 
手势操作
由于XCUI的原因,之前的一些手势操作如swipe、pinch、TouchAction等都不能用了,可以参考这篇官方文档,使用driver.execute_script方法代替。如
| 1 2 | driver.execute_script('mobile: scroll', {'direction': 'down'})  # 向下滚动 driver.execute_script('mobile: dragFromToForDuration', {'duration': 0, 'fromX': 374, 'fromY': 115, 'toX': 200, 'toY': 100})  # 从右往左拖 | 
对于直接用坐标的,还要注意逻辑分辨率的问题,如iPhone 7 Plus的逻辑分辨率是414×736。
结语
刚接触iOS的Appium,之后肯定还会遇到问题,会继续更新本文。
更新:最近更新到了Appium Desktop 1.1,里面带了1.6.5的Appium,使用起来暂时未发现明显区别。
20180121更新
最近更新了Mac OS到10.13.2,XCode到9.2,Appium Desktop到1.3.1。上文的一些步骤可以简化了。
例如,WebDriverAgent用自带的没有问题,不需要重新clone一个。在打开WebDriverAgent.xcodeproj,成功编译运行到真机后,就可以在Appium Desktop中使用“Start Inspector Session”,填入capabilities,即可新建一个session进行inspect;或者选“Attach to Session”复用正在运行的session来inspect。如图:


试了一下,内置的Inspector可以支持iOS和Android的App,比以前的版本好用多了。
最后附上一些参考:
1. http://www.cocoachina.com/ios/20170112/18518.html
2. http://blog.sina.com.cn/s/blog_b5a76ebd0102wuce.html
3. https://github.com/appium/appium/blob/master/docs/en/appium-setup/real-devices-ios.md
4. http://blog.csdn.net/achang21/article/details/70877583
5. https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/caps.md
6. https://github.com/appium/python-client
7. https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/appium-bindings.md
8. https://github.com/facebook/WebDriverAgent/issues/537
9. https://github.com/facebook/WebDriverAgent/wiki/Using-the-Inspector
文章转载:http://www.7forz.com/2973/
后记:
在xcode构建的时候,signing账号要使用app 对应的账号不然会启动不了的

后记:
这篇好像很ok 但是没有试过
https://testerhome.com/topics/6962
22年8月
b站有了
https://www.bilibili.com/video/BV1tT4y137bD?p=8&vd_source=caabcbd2a759a67e2a3de8acbaaf08ea
虫师
https://www.cnblogs.com/fnng/p/12616466.html
 
                     
                    
                 
                    
                
 


 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号