我的Appium学习记录—— iOS 10.3.2 + Appium Desktop 1.0.2 真机实战

===========分割线===========

20180121更新,升级到了Appium Desktop 1.3.1,详细更新内容见本文末,建议阅读本文前先看最近更新的内容。

===========分割线===========

上一篇文章中,进了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. 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,在终端运行

sudo easy_install pip   # 系统自带easy_install
pip install Appium-Python-Client --user  # 加上--user是因为Mac下有权限的问题

4. Homebrew

Homebrew相当于Linux下的apt-get、yum,要用它来安装node,在终端运行

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/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/
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改成公司的证书可以接受的名字,具体可以参考官方文档操作,不懂的找开发同学协助。

然后就可以把WebDriverAgentLib和WebDriverAgentRunner都编译到真机运行一下了。正常来说,会在桌面生成一个没图标的WebDriverAgentRunner,点开之后不会有什么反应,这就对了。

终于把环境搭好了,感动啊。

 

写测试脚本

1. Appium server capabilities

要让App跑起来,还需要了解Appium server capabilities,它告诉Appium服务器很多信息,例如开哪个App、手机的系统/版本、在哪台设备上跑(真机还是模拟器等)等。

给出我用到的一些参数(in Python),其他capabilities请参考官方文档

# -*- 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,使用起来暂时未发现明显区别。

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


