標籤:android blog http os io 使用 java strong ar
Calabash-android是支援android的UI自動化測試架構,前面已經介紹過《中文Win7下成功安裝calabash-android步驟》,這篇博文嘗試測試一個真實應用:開源中國用戶端。目的是和大家一起學習calabash測試載入器。
測試環境與源碼準備 先介紹一下oschina.net
oschina除了有網站,還有三大平台手機用戶端:
http://www.oschina.net/app
用戶端已經開源!
那麼開源可以用來做什麼呢?
我正在學用calabash-android,得找到一個合適的待測app,平時手機上開源中國這個app用的蠻順手了,所以就選它了,再次特別向開源中國的開發工程師致謝!
環境準備:ADT+calabash-android
可以參考:
中文Win7下成功安裝calabash-android步驟
下載oschina/android-app原始碼
首先到 http://git.oschina.net/oschina/android-app ,
下載原始碼方式的方式都可:
- 可以直接點擊”下載zip“
- 複製git倉庫url: https://git.oschina.net/oschina/android-app.git,然後在Eclipse/ADT中開啟Windows > Open Perspective > Other… > Get Resporsitory Exploring,clone原始碼
匯入到ADT中後,原始碼如下:
在adt-bundle-20140702中編譯運行oschina/android-app的幾個問題
問題1: adt-bundle-20140702的API版本是20,所以要修改project.properties:
target=android-15 改成 target=android-20
問題2: 原始碼是使用了已作廢的class: android.webkit.CacheManager
oschina-android-app/src/net/oschina/app/AppContext.java中使用了android.webkit.CacheManager
所以要把相關代碼禁掉:
61行:
//import android.webkit.CacheManager;
1503到1509行:
// File file = CacheManager.getCacheFileBaseDir(); // if (file != null && file.exists() && file.isDirectory()) { // for (File item : file.listFiles()) { // item.delete(); // } // file.delete(); // }
問題3:Run As Android Application報錯:Installation error: INSTALL_FAILED_VERSION_DOWNGRADE
原因是:手機已經裝了一個開源中國的1.7.7.0版本,而ADT要下載的是1.7.6.9版本,Android系統不允許安裝一個比已安裝版本更舊的版本,所以從手機上卸載已有的1.7.7.0版本就可以了。
ADT編譯並上傳oschina/android-app到手機
Run As Android Application > 選擇串連到電腦Usb的手機 > OK
calabash測試步驟 先確認oschina/android-app聲明了訪問網路的許可權
oschina/android-app項目的AndroidManifest.xml中應該如下行:
<uses-permission android:name="android.permission.INTERNET" />
在oschina/android-app根目錄中建立calabash目錄
在命令列下進入oschina/android-app的原始碼根目錄:
D:\git\oschina>cd android-appD:\git\oschina\android-app>dir Volume Serial Number is 9823-AB19 Directory of D:\git\oschina\android-app2014/09/01 20:26 <DIR> .2014/09/01 20:26 <DIR> ..2014/09/01 20:21 783 .classpath2014/09/01 20:21 <DIR> .git2014/09/01 20:21 64 .gitignore2014/09/01 20:21 822 .project2014/09/01 20:21 <DIR> .settings2014/09/01 20:21 10,829 AndroidManifest.xml2014/09/01 20:21 <DIR> assets2014/09/01 20:42 <DIR> bin2014/09/01 20:26 <DIR> gen2014/09/01 20:21 <DIR> libs2014/09/01 20:21 18,092 LICENSE.txt2014/09/01 20:21 1,424 proguard.cfg2014/09/01 20:41 563 project.properties2014/09/01 20:21 4,183 README.md2014/09/01 20:21 <DIR> res2014/09/01 20:21 <DIR> src 8 File(s) 36,760 bytes 10 Dir(s) 133,131,993,088 bytes freeD:\git\oschina\android-app>
建立calabash目錄:
D:\git\oschina\android-app>mkdir calabash
建立cucumber skeleton:
D:\git\oschina\android-app>cd calabashD:\git\oschina\android-app\calabash>calabash-android gen----------Question----------I‘m about to create a subdirectory called features.features will contain all your calabash tests.Please hit return to confirm that‘s what you want.-------------------------------------Info----------features subdirectory created.---------------------------D:\git\oschina\android-app\calabash>dir Volume Serial Number is 9823-AB19 Directory of D:\git\oschina\android-app\calabash2014/09/01 21:54 <DIR> .2014/09/01 21:54 <DIR> ..2014/09/01 21:54 <DIR> features 0 File(s) 0 bytes 3 Dir(s) 133,131,988,992 bytes freeD:\git\oschina\android-app\calabash>
編輯 D:\git\oschina\android-app\calabash\features\my_first.feature:
初始內容:
Feature: Login feature Scenario: As a valid user I can log into my app When I press "Login" Then I see "Welcome to coolest app ever"
第一行Feature: XX,第二行 Scenario: YY是給人讀的,所以隨便填寫什麼中文英文內容都可以
關鍵內容是第三行When I press ..和第四行Then I see .., 這是給Cucumber軟體識別的。
When後面跟動作語句,Then後面跟內容檢查語句
先嘗試如下改動:
Feature: 啟動開源中國 Scenario: 啟動應用後,能看到軟體版本更新資訊 Then I see "軟體版本更新"
運行calabash-andriod run
D:\git\oschina\android-app\calabash>calabash-android run D:\git\oschina\android-app\bin\oschina-android-app.apk
測試失敗,原因是Then的預設等待時間只有2秒,開源中國app的啟動時間比較長。
ansicon中未能顯示中文:“軟體版本更新”,這個後面再解決。
按照 https://github.com/calabash/calabash-android/blob/master/ruby-gem/lib/calabash-android/canned_steps.md的指導,可以指定等待幾秒:
Feature: 啟動開源中國 Scenario: 啟動應用後,能看到軟體版本更新資訊 Then I wait for 10 seconds Then I see "軟體版本更新"
手機上看到的開源中國啟動後畫面:
按鈕“立即更新”為什麼是紅色的?以後再解決。
到此步為止,calabash測試開源中國Android用戶端的環境已經建立完畢,接下去同學們就可以歡快的嘗試canned_steps.md裡的各個預定義步驟了。
我也會繼續完成這個測試案例,大家一起共同進步!
使用calabash測試開源中國Android用戶端