什麼是 Calabash?
Calabash 是一個自動化測試框架,它能夠測試?android?和 iOS 原生應用和混合應用。
它有:
calabash-android
calabash-ios
主頁:? http://calabash.sh
Calabash-android介紹
Calabash-android 是支持 android 的 UI 自動化測試框架,PC 端使用了 cucumber 框架,經過 http 和 json 與模擬器和真機上安裝的測試 apk 通訊,測試 apk 調用 Robotium 的方法來進行 UI 自動化測試,支持 webview 操做。
Calabash-android 架構圖html
Features —— 這裏的 feature 就是 cucumber 的 feature,用來描述 user stories 。
Step Definitions —— Calabash Android 事先已經定義了一些通用的 step。你能夠根據本身的需求,定義更加複雜的步驟。
Your app —— 測試以前,你沒必要對你的應用修改。(這裏實際上是有問題,後面咱們會說到。)
Instrumentation Test Server —— 這是一個應用,在運行測試的時候會被安裝到設備中去。 這個應用是基於 Android SDK 裏的 ActivityInstrumentationTestCase2。它是 Calabash Android 框架的一部分。Robotium 就集成在這個應用裏。
Calabash-android 環境搭建
ruby 環境
rvm
rbenv
RubyInstaller.org for windows
Android 開發環境
JAVA
Android SDK
Ant
指定 JAVA 環境變量, Android SDK 環境變量(ANDROID_HOME), Ant 加入到 PATH 中去。
安裝 Calabash-android
gem install calabash-android
sudo gem install calabash-android # 若是權限不夠用這個。
若有疑問,請參考:? https://github.com/calabash/calabash-android/blob/master/documentation/installation.md
建立 calabash-android 的骨架
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.featureandroid
寫測試用例
像通常的 cucumber 測試同樣,咱們只要在 feature 文件裏添加測試用例便可。好比咱們測試 ContactManager.apk (android sdk sample 裏面的, Appium 也用這個 apk)。ios
咱們想實現,
打開這個應用
點擊 Add Contact 按鈕
添加 Contact Name 爲 hello
添加 Contact Phone 爲 13817861875
添加 Contact Email 爲? hengwen@hotmail.com
保存git
因此咱們的 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 可讀性會強一點。
運行 feature
在運行以前,咱們對 apk 仍是得處理下,不然會遇到一些問題。
App did not start (RuntimeError)
由於calabash-android的client和test server須要通訊,因此要在 AndroidManifest.xml 中添加權限:
<uses-permission android:name="android.permission.INTERNET" />
ContacterManager 代碼自己的問題
因爲 ContacerManager 運行時候,須要你必定要有一個帳戶,若是沒有帳戶 Save 的時候會出錯。爲了便於運行,咱們要修改下。
源代碼地址在 $ANDROID_HOME/samples/android-19/legacy/ContactManager,你們本身去找。
須要修改 com.example.android.contactmanager.ContactAdder 類裏面的 createContactEntry 方法,咱們須要對 mSelectedAccount 進行判斷, 修改地方以下:
// Prepare contact creation request
//
// Note: We use RawContacts because this data must be associated with a particular account.
//?????? The system will aggregate this with any other data for this contact and create a
//?????? coresponding entry in the ContactsContract.Contacts provider for us.
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
if(mSelectedAccount != null ) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, mSelectedAccount.getType())
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, mSelectedAccount.getName())
.build());
} else {
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.build());
}....
if (mSelectedAccount != null) {
// Ask the Contact provider to create a new contact
Log.i(TAG,"Selected account: " + mSelectedAccount.getName() + " (" +
mSelectedAccount.getType() + ")");
} else {
Log.i(TAG,"No selected account");
}
代碼修改好以後,導出 apk 文件。
運行很簡單:
calabash-android run <apk>
若是遇到簽名問題,請用: calabash-android resign apk。
能夠看看我運行的狀況:
?? calabash? calabash-android run ContactManager.apk
Feature: Login feature
Scenario: As a valid user I can log into my app??????????????? # features/my_first.feature:33135 KB/s (556639 bytes in 0.173s)3315 KB/s (26234 bytes in 0.007s)
When I press "Add Contact" # calabash-android-0.4.21/lib/calabash-android/steps/press_button_steps.rb:17
Then I see "Target Account" # calabash-android-0.4.21/lib/calabash-android/steps/assert_steps.rb:5
Then I enter "hello" into input field number 1 # calabash-android-0.4.21/lib/calabash-android/steps/enter_text_steps.rb:5
Then I enter "13817861875" into input field number 2 # calabash-android-0.4.21/lib/calabash-android/steps/enter_text_steps.rb:5
Then I enter "hengwen@hotmail.com" into input field number 3 # calabash-android-0.4.21/lib/calabash-android/steps/enter_text_steps.rb:5
When I press "Save" # calabash-android-0.4.21/lib/calabash-android/steps/press_button_steps.rb:17
Then I wait for 1 second # calabash-android-0.4.21/lib/calabash-android/steps/progress_steps.rb:18
Then I toggle checkbox number 1 # calabash-android-0.4.21/lib/calabash-android/steps/check_box_steps.rb:1
Then I see "hello" # calabash-android-0.4.21/lib/calabash-android/steps/assert_steps.rb:51 scenario (1 passed)9 steps (9 passed)0m28.304s
All pass!github
你們看到 gif 是 failed,是由於在模擬器上運行的。而上面所有經過的是我在海信手機上運行的。環境不同,略有差別。
總結
本文是對 calabash-android 的一個簡單介紹,作的是拋磚引玉的活。移動測試框架並不是 Appium 一家,TesterHome 但願其餘框架的話題也能熱火起來。watch and learn!web
本文選自:http://www.spasvo.com/news/html/20141218171136.htmljson