概要
Espressoの操作録画機能(Record Espresso Test)は便利ですが、残念ながら一部の操作は記録されないようです。
今回は、WebViewのスクロールが記録されなくて困ったので、座標指定でスクロールするコードで代用できないか調べました。
結果としては可能で、UI AutomatorのUiDeviceを使います。
実装方法(例)
build.gradle
dependencies {
・・・
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3'
・・・
}
テストコード
// Initialize UiDevice instance (UI Automator)
UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
// 座標{80, 1000}から{80, 100}に1秒(第5引数かける5ミリ秒)かけてスワイプ
device.swipe(80, 1000, 80, 100, 200);
補足
以下調査中。もしわかる方いましたらコメント下さい。
- WebViewの最後までスクロールする
- WebViewの中で特定の文字列が見つかるまでスクロールする