Appium Appium 开发环境搭建 (2)--执行官方的测试用例

陈恒捷 · 2015年02月24日 · 最后由 陈恒捷 回复于 2015年03月22日 · 2107 次阅读
本帖已被设为精华帖!

这是一个系列文章,完整的合集链接:Appium 开发环境搭建合集

通过Appium 开发环境搭建(1)-- 配置源码运行环境,我们让 appium 通过源码跑了起来,但对于开发这还不够。在修改完源代码后,我们必须 运行一些基础的测试来保证我们的代码没有破坏原有的功能。下面我们来看看如何执行 appium 的中已经写好的自动化测试用例。

Unit Tests

appium 有一个很简单且执行速度很快的单元测试。只需要一条命令就能执行:

grunt unit

如果提示没有 grunt 命令,执行下面语句:

npm install -g mocha
npm install -g grunt-cli

执行结果大致如下:

$ grunt unit
Running "exec:gulp-test-unit" (exec) task
[21:57:32] Using gulpfile ~/Develop/appiumSourceCode/appium/gulpfile.js
[21:57:32] Starting 'test-unit'...
 10  -_-_-_-_-_-_,------,
 0   -_-_-_-_-_-_|   /\_/\
 0   -_-_-_-_-_-^|__( ^ .^)
     -_-_-_-_-_-  ""  ""

  10 passing (67ms)


>> error: Permission to start activity denied.
>> error: Activity used to start app doesn't exist or cannot be launched! Make sure it exists and is a launchable activity
 1   -__,------,
 0   -__|  /\_/\
 0   -_~|_( ^ .^)
     -_ ""  ""

  1 passing (12ms)


warn: [DEPRECATED] The name locator strategy has been deprecated and will be removed.  Please use the accessibility id locator strategy instead.
 18  -_-_-_-_-_-_-_-_-_-_,------,
 0   -_-_-_-_-_-_-_-_-_-_|   /\_/\
 0   -_-_-_-_-_-_-_-_-_-^|__( ^ .^)
     -_-_-_-_-_-_-_-_-_-  ""  ""

  18 passing (49ms)


warn: Converting cap app to string, since it was an object. This might be a user error. Original value was: {"some":"object"}
warn: Converting cap stringFalseCap from string to boolean. This might cause unexpected behavior.\
warn: Converting cap stringTrueCap from string to boolean. This might cause unexpected behavior.
 10  -_-_-_-_-_-_,------,
 0   -_-_-_-_-_-_|   /\_/\
 0   -_-_-_-_-_-^|__( ^ .^)
     -_-_-_-_-_-  ""  ""

  10 passing (39ms)


warn: [DEPRECATED] The old function has been deprecated and will be removed.  Please use the new function instead.
warn: [DEPRECATED] The old function has been deprecated and will be removed.  Please use the new function instead.
warn: [DEPRECATED] The old function has been deprecated and will be removed.  Please use the new function instead.
warn: [DEPRECATED] The YeOlde function has been deprecated and will be removed.  Please use the Moderne function instead.
warn: [DEPRECATED] The old function has been deprecated and will be removed.  Please use the new function instead.
warn: [DEPRECATED] You used 1 deprecated capabilities during this session.  Please check the logs as they will be removed in a future version of Appium.
 9   -_-_-_-_-__,------,
 0   -_-_-_-_-__|  /\_/\
 0   -_-_-_-_-_~|_( ^ .^)
     -_-_-_-_-_ ""  ""

  9 passing (66ms)


 9   -_-_-_-_-__,------,
 0   -_-_-_-_-__|  /\_/\
 0   -_-_-_-_-_~|_( ^ .^)
     -_-_-_-_-_ ""  ""

  9 passing (26ms)


 39  -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-__,------,
 0   -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-__|  /\_/\
 0   -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_~|_( ^ .^)
     -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_ ""  ""

  39 passing (78ms)


