qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請(qǐng)?jiān)L問 http://qaseven.github.io/

          移動(dòng)應(yīng)用測(cè)試框架—Calabash Android 簡介

          什么是 Calabash
            Calabash 是一個(gè)自動(dòng)化測(cè)試框架,它可以測(cè)試 Android 和 iOS 原生應(yīng)用和混合應(yīng)用。
            它有:
            calabash-android
            calabash-ios
            主頁: http://calabash.sh
            Calabash-android介紹
            Calabash-android 是支持 android 的 UI 自動(dòng)化測(cè)試框架,PC 端使用了 cucumber 框架,通過 http 和 json 與模擬器和真機(jī)上安裝的測(cè)試 apk 通信,測(cè)試 apk 調(diào)用 Robotium 的方法來進(jìn)行 UI 自動(dòng)化測(cè)試,支持 webview 操作。
            Calabash-android 架構(gòu)圖
            Features —— 這里的 feature 就是 cucumber 的 feature,用來描述 user stories 。
            Step Definitions —— Calabash Android 事先已經(jīng)定義了一些通用的 step。你可以根據(jù)自己的需求,定義更加復(fù)雜的步驟。
            Your app —— 測(cè)試之前,你不必對(duì)你的應(yīng)用修改。(這里其實(shí)是有問題,后面我們會(huì)說到。)
            Instrumentation Test Server —— 這是一個(gè)應(yīng)用,在運(yùn)行測(cè)試的時(shí)候會(huì)被安裝到設(shè)備中去。 這個(gè)應(yīng)用是基于 Android SDK 里的 ActivityInstrumentationTestCase2。它是 Calabash Android 框架的一部分。Robotium 就集成在這個(gè)應(yīng)用里。
            Calabash-android 環(huán)境搭建
            ruby 環(huán)境
            rvm
            rbenv
            RubyInstaller.org for windows
            Android 開發(fā)環(huán)境
            JAVA
            Android SDK
            Ant
            指定 JAVA 環(huán)境變量, Android SDK 環(huán)境變量(ANDROID_HOME), Ant 加入到 PATH 中去。
            安裝 Calabash-android
            gem install calabash-android
            sudo gem install calabash-android # 如果權(quán)限不夠用這個(gè)。
            如有疑問,請(qǐng)參考: https://github.com/calabash/calabash-android/blob/master/documentation/installation.md
            創(chuàng)建 calabash-android 的骨架
            calabash-android gen
            會(huì)生成如下的目錄結(jié)構(gòu):
            ?  calabash  tree
            .
            features
            |_support
            | |_app_installation_hooks.rb
            | |_app_life_cycle_hooks.rb
            | |_env.rb
            |_step_definitions
            | |_calabash_steps.rb
            |_my_first.feature
          寫測(cè)試用例
            像一般的 cucumber 測(cè)試一樣,我們只要在 feature 文件里添加測(cè)試用例即可。比如我們測(cè)試 ContactManager.apk (android sdk sample 里面的, Appium 也用這個(gè) apk)。
            我們想實(shí)現(xiàn),
            打開這個(gè)應(yīng)用
            點(diǎn)擊 Add Contact 按鈕
            添加 Contact Name 為 hello
            添加 Contact Phone 為 13817861875
            添加 Contact Email 為 hengwen@hotmail.com
            保存
           所以我們的 feature 應(yīng)該是這樣的:
            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 就針對(duì)了 ContactAdder Activity 中輸入框。我現(xiàn)在這樣寫其實(shí)不太友好,比較好的方式是進(jìn)行再次封裝,對(duì) 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 可讀性會(huì)強(qiáng)一點(diǎn)。
            運(yùn)行 feature
            在運(yùn)行之前,我們對(duì) apk 還是得處理下,否則會(huì)遇到一些問題。
            App did not start (RuntimeError)
            因?yàn)閏alabash-android的client和test server需要通信,所以要在 AndroidManifest.xml 中添加權(quán)限:
            <uses-permission android:name="android.permission.INTERNET" />
            ContacterManager 代碼本身的問題
            由于 ContacerManager 運(yùn)行時(shí)候,需要你一定要有一個(gè)賬戶,如果沒有賬戶 Save 的時(shí)候會(huì)出錯(cuò)。為了便于運(yùn)行,我們要修改下。
            源代碼地址在 $ANDROID_HOME/samples/android-19/legacy/ContactManager,大家自己去找。
            需要修改 com.example.android.contactmanager.ContactAdder 類里面的 createContactEntry 方法,我們需要對(duì) mSelectedAccount 進(jìn)行判斷, 修改地方如下:
          // 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");
          }
            代碼修改好之后,導(dǎo)出 apk 文件。
            運(yùn)行很簡單:
            calabash-android run <apk>
            如果遇到簽名問題,請(qǐng)用: calabash-android resign apk。
            可以看看我運(yùn)行的情況:
          ?  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!
            大家看到 gif 是 failed,是因?yàn)樵谀M器上運(yùn)行的。而上面全部通過的是我在海信手機(jī)上運(yùn)行的。環(huán)境不一樣,略有差異。
            總結(jié)
            本文是對(duì) calabash-android 的一個(gè)簡單介紹,做的是拋磚引玉的活。移動(dòng)測(cè)試框架并非 Appium 一家,TesterHome 希望其他框架的話題也能熱火起來。watch and learn!

          posted on 2014-12-23 00:15 順其自然EVO 閱讀(837) 評(píng)論(0)  編輯  收藏 所屬分類: 測(cè)試學(xué)習(xí)專欄

          <2014年12月>
          30123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 商河县| 习水县| 若尔盖县| 红安县| 萨嘎县| 安龙县| 江安县| 大洼县| 扶风县| 栾川县| 定陶县| 利川市| 新密市| 衡东县| 敦煌市| 维西| 西丰县| 深泽县| 岢岚县| 淮阳县| 和政县| 靖西县| 雷波县| 广宁县| 新化县| 清流县| 锡林郭勒盟| 天等县| 盱眙县| 闻喜县| 冕宁县| 榆树市| 平武县| 塔河县| 麻阳| 泰安市| 壤塘县| 卓尼县| 前郭尔| 芮城县| 凌源市|