Calabash-Androidでテストシナリオを作成する方法スマホ向け無料システムテスト自動化ツール(5)(1/5 ページ)

本連載では、AndroidおよびiOSアプリ開発における、システムテストを自動化するツールを紹介していきます。今回は、Calabash-Androidのテストで必要になる、各種UIコンポーネントの特定・操作・検証方法を紹介し、ステップ定義の仕方やUIコンポーネントを調査するための「console」について解説します。

» 2015年01月28日 18時00分 公開
[伊藤宏幸,テスト自動化研究会(STAR),楽天株式会社]

UIコンポーネントの特定・操作・検証

「スマホ向け無料システムテスト自動化ツール」のインデックス

連載目次

 本連載では、AndroidおよびiOSアプリ開発における、システムテストを自動化するツールを紹介していきます。前回の「iOS/AndroidにCucumberのBDDをもたらすテストフレームワークCalabashの基礎知識とインストール」から数回にわたり「Calabash」、特に「Calabash-Android」を中心に解説しています。今回はCalabash-Androidで事前に用意されている「ステップ定義」(canned steps/Predefined steps)(GitHub)を使用してテストシナリオを作成する方法を中心に解説します。

 まずは、Calabash-Androidのテストで必要になる、各種UIコンポーネントの特定・操作・検証方法を紹介し、ステップ定義の仕方やUIコンポーネントを調査するための「console」について解説します。

UIコンポーネントを特定する3つの方法

 Calabash-Androidでは、基本的に次の3つの方法でUIコンポーネントを指定します。

【1】どのコンポーネントを操作するのかが明確な「ステップ定義」を使うパターン

Then I go back

 「ステップ定義」の中には、どのUIコンポーネントを操作するのかが明確なものがあります。ちなみに、この例では、端末の「戻る」ボタンを操作します。

【2】UIコンポーネントにIDなどの情報を付与し、これを指定するパターン

Then I enter text "ほむら" into field with id "name"

 「{プロジェクトのルート}/app/src/main/res/layout」以下にあるxmlファイルに定義されているUIコンポーネントにIDを付与することで、当該コンポーネントを指定できます。またUIコンポーネントによっては、指定した「テキスト」または「description」で指定できるものもあります。

 なお、UIコンポーネントにおけるIDなどの情報はSeleniumのLocatorに相当するものです。気になった方は、「Selenium の locator とうまくつきあうための話 - qiita」も併せてご確認ください。

【3】その画面上の何番目のUIコンポーネントかを指定するパターン

Then I press button number 3

 初期のJavaScriptのように、UIコンポーネントの順番で指定することも可能です。

各UIコンポーネントの操作

 以下、代表的なUIコンポーネントの操作をいくつか紹介します。下記は全てのステップに共通する点です。

  • 「ステップ定義」には「Given」「When」「Then」が明記されているが、テストシナリオ内で実際に使用する際は、「Given」「When」「Then」のどれを使用しても問題ない
  • 基本的に、「{プロジェクトのルート}/app/src/main/res/layout」以下にあるxmlファイルに定義されているUIコンポーネントが対象となる(「WebView」は事前に用意されているステップ定義では適切に処理できない(後述))
  • IDは、レイアウトファイルの「android:id="@+id/name」の「@+id/」以下の部分が該当
  • 順番を指定するステップはいずれも、UIのレイアウトを変更するたびにテストが失敗する恐れが大きいため、できる限り使用を避けた方が無難

Button

 「UIコンポーネントのボタンを押す方法(GitHub)」には、下記のものがあります。

