上記「ステップ定義」は、Calabash-Androidが提供するスケルトンから作成することも可能です。便利なので、併せて紹介します。
基本的な流れは、以下の通りです。
特に上記『「ステップ定義」の基本的な作り方』との変更点はありません。
$ calabash-android run app/build/outputs/apk/app-calabash-release.apk
(中略)
Feature: Preview customer
(中略)
Scenario: Add customer information and preview it by using WebView # features\PreviewCustomer.feature:9
(中略)
And go preview # features\PreviewCustomer.feature:16
1 scenario (1 undefined)
5 steps (1 undefined, 4 passed)
0m13.425s
You can implement step definitions for undefined steps with these snippets:
When(/^go preview$/) do
pending # express the regexp above with the code you wish you had
end
「calabash-android run {YOUR_APK_FILE}」コマンドを実行すると、「And go preview」の部分が「ステップ定義」として未定義であること(「1 undefined」がこれを意味します)が分かります。
また、当該の「ステップ定義」のスケルトンも併せて作成、表示してくれます。
「{プロジェクトのルート}/features/step_definitions/PreviewCustomer_steps.rb」を作成し、上記スケルトンをコピー&ペーストします。
When(/^go preview$/) do pending # express the regexp above with the code you wish you had end
ちなみに「pending」は、実装未済であることを表します。
$ calabash-android run app/build/outputs/apk/app-calabash-release.apk
(中略)
Feature: Preview customer
(中略)
Scenario: Add customer information and preview it by using WebView # features\PreviewCustomer.feature:9
(中略)
And go preview # features/step_definitions/PreviewCustomer_steps.rb:1
TODO (Cucumber::Pending)
./features/step_definitions/PreviewCustomer_steps.rb:4:in `/^go preview$/'
features\PreviewCustomer.feature:16:in `And go preview'
1 scenario (1 pending)
5 steps (1 pending, 4 passed)
0m11.357s
上記【3】で追加した箇所の実行結果が「undefined」から「pending」に変わり、実装未済という扱いに変わります。従って、上記【3】で追加した「ステップ定義」とスケルトンとが、キチンとCalabash-Androidに認識されたことを確認できます。
「ステップ定義」ファイルで、スケルトンを実装します。上記『【2】「calabash-android run {YOUR_APK_FILE}」コマンドを実行し、スケルトンを作成する』と同じ要領で、「{プロジェクトのルート}/features/step_definitions/PreviewCustomer_steps.rb」に下記のようにスケルトンを実装します。
When(/^go preview$/) do
touch("ActionMenuItemView id:'button_preview'")
end
$ calabash-android run app/build/outputs/apk/app-calabash-release.apk
(中略)
Feature: Preview customer
(中略)
Scenario: Add customer information and preview it by using WebView # features\PreviewCustomer.feature:9
(中略)
And go preview # features/step_definitions/PreviewCustomer_steps.rb:1
1 scenario (1 passed)
5 steps (5 passed)
0m11.134s
前述しましたが、Calabash-Androidの「入力フィールドの操作方法(GitHub)」には、DatePickerおよびTimePickerを操作するステップは用意されているのですが、NumberPickerを操作するステップは現時点(バージョン0.5.2)では用意されていません。
従って、NumberPickerを操作したい場合は、独自にステップを定義する必要があります。
今回のサンプルプログラムの場合、Customer Detail画面のNumberPickerは、IDが「agePicker」です。このことは、後述の「console」の結果からも確認できます。
irb(main):011:0> query("NumberPicker id:'agePicker'")
[
[0] {
"id" => "agePicker",
"enabled" => true,
"contentDescription" => "age pickerview",
"class" => "android.widget.NumberPicker",
"rect" => {
"center_y" => 321,
"center_x" => 146,
"height" => 180,
"y" => 231,
"width" => 128,
"x" => 82
},
"tag" => nil,
"description" => "android.widget.NumberPicker{b20df0b0 V.ED.... ......I. 66,0-194,180 #7f09000d app:id/agePicker}"
}
]
「query」メソッドは下記のように、第2引数に「:{メソッド名}{操作}」の書式を指定することで、当該UIコンポーネントのメソッドを呼び出すことが可能です。
query("NumberPicker id:'agePicker'", :setValue=>14)
この例では、NumberPickerの「setValue」メソッドを、引数「14」を指定して呼び出しています。
しかし今回のサンプルでは、Customer Detail画面のNumberPickerでsetValueメソッドを呼び出しただけでは、CustomerPreviewActivity画面で年齢欄を更新できません。
これは、「{プロジェクトのルート}/app/src/main/java/com/nowsprinting/hellotesting/app/CustomerDetailFragment.java」の「onCreateView」メソッド内で、「onValueChange」イベントが呼ばれたときに、NumberPickerに設定したvalueが初めて年齢に反映される作りとなっているためです。
そのため今回は、setValueメソッド呼び出し後にNumberPickerを「pan」することでonValueChangeイベントを(無理やり)発動させて、setValueメソッドで設定した値を反映することとします。
pan("NumberPicker id:'agePicker'", :up)
pan("NumberPicker id:'agePicker'", :down)
# 設定中に画面が遷移しないよう、念のため1秒待機します。
sleep 1
テスト自動化の歴史と今後、良い/悪い事例〜システムテスト自動化カンファレンス2013レポート
ビジネス目標を見据えたテスト設計が肝!「DevOps時代のテスト自動化カンファレンス 冬の陣」開催
Kiwi+CocoaPodsで始めるiOSアプリの振る舞いテスト入門
Android SDKでビジネスロジックのテストを自動化するにはCopyright © ITmedia, Inc. All Rights Reserved.