上文介绍了如何在windows中安装calabash-android的测试环境,本文用一个实例来介绍calabash-android的基本用法。
本文所用的源代位于:https://github.com/bigconvience/HackerNews
下载完代码后,进入工程目录中,在命令行中输入:calabash-android gen,此命令会在工程目录下生成目录features,目录结构如下图:
features
|_support
| |_app_installation_hooks.rb
| |_app_life_cycle_hooks.rb
| |_env.rb
| |_hooks.rb
|_step_definitions
| |_calabash_steps.rb
|_my_first.feature
step_definions目录中存放用户自定义的features, my_first.feature用来书写测试的步骤。
基本用法之截屏
在my_first.feature中写下如下代码
Feature: Startup feature
Scenario: I can start my app
Then I wait for 15 seconds
Then I take a screenshot
第一行表示功能测试的名称,第二行表示应用场景,第三行和第四行为该应用场景所做的事:先等15秒,然后在截屏。
在命令行输入 calabash-android run HackNews.apk,应用程序会被安装到手机或模拟器中,15秒之后,会自动截屏,图片保存在当前工程目录下。也可以自定义截图保存的路径:
SCREENSHOT_PATH=/tmp/foo/ calabash-android run
以这种方式启动测试,图片保存在目录/tmp/foo/下面。
基本用法之自定义feature
在step_definitions中新建文件touch_steps.rb,添加代码如下:
# -- Touch --#
Then /^I (?:press|touch) on screen (\d+) from the left and (\d+) from the top$/ do |x, y|
touch(nil, {:offset => {:x => x.to_i, :y => y.to_i}})
sleep(3)
end
自定义的feature会被测试框架解析。功能名称写在了/^ &/当中,参数列表位于 | | 中。Then和end中间的代码为执行的语句。然后在my_first.feature中添加代码:
Then I touch on screen 100 from the left and 150 from the top
上面代码表示点击图中的广告条
下一张将详细介绍calabash-android中内置的features