在上一篇文章中,进了Android的坑,这次,要跳进更大更深的坑——iOS。 百度google了一轮,最大的感触是:好多教程都不适用啊!要么是Appium版本旧,要么是iOS版本旧。想找一篇详细的“从入门到放弃”的教程都没有,之前搭Android环境的时候,能搜到很多十分详实的教程,而iOS的就有点蛋疼了。 然而,坑还是要入的,所以,就从搭环境开始吧。
环境搭建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. AppiumAppium向来有命令行版的和GUI版的——我选择后者,到官网下载安装最新的Appium Desktop 1.0.2的dmg,里面带了1.6.4的Appium。 3. Appium客户端库Python、Ruby、Java、Javascript、PHP、C#等,任君选择,去官网下载。 例如我用Python,就安装Appium-Python-Client,在终端运行 | sudo easy_install pip # 系统自带easy_install pip install Appium-Python-Client --user # 加上--user是因为Mac下有权限的问题 |
4. HomebrewHomebrew相当于Linux下的apt-get、yum,要用它来安装node,在终端运行 | /usr/bin/ruby -e "$(curl -fsSL https://raw./Homebrew/install/master/install)" brew -v # 显示版本,如Homebrew 1.2.1 |
5. node | brew install node node -v # e.g. v7.10.0 |
6. appium-doctor相关用来检测Appium相关环境有没有装好的工具: | npm install -g appium-doctor # 装好之后 检测一下iOS的环境有没有配置好 如果不加--ios 则检测Android和iOS appium-doctor --ios # 它提示我缺少Xcode Command Line Tools和Carthage,那就补上 xcode-select --install brew install carthage |
7. 还有一些库 | 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一份才是王道: | 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/ rm -rf WebDriverAgent # 把自带的删掉 ln -s ~/WebDriverAgent WebDriverAgent # 用facebook的原版替换回去 |
经过了baidu和google,用以上方法解决了不能Inspect的问题。 在使用Appium时,需要把WDA装到真机上,然后又会遇到证书的问题,我也不是很明白,总之跟provisioning profile有关。 用Xcode打开目录下的WebDriverAgent.xcodeproj,对于WebDriverAgentLib 和 WebDriverAgentRunner,勾选“Automatically manage signing”,把Team改成公司的,Bundle Identifier改成公司的证书可以接受的名字,具体可以参考官方文档操作,不懂的找开发同学协助。
然后就可以把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新版导致的问题,降级后解决: | pip uninstall selenium pip install selenium==3.3.1 |
手势操作由于XCUI的原因,之前的一些手势操作如swipe、pinch、TouchAction等都不能用了,可以参考这篇官方文档,使用driver.execute_script方法代替。如 | 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,使用起来暂时未发现明显区别。 附上一些参考: 1. http://www./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
|