info: Will try to parse the plist file as XML
info: Will try to parse the plist file as XML
warn: Could not parse app unknown_file assuming it doesn't exist
 6   -_-_-_-_,------,
 0   -_-_-_-_|   /\_/\
 0   -_-_-_-^|__( ^ .^)
     -_-_-_-  ""  ""

  6 passing (658ms)


 1   -__,------,
 0   -__|  /\_/\
 0   -_~|_( ^ .^)
     -_ ""  ""

  1 passing (6ms)


info: Shutting down appium session
info: Found an existing session to clobber, shutting it down first...
info: Shutting down appium session
info: Old session shut down OK, proceeding to new session
info: Found an existing session to clobber, shutting it down first...
info: Shutting down appium session
info: Old session shut down OK, proceeding to new session
info: Found an existing session to clobber, shutting it down first...
info: Shutting down appium session
info: Old session shut down OK, proceeding to new session
info: Found an existing session to clobber, shutting it down first...
info: Shutting down appium session
info: Old session shut down OK, proceeding to new session
info: Found an existing session to clobber, shutting it down first...
info: Shutting down appium session
info: Old session shut down OK, proceeding to new session
info: Found an existing session to clobber, shutting it down first...
info: Shutting down appium session
info: Old session shut down OK, proceeding to new session
info: Found an existing session to clobber, shutting it down first...
info: Shutting down appium session
info: Old session shut down OK, proceeding to new session
info: Found an existing session to clobber, shutting it down first...
info: Shutting down appium session
info: Old session shut down OK, proceeding to new session
info: Found an existing session to clobber, shutting it down first...
info: Shutting down appium session
info: Old session shut down OK, proceeding to new session
info: Found an existing session to clobber, shutting it down first...
info: Shutting down appium session
info: Old session shut down OK, proceeding to new session
 3   -_-__,------,
 0   -_-__|  /\_/\
 0   -_-_~|_( ^ .^)
     -_-_ ""  ""

  3 passing (538ms)


[21:57:35] Finished 'test-unit' after 3.53 s

Done, without errors.

简单分析一下:
Grunt是一个 javascript 任务执行器(task runner),主要用来自动化执行一些任务 (task),如部署、测试等。这里使用grunt unit执行了名叫 unit 的任务。
所有 grunt 的 task 的作用和用法可以看 appium 的说明:https://github.com/appium/appium/blob/master/docs/en/contributing-to-appium/grunt.md

Functional Tests

appium 也有不少自动化的功能性测试。可通过以下命令执行所有的测试

bin/test.sh

也可以自己选择要执行某个类别的测试。如:

bin/test.sh --android
bin/test.sh --ios
bin/test.sh --ios7

注意:所有功能测试执行前 必须先通过node .打开从源码运行的 appium server, 并执行./reset.sh --dev来下载和配置所有需要用到的测试 app

执行reset.sh中有一个小问题,可能会导致TestAppbuild 失败(执行结果是 success 的,但是运行 functional test 时会报错)。reset.sh中运行 ios 的 build TestApp 的相关语句为

203         echo "* Cleaning/rebuilding iOS test app: TestApp"
204         run_cmd "$grunt" buildApp:TestApp:iphonesimulator:$sdk_ver

