Calabash Calabash Android 使用教程 (二)

詹金辉 · 2015年07月21日 · 最后由 Magicchn 回复于 2018年05月04日 · 3232 次阅读

上一节整理了 Calabash-Android 的安装过程,这一节找一个测试 demo 来体验一下 UI 测试

Calabash Android 使用教程 (一)

一、准备工作

  1. 新建一个目录,用于存放 apk 和对应的测试用例,这里我新建了 E:\Android_Test 目录
  2. 测试 apk,不需要源码,这里使用微信最新版本,将 apk 拷贝到 E:\Android_Test 目录

    weixin622android580.apk

  3. 需要一个 android 签名,本例中使用 Android SDK 自带的默认 debug.keystore

  4. 按 Win 键 +R 键,输入 cmd,打开 cmd 后,输入 cd /d E:\Android_Test,然后在该目录下运行 calabash-android gen,按回车确认,生成用例目录结构,如图

    可以看到在 E:\Android_Test 目录下生成了一个 features 目录,features 目录下有个 my_first.feature 文件,这个就是用例文件,另外生成了两个文件夹,一个 step_definitions 用来存放一些自定义 step 文件,一个 support 存放初始化文件

二、给 apk 重新签名

由于 Calabash-Android 不能直接测试运行 apk,需要给 apk 重新签名,在 cmd 中输入 calabash-android resign weixin622android580.apk,如图

如果没有任何提示,则说明重新签名成功

注:重新签名有时候会提示找不到 keystore,这时就需要根据签名信息设置签名文件,输入 calabash-android setup 进行签名设置

三、运行测试用例

首先我们编辑前面生成的 my_first.feature 文件,内容如下

Feature: 启动功能
        Scenario: 启动应用,并给应用截图
            Then I take a screenshot

然后在 E:\Android_Test 目录运行 calabash-android build weixin622android580.apk 会发现如下错误,可以看出是 GBK 编码导致的,我们需要修改 cmd 控制台编码,输入 chcp 65001 进行编码切换,然后继续运行 calabash-android build weixin622android580.apk