已有50条评论 发表评论

  1. apple863 /

    写了很详细,感谢作者

    1. 7forz / 本文作者

      谢谢

      1. 匿名 /

        内置的Inspector加载页面结构很慢怎么解决啊

        1. 7forz / 本文作者

          不知道有多慢?10s?30s?60s?

  2. 烧饼 /

    可以加你qq吗?

    1. 7forz / 本文作者

      可以邮件,上班不能用qq,7f@7forz.com

      1. 烧饼 /

        好的

  3. 小雪 /

    感谢~~

    1. 7forz / 本文作者

      (●’◡’●)

  4. 打豆豆 /

    要哭,邮件发出去被退回,楼主宝宝,WebDriverAgentLib 和 WebDriverAgentRunner的具体操作能写一写嘛,卡在这一步了,开发童鞋也没能解决

    1. 7forz / 本文作者

      啊,看到邮件了,可能是QQ邮箱的过滤太敏感…
      这个我也不专业,大概就是说Bundle Identifier要和Team的对应上,Build Settings里面的Product Bundle Identifier也要改,之后就可以编译运行到手机上

      1. 打豆豆 /

        只改这两个的话,还是不能够编译到手机,会报如下两个错误
        Undefined symbols for architecture armv7:
        “_OBJC_CLASS_$_XCTRunnerDaemonSession”, referenced from:
        objc-class-ref in XCUIElement+FBTap.o
        ld: symbol(s) not found for architecture armv7
        clang: error: linker command failed with exit code 1 (use -v to see invocation)

        1. 7forz / 本文作者

          并没有遇到过 ╮(╯-╰)╭
          但是看报错感觉也不是证书的问题..

  5. 打豆豆 /

    更新了mac系统,就不报错了~

    1. 姜北生 /

      请问更新到哪个版本? 我的是10.12.6

  6. zhaodong /

    楼主你好,我打http://localhost:8100/inspector这报以下错误:求解答
    {
    “value” : “Invalid parameter not satisfying: path\n\n(\n\t0 CoreFoundation 0x000000018142ae50 + 148\n\t1 libobjc.A.dylib 0x0000000180a8ff80 objc_exception_throw + 56\n\t2 CoreFoundation 0x000000018142ad08 + 0\n\t3 Foundation 0x0000000181db0124 + 112\n\t4 WebDriverAgentLib 0x00000001020d1298 -[FBResponseFilePayload initWithFilePath:] + 284\n\t5 WebDriverAgentLib 0x00000001020c926c FBResponseFileWithPath + 84\n\t6 WebDriverAgentLib 0x00000001020dee1c __29+[FBInspectorCommands routes]_block_invoke + 100\n\t7 WebDriverAgentLib 0x00000001020c958c -[FBRoute_Sync mountRequest:intoResponse:] + 168\n\t8 WebDriverAgentLib 0x00000001020c0b78 __37-[FBWebServer registerRouteHandlers:]_block_invoke + 496\n\t9 RoutingHTTPServer 0x00000001067d23cc -[RoutingHTTPServer handleRoute:withRequest:response:] + 144\n\t10 RoutingHTTPServer 0x00000001067d2b80 __72-[RoutingHTTPServer routeMethod:withPath:parameters:request:connection:]_block_invoke + 44\n\t11 libdispatch.dylib 0x0000000180e7547c + 16\n\t12 libdispatch.dylib 0x0000000180e84ae8 + 644\n\t13 libdispatch.dylib 0x0000000180e7547c + 16\n\t14 libdispatch.dylib 0x0000000180e7ab84 _dispatch_main_queue_callback_4CF + 1844\n\t15 CoreFoundation 0x00000001813e0dd8 + 12\n\t16 CoreFoundation 0x00000001813dec40 + 1628\n\t17 CoreFoundation 0x0000000181308d10 CFRunLoopRunSpecific + 384\n\t18 Foundation 0x0000000181d18d8c + 308\n\t19 Foundation 0x0000000181d6dff8 + 88\n\t20 WebDriverAgentLib 0x00000001020bfcd0 -[FBWebServer startServing] + 260\n\t21 WebDriverAgentRunner 0x00000001020afed0 -[UITestingUITests testRunner] + 120\n\t22 CoreFoundation 0x0000000181430ae0 + 144\n\t23 CoreFoundation 0x0000000181328548 + 284\n\t24 XCTest 0x00000001000e60a8 __24-[XCTestCase invokeTest]_block_invoke_2 + 388\n\t25 XCTest 0x000000010011ac98 -[XCTestContext performInScope:] + 208\n\t26 XCTest 0x00000001000e5f0c -[XCTestCase invokeTest] + 268\n\t27 XCTest 0x00000001000e65e0 -[XCTestCase performTest:] + 460\n\t28 XCTest 0x00000001000e3a5c -[XCTestSuite performTest:] + 428\n\t29 XCTest 0x00000001000e3a5c -[XCTestSuite performTest:] + 428\n\t30 XCTest 0x00000001000e3a5c -[XCTestSuite performTest:] + 428\n\t31 XCTest 0x00000001000cf740 __25-[XCTestDriver _runSuite]_block_invoke + 56\n\t32 XCTest 0x00000001000f0260 -[XCTestObservationCenter _observeTestExecutionForBlock:] + 528\n\t33 XCTest 0x00000001000cf5d8 -[XCTestDriver _runSuite] + 460\n\t34 XCTest 0x00000001000d03b4 -[XCTestDriver _checkForTestManager] + 296\n\t35 XCTest 0x000000010011c164 _XCTestMain + 628\n\t36 CoreFoundation 0x00000001813e100c + 20\n\t37 CoreFoundation 0x00000001813e0944 + 308\n\t38 CoreFoundation 0x00000001813de8a8 + 708\n\t39 CoreFoundation 0x0000000181308d10 CFRunLoopRunSpecific + 384\n\t40 GraphicsServices 0x0000000182bf0088 GSEventRunModal + 180\n\t41 UIKit 0x00000001865ddf70 UIApplicationMain + 204\n\t42 XCTRunner 0x00000001000a83d4 XCTRunner + 33748\n\t43 libdyld.dylib 0x0000000180ea68b8 + 4\n)”,
    “sessionId” : “111B2BBF-F287-40C8-BC09-86C3882FC8EE”,
    “status” : 13
    }

    1. 7forz / 本文作者

      你好,虽然我没有遇到,想问一下,①用的是不是FB的WDA;②编译有没有报错?

      1. zhaodong /

        嗯是的,编译没问题啊,我用appium客户端是可以定位的,但是用你那个方法就是这样。

        1. 7forz / 本文作者

          啊..(T_T) 另外请教一下,用appium客户端如何定位?appium版本是?适用的iOS版本?

        2. lisen /

          我也出现类似问题。不知道如何查找元素。希望大神指教

    2. 匿名 /

      我现在也是这样的状态,请问你最后解决了吗?如果解决是如何解决的?

    3. lirenwei /

      大佬你这个问题解决了吗,我现在也遇到了一样的问题。

  7. 匿名 /

    我在浏览器里输入http://localhost:8100/inspector直接是不能连接到服务器

  8. 匿名 /

    xudongzhideiMac:WebDriverAgent admin$ ./Scripts/bootstrap.sh
    Fetching dependencies
    Please update to the latest Carthage version: 0.26.0. You currently are on 0.25.0
    *** Downloading RoutingHTTPServer.framework binary at “v1.0.1”
    *** xcodebuild output can be found in /var/folders/lq/_rh3p0dj6756np74dsm6mrmr0000gn/T/carthage-xcodebuild.tojvt5.log
    Building Inspector
    Creating bundle directory…
    Fetching Inspector dependencies…
    npm ERR! code E404
    npm ERR! 404 Not Found: babel-loader@^5.3.2

    npm ERR! A complete log of this run can be found in:
    npm ERR! /Users/admin/.npm/_logs/2017-10-12T02_12_32_820Z-debug.log
    大神 你好 我在输入./Scripts/bootstrap.sh时总是报404

    1. 7forz / 本文作者

      1. 提示更新Carthage
      2. npm尝试换个镜像?
      3. 查看对应log file,看有没有更进一步的信息?

  9. xc /

    大佬,求助!快被这个错误搞死了。我的xcode9.2,ios10.3.2,appium
    desktop1.3.1。
    [XCUITest] Not clearing log files. Use `clearSystemFiles` capability to turn on.
    [iOSLog] Stopping iOS log capture
    [MJSONWP] Encountered internal error running command: Error: App with bundle identifier ‘com.zxx.apps’ unknown
    at Object.wrappedLogger.errorAndThrow (/Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/appium-support/lib/logging.js:69:13)
    at XCUITestDriver.start$ (/Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/appium-xcuitest-driver/lib/driver.js:318:13)
    at tryCatch (/Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:67:40)
    at GeneratorFunctionPrototype.invoke [as _invoke] (/Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:315:22)
    at GeneratorFunctionPrototype.prototype.(anonymous function) [as next] (/Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:100:21)
    at GeneratorFunctionPrototype.invoke (/Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/babel-runtime/regenerator/runtime.js:136:37)
    at
    [HTTP] <– POST /wd/hub/session 500 1565 ms – 203

  10. 大智 /

    楼主,您好,我环境(IOS11,XCODE9.2,desktop1.3.1)已经准备OK但是在用desktop取元素的时候,webveiw的元素,根本定位不了,不知道您遇到了没有?如何解决的,现在的APP都是混合开发,免不了遇到webveiw。如果搞不定有啥策略么

    1. 7forz / 本文作者

      你好,我司的都是原生的App,所以我还没有试过webview。粗略找了一下,百度关键词”webview远程调试”,希望能帮到你

      1. 匿名 /

        楼主你好,你有在iOS真机上进行过app测试么,我目前前期环境都已经调通了,appium能够打开iOS上的app,但是接下来不知道在哪里编写脚本进行自动化测试,期待回复

        1. 7forz / 本文作者

          本文中有写呀,我用的是Python的客户端,还支持Java PHP C# ruby等语言

    2. ET /

      请问这个问题解决了吗?怎么解决的呢?

  11. 匿名 /

    内置的Inspector可以支持iOS加载页面结构很慢,有时候加载不出来

    1. 小颗粒 /

      我也有些页面结构加载不出来,不知道什么原因,大部分页面是能加载查看元素的。请问有什么好的解决办法了吗?

  12. 匿名 /

    感谢作者的无私分享

  13. 杨跃 /

    -bash: cd: /Applications/Appium.app/Contents/Resources/app/node_modules/appium/node_modules/appium-xcuitest-driver/: No such file or directory
    为什么没这个appium-xcuitest-driver

    1. 7forz / 本文作者

      新版的目录换了,看看 /Applications/Appium.app/Contents/Resources/app/node_modules/appium-xcuitest-driver/ 有没有?

      1. 杨跃 /

        求+qq 指导一下 417993207

      2. 杨跃 /

        这个路径的编译报错

  14. likelin /

    Appium Desktop 1.3.1 缺少 RoutingHTTPServer 文件

  15. 帅帅 /

    太谢谢作者了最近正为了这个问题烦恼呢!!!

  16. 匿名 /

    appiumV1.8.1
    自带的WebDriverAgent
    self.driver.execute_script(‘mobile: swipe’, {‘direction’:’left’, ‘element’: el.id})看不到滑动效果,也不报错

  17. 匿名 /

    楼主现在有用到mobile: selectPickerWheelValue吗,
    我的时间控件滑动老是闪退
    self.driver.execute_script(‘mobile: selectPickerWheelValue’, {‘order’:’previous’, ‘offset’: 0.3, ‘element’: elementID})
    可以帮我看看是哪里出问题么

  18. 匿名 /

    我用的appium版本1.8 ,python语言进行iOS 测试 ,请问有什么方法可以在我们测试的app里面下拉iOS的通知栏吗 ,试过了 swipe ,flick 都出错了 ,用touchaction也是没反应,求指点

    1. 7forz / 本文作者

      我暂时没有找到,找了https://github.com/appium/appium/tree/master/docs/en/commands 和https://github.com/appium/appium/blob/master/docs/en/commands/mobile-command.md ,而Android倒是有办法

  19. 来自深圳的测试仔 /

    我有个疑问一直困惑着我:开发打给我们的ipa文件包签名是他们的开发者账号,然后我在wda上设置的是我个人的开发者账号,这样start session的时候就会报错,解决方法是开发给源码我,我用我自己的开发者账号去打包或者让开发人员把账号给我用?是这样吗?

    1. 7forz / 本文作者

      最近没接触这个了,记得当时是在xcode上导入某个文件,然后就相当于用开发者的账号

  20. 匿名 /

    inspector能在浏览器里边通过http://localhost:8100/inspector这个地址访问?

  21. 匿名 /

    我用的是window,安卓,appium desktop1.10.0,没能找到在浏览器里边访问inspector的方法

    1. 7forz / 本文作者

      之前还写过Android的,https://www.7forz.com/2905/

回复给7forz 取消回复