ios环境的搭建比android稍微复杂,一不小心就会掉入大坑(o´゚□゚`o)
ios环境必须在mac系统里运行,建议用真机,不要用虚拟机。
Xcode
直接在App Store下载安装就好了。Xcode最好是用最新版的,不然版本高的iphone可能会不支持。
Appium desktop
直接在官方的GitHub下载安装就行了,这个没什么难度。https://github.com/appium/appium-desktop/releases
有一点要注意的是必须先安装Xcode再安装Appium desktop,不然后续跑起来会报错。
(在写这篇文章的时候,最新版为1.6.1。但是我用起来有问题,会报
appium Cannot read property 'async' of undefined
的错,后来换了1.6.0就没问题了。不知道是版本的问题还是我哪一步出了问题,这里先记录一下)
python
下载安装python和相应的IDE,这里就详细叙述了。(这里使用的是python3)
使用pip安装python-client:pip install Appium-Python-Client
Homebrew
Homebrew相当于Linux下的apt-get、yum,要用它来安装node,在终端运行以下口令就可以安装
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
终端运行brew -v
看有没安装成功,如果成功的会显示版本如
yauloladeMacBook-Air:WebDriverAgent yaulola$ brew -v
Homebrew 1.6.4
Homebrew/homebrew-core (git revision ecf4; last commit 2018-05-23)
node
安装好Homebrew后在终端运行brew install node
就可以安装,同样用node -v
去验证有没安装成功
yauloladeMacBook-Air:WebDriverAgent yaulola$ node -v
v10.0.0
appium-doctor
appium-doctor这个工具可以用来检测Appium相关环境有没有安装好
在终端输入npm install -g appium-doctor
来安装
如果报错了,可以试试在命令前加sudo:sudo npm install -g appium-doctor
这里会要求输入电脑密码
安装好之后我们可以检测一下ios的环境有没有配置好
终端输入appium-doctor --ios
我用的时候它提示我WARN AppiumDoctor ✖ Carthage was NOT found!
那我就安装Carthage吧:brew install carthage
一些相关的库
brew install libimobiledevice --HEAD
npm install -g ios-deploy
WebDriverAgent
WebDriverAgent是最麻烦最容易踩坑的地方,在这里一定要小心翼翼的!d(・`ω´・d*)
我们先在终端cd到WebDriverAgent目录下新建Resources/WebDriverAgent.bundle文件,WebDriverAgent一般是在/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver
这个目录,如果不是的话请自行百度
终端运行:
cd /Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/WebDriverAgent
mkdir -p Resources/WebDriverAgent.bundle
然后再运行:
./Scripts/bootstrap.sh
这里可能会有2条报错,我都忽略了,后面也没什么问题。
接下来打开/Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/WebDriverAgent/WebDriverAgent.xcodeproj
默认是用xcode打开
对于WebDriverAgentLib 和 WebDriverAgentRunner,勾选“Automatically manage signing”,把Team改成公司的。这里会需要开发者账号,可以让开发的同学把自己的appleID加入开发团队。(这里我踩了一个坑,在选择团队的时候会一直报账号密码错误,百度了好久后来终于找到解决方法,就是在钥匙串访问中把左上角的锁🔐锁上,然后再解锁即可。具体怎么在哪里打开钥匙串访问请自行百度)
然后点击如下步骤把WebDriverAgentLib和WebDriverAgentRunner都编译到真机
这里因为我手机没有连电脑所以显示的是No devices,但是做这一步必须连接手机并把WebDriverAgentLib和WebDriverAgentRunner编译到手机里
只要这一步没有报错,我们就基本完成环境的搭建。
验证
现在我们需要验证我们的环境有没有搭建好,我们编写以下代码并开启appium服务
from time import sleep
from appium import webdriver
desired_caps = {}
desired_caps['automationName'] = 'XCUITest'
desired_caps['platformName'] = 'iOS'
desired_caps['platformVersion'] = '11.3.1'
desired_caps['deviceName'] = 'iPhone 6 Plus'
desired_caps['bundleId'] = ' '
desired_caps['udid'] = '3d0a0a1f35072378bbdb7d6011c0fd36df6c0939'
desired_caps['newCommandTimeout'] = 3600
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
sleep(30)
driver.quit()
XCUITest是ios的自动化框架
bundleId类似安卓的appPackage,这个需要跟开发同学要。(这个是必填的,因为保密原因所以我这里不显示出来)
udid是每台iphone独特的一种id,可以连接电脑在iTunes里获取
newCommandTimeout指在终止尝试和产生错误之前执行命令期间需等待的时间。
运行代码,当没有报错并手机自动开打了要测的app即为搭建环境成功。如果还有报错就请自行百度了( ̄▽ ̄)~*