Calabash Calabash Android 使用教程 (二)

詹金辉 · July 21, 2015 · Last by Magicchn replied at May 04, 2018 · 3315 hits

上一节整理了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可成功运行脚本:一个商城,一个蜂鸟众包。
注:不要试图删掉报错的文件夹,不管用,因为文件夹本身就是空的,而且这个生成的文件夹也是必须要有。否则重签名时报错

需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up