新聞中心

Calabash-Android用法入門

作者: 時(shí)間:2016-09-12 來(lái)源:網(wǎng)絡(luò) 收藏

本文所用的源代位于:https://github.com/bigconvience/HackerNews

本文引用地址:http://butianyuan.cn/article/201609/304698.htm

下載完代碼后,進(jìn)入工程目錄中,在命令行中輸入:calabash-android gen,此命令會(huì)在工程目錄下生成目錄features,目錄結(jié)構(gòu)如下圖:

?

1
2
3
4
5
6
7
8
9
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用來(lái)書寫測(cè)試的步驟。

基本用法之截屏

在my_first.feature中寫下如下代碼

?

1
2
3
4
Feature: Startup feature
Scenario: I can start my app
Then I wait for 15 seconds
Then I take a screenshot

第一行表示功能測(cè)試的名稱,第二行表示應(yīng)用場(chǎng)景,第三行和第四行為該應(yīng)用場(chǎng)景所做的事:先等15秒,然后在截屏。

在命令行輸入 calabash-android run HackNews.apk,應(yīng)用程序會(huì)被安裝到手機(jī)或模擬器中,15秒之后,會(huì)自動(dòng)截屏,圖片保存在當(dāng)前工程目錄下。也可以自定義截圖保存的路徑:

?

1
code>SCREENSHOT_PATH=/tmp/foo/ calabash-android run/code>

以這種方式啟動(dòng)測(cè)試,圖片保存在目錄/tmp/foo/下面。

基本用法之自定義feature

在step_definitions中新建文件touch_steps.rb,添加代碼如下:

?

1
2
3
4
5
# -- 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會(huì)被測(cè)試框架解析。功能名稱寫在了/^ /當(dāng)中,參數(shù)列表位于 | | 中。Then和end中間的代碼為執(zhí)行的語(yǔ)句。然后在my_first.feature中添加代碼:

?

1
Then I touch on screen 100 from the left and 150 from the top

上面代碼表示點(diǎn)擊圖中的廣告條



關(guān)鍵詞:

評(píng)論


相關(guān)推薦

技術(shù)專區(qū)

關(guān)閉