More Related Content
Similar to Androidテスティング実践2 システムテスト編 (20)
More from 株式会社 NTTテクノクロス (9)
Androidテスティング実践2 システムテスト編
- 10. 【Robotium】テストコードの書き方: setup
64
@RunWith(AndroidJUnit4.class)
public class MyRobotiumTest {
// ActivityTestRuleの宣言は省略
@Rule
public ActivityTestRule<...> activityTestRule = ...;
private Solo solo;
@Before
public void setUp() throws Exception {
solo = new Solo(InstrumentationRegistry.
getInstrumentation(),
activityTestRule.getActivity());
}
Robotiumの主要クラスSoloをインスタンス化する
Copyright © 2016, NTT Software Corporation.
- 15. 【Robotium】テスト用APIの紹介: 文字列取得・入力
表示文字列の取得・テキスト入力
69
メソッド(Soloクラス) 概要
searchText(String) 指定された正規表現にマッチする文字列を表示し
ているUI部品を探す。見付かればtrueを返す。
enterText(int, String) 指定されたindex(0始まり)に存在するEditText
に対して、指定された文字列を入力する。
getText(String) 指定された正規表現にマッチする文字列を表示し
ているTextViewを返す。
getEditText(int) 指定されたindex(0始まり)に存在するEditText
を返す。
画面のどこにでも良いから文字列が表示されているか確認する場合は
searchText()が便利
特定のViewに表示されている文字列を確認する場合は、
getText()・getEditText()でTextView・EditTextを取得してから
TextView#getText()・EditText#getText()を使う
Copyright © 2016, NTT Software Corporation.
- 16. 【Robotium】テスト用APIの紹介: そのほか
そのほか
70
メソッド(Soloクラス) 概要
assertCurrentActivity(St
ring, Class)
現在の画面が指定されたActivity(クラス)
であることをassertする。
takeScreenshot() 画面のスクリーンショットを撮る。
/sdcard/Robotium-Screenshots/に保存。
clickOnWebElement(By) 指定された検索条件(Byオブジェクト)に合致した
WebView内HTML部品をクリックする。
typeTextInWebElement(By,
String)
指定された検索条件(Byオブジェクト)に合致した
WebView内HTML部品(フォーム)に対して、
指定された文字列を入力する。
※スクリーンショットを撮る場合は、テスト対象アプリ側に、以下のパー
ミッション宣言が必要
<uses-permission android:name=
"android.permission.WRITE_EXTERNAL_STORAGE" />
Copyright © 2016, NTT Software Corporation.
- 24. 【Espresso】テスト用APIの紹介: 概要
78
概要
ViewMatcher Viewの検索条件を指定する。ViewMatchersクラス参照。
withId(), withClassName(), withText(), ...
※hamcrestのallOf(), not(), is(),なども使える
ViewAction Viewに対する操作を指定する。ViewActionsクラス参照。
clearText(), typeText(), click(), ...
ViewAssertion 確認条件を指定する。ViewAssertionsクラス参照。
doesNotExist(), matches(ViewMatcher), ...
onView(ViewMatcher)
.perform(ViewAction)
.check(ViewAssertion);
以下の基本形を理解する
※perform(), check()は省略可 「ViewMatcher」にあてはまる
Viewに対して
「ViewAction」を実行した結果
そのViewが「ViewAssertion」
を満たすことを確認する
Copyright © 2016, NTT Software Corporation.
- 26. 【Espresso】テスト用APIの紹介: ViewMatchers
検索条件を表すメソッドが定義されている
80
メソッド 概要
withId(int) 指定されたIDを持つView
withText(String) 指定されたテキストが表示されている
View
withContentDescription(String) 指定されたcontentDescription属性を
持ったView
withClassName(Matcher<String>) 指定されたクラス名のView
※引数には「is(クラス名)」を指定する
hamcrestのmatcherを使うこともある(CoreMatchers)
allOf(条件1, 条件2, ...)
anyOf(条件1, 条件2, ...)
is()
not()
Copyright © 2016, NTT Software Corporation.
- 38. 【UI Automator】テストコードの書き方
92
@RunWith(AndroidJUnit4.class)
public class MyUiautomatorTest {
@Rule
public ActivityTestRule<...> activityTestRule = ...;
private UiDevice uiDevice;
@Before
public void setUp() throws Exception {
uiDevice = UiDevice.getInstance(InstrumentationRegistry
.getInstrumentation());
}
...
}
ATSLの設定に加えて
UiDeviceオブジェクトの初期化が必要
Copyright © 2016, NTT Software Corporation.
- 39. 【UI Automator】テスト用APIの紹介: 検索(1/2)
条件にあったUI部品を検索する
93
UiObject uiObject = uiDevice.findObject(<検索条件1>)
// または
UiObject2 uiObject = uiDevice.findObject(<検索条件2>)
検索条件1はUiSelectorオブジェクトで表現
//検索条件1
new UiSelector().text("OK").className(Button.class)
検索条件2はBySelectorオブジェクトで表現
//検索条件2
By.text("OK").clazz(Button.class)
Copyright © 2016, NTT Software Corporation.
- 40. 【UI Automator】テスト用APIの紹介: 検索(2/2)
94
UiSelector検索条件 (UiObject向け)
メソッド 概要
className(Class) Viewのクラス(EditTextやButtonなど)を条件に指定する
text(String) Viewの表示文字列を条件に指定する
description(String) ViewのcontentDescription属性値を条件に指定する
resourceId(String) ViewのリソースID(文字列表現)を条件に指定する
BySelector検索条件 (UiObject2向け)
メソッド 概要
clazz(Class) Viewのクラス(EditTextやButtonなど)を条件に指定する
text(String) Viewの表示文字列を条件に指定する
desc(String) ViewのcontentDescription属性値を条件に指定する
res(String) ViewのリソースID(文字列表現)を条件に指定する
Copyright © 2016, NTT Software Corporation.
- 42. 【UI Automator】テスト用APIの紹介: UiObject系
96
「OK」ボタンをクリックする例
// UiObjectを使う場合
UiObject okButton =
uiDevice.findObject(new UiSelector().text("OK").
className(Button.class));
okButton .click();
// UiObject2を使う場合
UiObject2 okButton2 =
uiDevice.findObject(By.text("OK").clazz(Button.class));
okButton2.click();
Copyright © 2016, NTT Software Corporation.
- 43. 【UI Automator】テスト用APIの紹介: 同期系(1/4)
UiObjectを使うときはUiObjectのメソッドを呼ぶ
97
メソッド(引数はタイムアウト値) 概要
clickAndWaitForNewWindow(long) クリックしてから、新しい窓(ダイアログ
など)が表示されるまで待つ
waitForExists(long) このViewが表示されるまで待つ
// 「OK」ボタンを押して、ダイアログが表示されるまで待つ
UiObject okButton =
uiDevice.findObject(new UiSelector().text("OK").
className(Button.class));
// ダイアログが表示されるまで2000msec待つ。タイムアウト時はテスト失敗。
assertThat(okButton.clickAndWaitForNewWindow(2000L), is(true));
// ここから表示されたダイアログに対する操作を行う
サンプルコード
※タイムアウトした場合は、falseが返される
Copyright © 2016, NTT Software Corporation.
- 44. 【UI Automator】テスト用APIの紹介: 同期系(2/4)
UiObject2は以下の汎用的なメソッドを使う
98
メソッド(引数は条件とタイムアウト値) 概要
clickAndWait(EventCondition,
long)
クリックしてから、指定された条件が満
たされるまで待つ
wait(SearchCondition, long)
※UiObject2とUiDeviceの両方に存在
指定された条件が満たされるまで待つ
wait(UiObject2Condition, long) 指定された条件が満たされるまで待つ
良く使う「条件」はUntilクラスに用意されている
Untilクラスのstaticメソッド 概要
newWindow() 新しい窓(ダイアログ)が表示されるまで
textEquals(String) 表示文字列が引数で指定された通りになるまで
gone(BySelector) 条件に合致するViewが見付からなくなるまで
※タイムアウトした場合は、null/0/falseが返される
Copyright © 2016, NTT Software Corporation.
- 45. 【UI Automator】テスト用APIの紹介: 同期系(3/4)
99
サンプルコード
// 「OK」ボタンを押して、ダイアログが表示されるまで待つ
UiObject2 okButton2 =
uiDevice.findObject(By.text("OK").clazz(Button.class));
// ダイアログが表示されるまで2000msec待つ
boolean result = okButton2.clickAndWait(Until.newWindow(),
2000L);
// タイムアウトしたときはテストを失敗させる
assertThat(result, is(true));
// ここからダイアログの操作などを行う
Copyright © 2016, NTT Software Corporation.
- 49. 【UI Automator】Tips: UiScrollable
スクロールすれば現れる画面外の要素を操作できる
103
// 標準ホームアプリのアプリ一覧から、画面外にある
//「足し算アプリ」アイコンをタップして起動する。
UiScrollable appViews
= new UiScrollable(new UiSelector().
scrollable(true));
// スクロール方向を水平方向に設定
appViews.setAsHorizontalList();
// "足し算アプリ"アプリを起動する
UiObject myApp
= appViews.getChildByText(new UiSelector().
className(TextView.class),
"足し算アプリ");
myApp.clickAndWaitForNewWindow();
Copyright © 2016, NTT Software Corporation.
- 50. 【UI Automator】Tips: 日本語入力
UiObject/UiObject2クラスの
setText(String)メソッド
Android 5.0以上: 日本語入力OK
Android 4.4以下: 指定できるのはASCII文字のみ
Android 4.4以下で日本語を使う場合は専用のIMEを使う
UIAutomator Unicode Input Helper
https://github.com/sumio/uiautomator-unicode-input-helper
Copyright © 2016, NTT Software Corporation. 104
// 上記IMEをインストールした状態で実行する。
// 「&」が含まれていないASCII文字の入力は今まで通り
UiObject editText = uiDevice.findObject(...);
editText.setText("Thank you");
// それ以外の文字を入力する時はUtf7ImeHelper.e()で文字列をラップする
editText.setText(Utf7ImeHelper.e("ありがとう"));
- 53. 【Appium】概要
Copyright © 2016, NTT Software Corporation. 107
End2End UIテストフレームワーク
Android 2.3.3以上/iOS 6.0以上両対応
別アプリにまたがったテストができる
Selenium WebDriverと同様にテストが書ける
URL: http://appium.io/
Apache License 2.0
https://github.com/appium よりロゴを引用
- 54. 【Appium】アーキテクチャ
Copyright © 2016, NTT Software Corporation. 108
http://www.atmarkit.co.jp/ait/articles/1504/27/news025.html より引用
Appiumサーバ: クライアント-テスト対象端末間のプロ
キシとして動作
テストスクリプトのプログラム言語は色々選べる
- 57. 【Appium】Appium Desktop Appのインストール
Appium ServerのGUI版
ダウンロードページ
https://bitbucket.org/appium/appium.app/downloads/
インストーラを実行するだけでOK
Android開発環境と同一PCにインストールすること
動作に必要なソフトウェア
.NET Framework 4.5再配布可能パッケージ (Windowsのみ)
JDK7以上
Android SDK
Copyright © 2016, NTT Software Corporation. 111
- 59. 【Appium】テストコードの書き方: setup
AndroidDriverを初期化する
そのためにDesired Capabilityの指定が必要
113
public class AppiumTest {
private AndroidDriver<MobileElement> mDriver;
@Before
public void setUp() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
...
mDriver =
new AndroidDriver<>(
new URL("http://localhost:4723/wd/hub"),
capabilities);
}
Copyright © 2016, NTT Software Corporation.
- 62. 【Appium】テスト用APIの紹介: 概要
基本形
Copyright © 2016, NTT Software Corporation. 116
WebElement elem = mDriver.findElement(<検索条件>);
elem.<操作>
検索条件(Byクラス・MobileByクラス)
http://www.atmarkit.co.jp/ait/articles/1506/02/news017_2.html#031
操作(WebElementクラスのメソッド)
http://www.atmarkit.co.jp/ait/articles/1506/02/news017_2.html#034
状態取得(WebElementクラスのメソッド)
http://www.atmarkit.co.jp/ait/articles/1506/02/news017_2.html#035
- 63. 【Appium】テスト用APIの紹介: 記述例
解説記事のサンプルプログラム https://goo.gl/chfSjz 参照
Appium最新版に対応するために以下の修正が必要
依存ライブラリバージョンの最新化(p.112参照)
AndroidDriverをAndroidDriver<MobileElement>に
MobileCapabilityTypeをAndroidMobileCapabilityTypeに
ChromeDriver関係のエラーが発生する場合
以下から古い(現時点でv2.20)chromedriverをダウンロード、展開
https://goo.gl/0lVlTE
展開した実行ファイルのフルパスを、Appiumの
「Chromedriver Path」に入力
Copyright © 2016, NTT Software Corporation. 117