其中$sdk_ver通过xcrun --sdk iphonesimulator --show-sdk-version 2>/dev/null获取。因为此处的iphonesimulator$sdk_ver之间多了一个冒号:,所以实际上 build app 时使用的是默认的iphonesimulator,而不是指定 sdk 的 simulator,如iphonesimulator8.1。在我的环境下这导致了TestApp.app无法 build 成功,最终我是手动执行grunt buildApp:TestApp:iphonesimulator8.1来 build 这个 app 的。
20150226 update:sdk 参数直接使用iphonesimulator等同于使用最新的 sdk。例如你的 mac 上最新的 sdk 是 8.1,那么iphonesimulator等同于iphonesimulator8.1。不过目前不是很清楚为啥这里会有两种用法(build WebView.app 时用的是run_cmd "$grunt" buildApp:TestApp:iphonesimulator$sdk_ver
参考资料:http://stackoverflow.com/questions/4314804/parameter-for-xcodebuild-for-using-latest-sdk/5596988#5596988

后面运行用例的过程中如果出现红字(failed), 且 appium server 的 log 里面说明找不到某个 app,可以使用文章末尾 Q&A 的方法解决。如果执行过程中出现其他问题,欢迎大伙跟帖提出,我会补充到 Q&A 中

为了方便大家了解大致有多少种 functional test 可以执行,这里简单列举一下 test.sh 的所有参数 (这里吐槽一下,竟然不能使用test.sh --help这种形式获得所有参数...):

--android
--android-chrome
--selendroid
--gappium
--ios6
--ios7
--ios71
--ios8
--ios81
--ios82
--no-xcode-switch
--real-device

如果没有添加任何参数,默认运行所有测试。

如果只是想执行单独的测试,可以使用下面的命令:

  • 运行某个测试文件的所有测试 DEVICE=ios81 mocha -t 60000 -R spec test/functional/ios/testapp/simple-specs.js
  • 运行某个文件夹内的某个类型的测试用例 (例子中是运行名称中含有"alert"的用例,test/functional/ios/uicatalog是一个文件夹): DEVICE=ios81 mocha -t 60000 -R spec --grep "alert" test/functional/ios/uicatalog 注意:
  • 官方文档上的描述 DEVICE 的值只能是ios71, ios6, android, selendroid,但实际上测试ios81也是可以的。
  • 官方文档描述运行 android 的测试用例必须在 4.0 英寸且分辨率为 480x800 的设备/模拟器。否则部分用例可能会因此失败。

所有测试用例都放在appium/test文件夹下。大家有兴趣可以自己进去研究一下。大部分测试用例都是使用mocha框架写的。

总结

  1. Appium 的自动化测试主要分为 unit test 和 functional test。其中 unit test 的测试用例放在appium/test/unit,funtional test 的用例放在appium/test/functional
  2. Appium 所有的任务(包括我们使用./reset.sh里执行的任务)都是使用grunt来执行的,因此出错后其实我们也可以单独使用 grunt 运行对应 task。这也是为什么./reset.sh的出错提示里会说用--force来强制执行,但加入--force却没有效果的原因(--force没有传递到实际执行 task 的语句里面)。
  3. Appium 官方的自动化测试用例主要使用mocha框架编写和执行。
  4. 可以通过使用mocha的执行命令来执行部分的用例(文件或用例名称)。

Q&A

执行bin/test.sh --ios81过程中很多用例出现红色的"before all" hook,appium server 提示App paths need to be absolute...

  • 原因分析:有些 app 没有被 build 成功,所以找不到.app(ios) 或.apk(android) 的应用安装文件。
  • 解决方案:先运行./reset.sh --dev重新下载和 build 所有测试 app。如果还是不成功,请执行对应的 buildApp 任务。此处以 ios 的TestApp.app应用为例: 执行 grunt buildApp:TestApp:iphonesimulator8.1 来编译TestApp(此处的 simulator 版本请按照自己环境进行配置,可通过xcodebuild -showsdks查看当前可以使用的所有 simulator 版本),然后再去执行 ios 的功能测试用例。
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 5 条回复 时间 点赞

学习了

$ grunt unit
grunt-cli: The grunt command line interface. (v0.1.13)

Fatal error: Unable to find local grunt.
这是为什么?求助啊,谢谢 l~

#4 楼 @hssdx 麻烦提供一下你运行这个命令时所在目录路径及你配置的 appium 源码根目录路径,以及你是否运行成功了reset.sh --dev命令。

陈恒捷 Appium 开发环境搭建合集 中提及了此贴 02月22日 20:42
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册