効果 テストシナリオ内での実際の記述方法 ステップ定義
指定したIDのUIコンポーネントを押す
(Button/ImageButtonは問わない)
Given I press "button_add" Then /^I press "([^\"]*)"$/
指定したテキストButtonを押す
ImageButtonは押せないので注意!)
Given I press the "検索" button Given /^I press the "([^\"]*)" button$/
指定した順番Buttonを押す
ImageButtonは押せないので注意!)
When I press button number 2 Then /^I press button number (\d+)$/
指定した順番ImageButtonを押す
Buttonは押せないので注意!)
Then I press image button number 3 Then /^I press image button number (\d+)$/

 特にButtonかImageButtonかを区別するテストニーズがない限り、テストシナリオの可読性を高める観点から、「Then /^I press “([^\"]*)"$/」のみを使用することをオススメします。

 また、「端末のボタンを押す方法(GitHub)」として、別途下記のものが定義されています。

効果 テストシナリオ内での実際の記述方法 ステップ定義
端末の「戻る」ボタンを押す Given I go back Then /^I go back$/
端末の「メニュー」ボタンを押す When I press the menu key Then /^I press the menu key$/
キーボードの「Enter」キーを押す Then I press the enter button Then /^I press the enter button$/

RadioButton/Checkbox

 「RadioButton/Checkboxの操作方法(GitHub)」としては、下記のものがあります。

効果 テストシナリオ内での実際の記述方法 ステップ定義
指定したIDのUIコンポーネントを押す When I press "genderFemale" Then /^I press "([^\"]*)"$/
指定した順番のチェックボックスをオン/オフする Then I toggle checkbox number 3 Then /^I toggle checkbox number (\d+)$/

Input

 「入力フィールドの操作方法(GitHub)」には、下記のものがあります。

効果 テストシナリオ内での実際の記述方法 ステップ定義
指定した順番の入力フィールドに、指定した文字を入力する Then I enter "ほむら" into input field number 2 Then /^I enter "([^\"]*)" into input field number (\d+)$/
指定したIDの入力フィールドに、指定した文字を入力する Given I enter text "ほむら" into field with id "name" Then /^I enter text "([^\"]*)" into field with id "([^\"]*)"$/
指定したDescriptionのある入力フィールドに、指定した文字を入力する When I enter "魔法少女" as "職業" Then /^I enter "([^\"]*)" as "([^\"]*)"$/
指定したDescriptionのある入力フィールドに、指定した文字を入力する Then I enter "魔法少女" into "職業" Then /^I enter "([^\"]*)" into "([^\"]*)"$/
指定した順番の入力フィールドをクリアする Given I clear input field number 5 Then /^I clear input field number (\d+)$/
指定したIDの入力フィールドをクリアする Then I clear "name" Then /^I clear "([^\"]*)"$/
指定したIDの入力フィールドをクリアする Then I clear input field with id "name" Then /^I clear input field with id "([^\"]*)"$/

NumberPicker

 なお「入力フィールドの操作方法(GitHub)」には、「DatePicker」「TimePicker」を操作するステップは用意されていますが、「NumberPicker」を操作するステップは現時点(バージョン0.5.2)では用意されていません。

 従って、NumberPickerの操作をしたい場合は、独自にステップを定義する必要があります。これについては後述します。

要素の検証

 Calabash-Androidでは、要素の検証機能として、下記の「テキストの検証機能(GitHub)」が用意されています。

効果 テストシナリオ内での実際の記述方法 ステップ定義
指定したテキストがあることを検証する Then I see the text "ほむら" Then /^I see the text "([^\"]*)"$/
指定したテキストがあることを検証する Then I see "ほむら" Then /^I see "([^\"]*)"$/
指定したテキストがあることを検証する Then I should see "ほむら" Then /^I should see "([^\"]*)"$/
指定したテキストがあることを検証する Then I should see text containing "ほむら" Then /^I should see text containing "([^\"]*)"$/
指定したテキストがないことを検証する Then I should not see "まどか" Then /^I should not see "([^\"]*)"$/
指定したテキストがないことを検証する Then I don't see the text "まどか" Then /^I don't see the text "([^\"]*)"$/
指定したテキストがないことを検証する Then I don't see "まどか" Then /^I don't see "([^\"]*)"$/

検証例

 それでは、実際に要素を検証してみましょう。要素を検証するステップをテストシナリオに定義し、Calabash-Androidを実行すると、以下のように、実際に実行されるシナリオ・ステップを確認できます。

 まずは成功例です。「Then I see the text "ほむら"」を検証してみます。

Feature: Add customer
  
  In order to know the basics of Calabash-Android
  As a trial user of HelloTesting
  I want to add customer data and watch it
 
  Scenario: Add customer information and preview it				# features/AddCustomer.feature:8
    Given I press "button_add"							# calabash-android-0.5.2/lib/calabash-android/steps/press_button_steps.rb:17
    When I enter text "ほむら" into field with id "name"			# calabash-android-0.5.2/lib/calabash-android/steps/enter_text_steps.rb:13
    And  I enter text "homumado@example.com" into field with id "email"	# calabash-android-0.5.2/lib/calabash-android/steps/enter_text_steps.rb:13
    And  I press "genderFemale"							# calabash-android-0.5.2/lib/calabash-android/steps/press_button_steps.rb:17
    Then I should see "ほむら"							# calabash-android-0.5.2/lib/calabash-android/steps/assert_steps.rb:9
 
1 scenario (1 passed)
5 steps (5 passed)
0m12.395s

 次に失敗例です。「Then I see the text "ほむまど"」を検証してみます。

Feature: Add customer
  
  In order to know the basics of Calabash-Android
  As a trial user of HelloTesting
  I want to add customer data and watch it
 
  Scenario: Add customer information and preview it				# features/AddCustomer.feature:8
    Given I press "button_add"							# calabash-android-0.5.2/lib/calabash-android/steps/press_button_steps.rb:17
    When I enter text "ほむら" into field with id "name"			# calabash-android-0.5.2/lib/calabash-android/steps/enter_text_steps.rb:13
    And  I enter text "homumado@example.com" into field with id "email"		# calabash-android-0.5.2/lib/calabash-android/steps/enter_text_steps.rb:13
    And  I press "genderFemale"							# calabash-android-0.5.2/lib/calabash-android/steps/press_button_steps.rb:17
    Then I should see "ほむまど"						# calabash-android-0.5.2/lib/calabash-android/steps/assert_steps.rb:9
 
calabash-android-0.5.2/lib/calabash-android/steps/assert_steps.rb:9
      Timeout waiting for elements: * {text CONTAINS[c] 'ほむまど'} (Calabash::Android::WaitHelpers::WaitError)
      features\AddCustomer.feature:16:in `Then I should see "ほむまど"'
 
Failing Scenarios:
cucumber features\AddCustomer.feature:8 # Scenario: Add customer information and preview it
 
1 scenario (1 failed)
5 steps (1 failed, 4 passed)
0m20.190s

 なお、要素の検証に失敗した場合、そのテストシナリオは失敗となり、後続のステップは実行されません(これを「skip」と呼称します)。

WebViewの検証

 今回のサンプルであれば、本来入力後のPreview画面を検証するのが妥当です。しかしPreview画面は、「WebView」コンポーネントを使用し、HTMLで定義されています。前述の通り、WebViewは事前に用意されているステップ定義では適切に処理できません。

 従って、WebViewの要素を検証する場合は、独自にステップを定義する必要があります。これについては後述します。

       1|2|3|4|5 次のページへ

Copyright © ITmedia, Inc. All Rights Reserved.

RSSについて

アイティメディアIDについて

メールマガジン登録

@ITのメールマガジンは、 もちろん、すべて無料です。ぜひメールマガジンをご購読ください。