源码地址:https://github.com/calabash/calabash-android
1. Instrumentation源代码地址,可以以project的形式导入到Eclpise中查看,也为后续对step-definition
calabash-android/ruby-gem/test-server/instrumentation-backend/
在src中可以查看到相应的instrumentation方法,利用其中方法可以对calabash-android进行扩展.
2. ruby-gem中calabash-android/ruby-gem/lib/calabash-android/ 下可以看到对cucumber中的step的定义
(1)我们看到此文件下面的calabash_steps.rb中有如下定义
require 'calabash-android/steps/assert_steps' | |
require 'calabash-android/steps/check_box_steps' | |
require 'calabash-android/steps/context_menu_steps' | |
require 'calabash-android/steps/date_picker_steps' | |
require 'calabash-android/steps/enter_text_steps' | |
require 'calabash-android/steps/location_steps' | |
require 'calabash-android/steps/navigation_steps' | |
require 'calabash-android/steps/press_button_steps' | |
require 'calabash-android/steps/progress_steps' | |
require 'calabash-android/steps/screenshot_steps' | |
require 'calabash-android/steps/search_steps' | |
require 'calabash-android/steps/spinner_steps' | |
require 'calabash-android/steps/time_picker_steps' |
3. 关于calabash的扩展
我们新建一个测试文件夹,cd到此文件先,然后运行“calabash-android gen”会有如此目录结构
➜ calabash tree
.
features
|_support
| |_app_installation_hooks.rb
| |_app_life_cycle_hooks.rb
| |_env.rb
|_step_definitions
| |_calabash_steps.rb
|_my_first.feature
其中calabah_steps.rb中默认引用了 require 'calabash-android/calabash_steps'此代码,如果需要扩展可以在此中定义具体的steps并且调用instrumentation-backend中定义好了的方法。
feature 简单定义:
Feature: Login feature Scenario: As a valid user I can log into my app When I press "Add Contact" Then I see "Target Account" Then I enter "hello" into input field number 1 Then I enter "13817861875" into input field number 2 Then I enter "hengwen@hotmail.com" into input field number 3 When I press "Save" Then I wait for 1 second Then I toggle checkbox number 1 Then I see "hello"
这里 input field number 就针对了 ContactAdder Activity 中输入框。我现在这样写其实不太友好,比较好的方式是进行再次封装,对 DSL 撰写者透明。比如:
When I enter "hello" as "Contact Name"
step_definition
When (/^I enter "([^\"]*)" as "([^\"]*)"$/) do | text, target |
index = case target
when "Contact Name": 1
...
end
steps %{
Then I enter #{text} into input field number #{index}
}end
这样 feature 可读性会强一点。后面的语句依旧调用calabash-android/calabash_steps中的steps
Then I enter #{text} into input field number #{index}