编译成功后,查看提示知道,在 E:\Android_Test 目录下面生成了一个 test_servers/******************_0.5.12.apk 文件,这就是用于安装到测试机上面的测试服务 apk

最后,被测 apk 重新签名了,测试服务也编译成功了,下面就是执行写的用例文件 my_first.feature 了,输入 calabash-android run weixin622android580.apk,注意先将编码切换成 GBK,否则会导致用例中有中文出现执行失败,执行 chcp 936 切换 GBK


可以看到,用例已经成功执行,在测试目录下生成了一张微信闪屏截图

说明:在 ubuntu 下面使用同样的步骤执行简单的用例,有一个优点就是不用进行编码切换,省去了 cmd 里面容易出错的编码切换问题

共收到 17 条回复 时间 点赞

感谢分享!!!

看到回复的楼主和各位前辈,刚接触 calabash,按着帖子的例子来,到 run 的时候出现 Unable to activate calabash-android-0.5.14, because cucumber-2.1.0 conflicts with cucumber (~> 1.3.17) (Gem::ConflictError)。求指点!

@luoq 知道了,之前单独 gem cucumber install 了,之后在 gem calabash-android install 时又会安装一个 cucumber。

build 过程中提示 “Could not find an Android SDK please make sure it is installed.” 可是我的电脑已经下载很多 Android SDK,android 开发打包都是正常的

运行 calabash-android build weixin622android580.apk 出现以下错误
F:/softwarepath/Ruby200-x64/Ruby200-x64/lib/ruby/gems/2.0.0/gems/calabash-android-0.7.2/lib/calabash-android/dependencies.rb:49:in java_dependencies': No such file '\Program Files\Java\jdk1.7.0_79\bin/keytool.exe' (RuntimeError)
from F:/softwarepath/Ruby200-x64/Ruby200-x64/lib/ruby/gems/2.0.0/gems/calabash-android-0.7.2/lib/calabash-android/dependencies.rb:81:in
keytool_path'
from F:/softwarepath/Ruby200-x64/Ruby200-x64/lib/ruby/gems/2.0.0/gems/calabash-android-0.7.2/lib/calabash-android/java_keystore.rb:9:in initialize'
from F:/softwarepath/Ruby200-x64/Ruby200-x64/lib/ruby/gems/2.0.0/gems/calabash-android-0.7.2/lib/calabash-android/java_keystore.rb:80:in
new'
from F:/softwarepath/Ruby200-x64/Ruby200-x64/lib/ruby/gems/2.0.0/gems/calabash-android-0.7.2/lib/calabash-android/java_keystore.rb:80:in read_keystore_with_default_password_and_alias'
from F:/softwarepath/Ruby200-x64/Ruby200-x64/lib/ruby/gems/2.0.0/gems/calabash-android-0.7.2/lib/calabash-android/java_keystore.rb:99:in
get_keystores'
from F:/softwarepath/Ruby200-x64/Ruby200-x64/lib/ruby/gems/2.0.0/gems/calabash-android-0.7.2/bin/calabash-android-build.rb:5:in calabash_build'
from F:/softwarepath/Ruby200-x64/Ruby200-x64/lib/ruby/gems/2.0.0/gems/calabash-android-0.7.2/bin/calabash-android:71:in
'
from F:/softwarepath/Ruby200-x64/Ruby200-x64/bin/calabash-android:23:in load'
from F:/softwarepath/Ruby200-x64/Ruby200-x64/bin/calabash-android:23:in

'

运行 calabash-android run weixin622android580.apk 后 calabash 才正常运行

#5 楼 @maestro 我也遇到了同样的问题,请问你解决了吗?可以分享下解决方法么?

#5 楼 @maestro
我也遇到了同样的问题,一直在看该如何解决?楼主帮忙分享下啊

5 楼的问题我也遇到了,应该是在找 keytool.exe 的时候,路径没有带盘符,所以找不到,尝试了下有两种办法可以解决:
1.将 calabash-android gen 生成的内容移到和\Program Files...同一个目录层级下;
2.修改 dependencies.rb 中 path_elements 函数,将 ENV['PATH'].split (/[:;]/)中,冒号去掉,这样不会根据冒号拆分,路径就会正确。

生成的 test_servers/******************_0.5.12.apk 是要安装到 模拟机上么? 我尝试过安装不上去

运行的时候显示:

H:\Android_Test>calabash-android run weixin622android580.apk
*** WARNING: You must use ANSICON 1.31 or higher (https://github.com/adoxa/ansicon/) to get coloured output on Windows
Feature: 启动功能

2402 KB/s (585905 bytes in 0.238s)
2631 KB/s (32003640 bytes in 11.876s)
系统找不到指定的路径。
Scenario: 启动应用,并给应用截图 # features/my_first.feature:3
Could not execute command to start test server (RuntimeError)
./features/support/app_life_cycle_hooks.rb:5:in Before'
Then I take a screenshot # calabash-android-0.7.3/lib/calabash-android/steps/screenshot_steps.rb:9
No connection could be made because the target machine actively refused it. - connect(2) for "127.0.0.1" port 34777 (127.0.0.1:34777) (Errno::ECONNREFUSED)
./features/support/app_life_cycle_hooks.rb:12:in
After'

Failing Scenarios:
cucumber features/my_first.feature:3 # Scenario: 启动应用,并给应用截图

1 scenario (1 failed)
1 step (1 skipped)
0m41.799s

最后虽然有截图,可是运行出错了 ,为什么呢?

楼主,实际使用过程中, 有没有遇到过偶尔 touch 事件执行后, app 没有反应?

你好,楼主我遇到一个问题你看看不能帮我看一下
No test server found for this combination of app and calabash version. Recreating test server.
D:/Ruby/Ruby22-x64/lib/ruby/gems/2.2.0/gems/calabash-android-0.8.0/lib/calabash-android/helpers.rb:18:in package_name': 'package' not found in aapt output (RuntimeError)
from D:/Ruby/Ruby22-x64/lib/ruby/gems/2.2.0/gems/calabash-android-0.8.0/bin/calabash-android-build.rb:41:in
block (2 levels) in calabash_build'
from D:/Ruby/Ruby22-x64/lib/ruby/gems/2.2.0/gems/calabash-android-0.8.0/bin/calabash-android-build.rb:37:in chdir'
from D:/Ruby/Ruby22-x64/lib/ruby/gems/2.2.0/gems/calabash-android-0.8.0/bin/calabash-android-build.rb:37:in
block in calabash_build'
from D:/Ruby/Ruby22-x64/lib/ruby/2.2.0/tmpdir.rb:88:in mktmpdir'
from D:/Ruby/Ruby22-x64/lib/ruby/gems/2.2.0/gems/calabash-android-0.8.0/bin/calabash-android-build.rb:36:in
calabash_build'
from D:/Ruby/Ruby22-x64/lib/ruby/gems/2.2.0/gems/calabash-android-0.8.0/lib/calabash-android/helpers.rb:106:in build_test_server_if_needed'
from D:/Ruby/Ruby22-x64/lib/ruby/gems/2.2.0/gems/calabash-android-0.8.0/bin/calabash-android-run.rb:16:in
calabash_run'
from D:/Ruby/Ruby22-x64/lib/ruby/gems/2.2.0/gems/calabash-android-0.8.0/bin/calabash-android:84:in <top (required)>'
from D:/Ruby/Ruby22-x64/bin/calabash-android:22:in
load'
from D:/Ruby/Ruby22-x64/bin/calabash-android:22:in `

'

刚刚解决了签名问题又出现了这个问题,求帮助

Scenario: start function and take a screenshot # features/my_first.feature:2
com.xx.xx did not get installed. Reason: ''. Aborting! (RuntimeEr
ror)
./features/support/app_installation_hooks.rb:20:in `Before'

这个是什么情况呢?楼主帮忙看下,谢谢呢


我用模拟器调试的,两个 apk 都不成功

重签名时报错,找了很久没找到问题,这个是什么问题,

分析原因,看到第一部分说道高版本 ruby 不支持,遂改为第一篇文章中推荐的 2.0 版本,重签名问题搞定,用例跑通(第一条用例是复制原文件,这个是必定失败的)

发现的问题

当进行编译时(build),出现上面这个报错,可以忽略,直接运行 apk 可成功。本人亲测两个 APP 可成功运行脚本:一个商城,一个蜂鸟众包。
注:不要试图删掉报错的文件夹,不管用,因为文件夹本身就是空的,而且这个生成的文件夹也是必须要有。否则重签名时报错

需要 登录 後方可回應,如果你還沒有帳號按這裡 注册