Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
CIでAndroidUIテストの様子を録画してみた
Search
mkeeda
May 17, 2022
Programming
1
530
CIでAndroidUIテストの様子を録画してみた
Mobile勉強会 Wantedly × チームラボ #5
https://wantedly.connpass.com/event/244011/
mkeeda
May 17, 2022
Tweet
Share
More Decks by mkeeda
See All by mkeeda
手動DIの教訓
mkeeda
0
88
WebViewと向き合う
mkeeda
1
690
お気に入りのAndroid Studio小技集
mkeeda
0
230
Scalable UI testing solutions かんたんまとめ
mkeeda
0
570
5分で分かるビルドロジック共通化の今
mkeeda
1
550
Compose で手に入れた UI の Unit test
mkeeda
2
1.1k
Jetpack Compose 完全に理解した
mkeeda
1
2.6k
5分でわかるCompose Modifiers deep dive
mkeeda
1
740
みんなで準備するスポンサー
mkeeda
0
180
Other Decks in Programming
See All in Programming
Ruby Function Composition
bkuhlmann
1
330
try!Swift Tokyo 2024 参加報告 LT
akidon0000
1
220
今、知っておきたい! 生成AIエージェントの世界
elith
3
350
[技育CAMPアカデミア]アイディアを形に!【超入門】スマホアプリ開発〜リリースまでの流れをご紹介
teamlab
PRO
0
360
はてなにおける CSS Modules、及び CSS Modules に足りないもの / CSS Modules in Hatena, and CSS Modules missing parts
mizdra
6
910
CA.swift19 恋するAIアプリ開発の裏側
oskmr
0
360
ゆるい個人開発のススメ
kuroppe1819
10
980
AWS Application Composerで始める、 サーバーレスなデータ基盤構築 / 20240406-jawsug-hokuriku-shinkansen
kasacchiful
1
260
Netty Chicago Java User Group 2024-04-17
sullis
0
170
SwiftUIで使いやすいToastの作り方 / How to build a Toast system which is easy to use in SwiftUI
lovee
3
130
1BRC--Nerd Sniping the Java Community
gunnarmorling
0
340
try! Swift Tokyo 初参加報告LT
hinakko2
0
220
Featured
See All Featured
Building a Modern Day E-commerce SEO Strategy
aleyda
17
6.4k
Building Applications with DynamoDB
mza
88
5.6k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
60
14k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
6
1.5k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
40
4.4k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
18
6.9k
10 Git Anti Patterns You Should be Aware of
lemiorhan
648
58k
A Philosophy of Restraint
colly
197
16k
The Straight Up "How To Draw Better" Workshop
denniskardys
227
130k
VelocityConf: Rendering Performance Case Studies
addyosmani
320
23k
Designing for Performance
lara
601
67k
A better future with KSS
kneath
231
16k
Transcript
CIでAndroidUIテストの様⼦を 録画してみた .PCJMFษڧձ8BOUFEMZºνʔϜϥϘ NLFFEB
About me • mkeeda (向井⽥ ⼀平) • Twitter: @mr_mkeeda •
Github: @mkeeda • Android Engineer at Cybozu, Inc 2
UIテストあるある • 不安定🤮 • CIでだけUIテストが落ちる🤮 • エラーがよくわからない🤮 • エラーが毎回違う🤮 3
UIテストの様⼦を録画してみた • CIのテスト実⾏時は Androidエミュレータの画⾯が⾒れない • UIテストが落ちるときの画⾯の状態を知りたい • Firebase test
labは使ってない • テスト失敗時にGithub Actionsの アーティファクトに録画データを残す 4
Androidデバイス画⾯収録の基本 • 録画開始 • adb shell screenrecord <ファイルパス> • ※ファイルパスはAndroidデバイスのパス
• 録画停⽌ • Ctrl + C (mac は Command + C) • 録画ファイル取得 • adb pull <録画ファイルのパス> <ローカルデバイスの保存先パス> 5
テストケースごとに録画してみる • ScreenRecordRule を作る • “record_<テストメソッド名>.mp4” というファイル名で保存する 6 @RunWith(AndroidJUnit4::class) class
SampleUiTest { @get:Rule var activityRule: ActivityScenarioRule<LoginActivity> = ActivityScenarioRule(LoginActivity::class.java) @get:Rule var screenRecordRule = ScreenRecordRule() @Test fun test() { // run test } }
テストコードから adb コマンドを実⾏ • UiAutomation.executeShellCommand(String command) を使う • ローカルマシンで adb
shell <command> をするのと同等 7 val uiAutomation = InstrumentationRegistry.getInstrumentation().uiAutomation // ը։࢝ uiAutomation.executeShellCommand("screenrecord /sdcard/record.mp4") // ըऴྃ (Ctrl + C) ϓϩηεID͕ඞཁ uiAutomation.executeShellCommand("kill -SIGINT $screenRecordProcessId")
screenrecord のプロセスを得る 8 fun executeCommand(cmd: String): String { val parcelFileDescriptor
= uiAutomation.executeShellCommand(cmd) return ParcelFileDescriptor.AutoCloseInputStream(parcelFileDescriptor).use { inputStream -> inputStream.readBytes().toString(Charset.defaultCharset()) } } fun findProcessIds(processName: String): List<Int> { return executeCommand(cmd = "pidof $processName") .trim() .split(Regex("\\s+")) .filter { it.isNotEmpty() } .map { it.toInt() } } val screenRecordProcessIds = findProcessIds(processName = "screenrecord")
ScreenRecordRule 9 class ScreenRecordRule : TestWatcher() { private val shell
= Shell() private var screenRecordProcessIds: List<Int> = emptyList() override fun starting(description: Description) { shell.executeCommand(cmd = "screenrecord /sdcard/record_${description.methodName}.mp4", awaitOutput = false) screenRecordProcessIds = shell.findProcessIds(processName = "screenrecord") } override fun finished(description: Description) { // গ͠Ԇ͔ͤͯ͞Βऴྃͤ͞ͳ͍ͱ࠷ޙ·ͰըͰ͖ͳ͍ͱ͖͕͋ͬͨͷͰํͳ͘ Thread.sleep(5000) // ͯ͢ͷscreenrecordϓϩηεΛࢭΊΔ // 1σόΠεͰ࣮ߦ͍ͯ͠ΕଞͷςετέʔεͷըΛࢭΊͯ͠·͏Մೳੑ͍ͣ screenRecordProcessIds.forEach { pid -> shell.executeCommand(cmd = "kill -SIGINT $pid", awaitOutput = false) } } }
Github Actionsでの録画データの回収 • テストが失敗したときだけ録画をCIのアーティファクトとして保存する 10 - name: Run android tests
uses: reactivecircus/android-emulator-runner@v2 with: api-level: 31 arch: x86_64 disable-animations: true script: | ./gradlew connectedDebugAndroidTest || (mkdir screen_record; adb shell 'ls sdcard/record_*.mp4' | tr -d '\r' | xargs -I% adb pull % ./screen_record && exit 1) - name: Save screen record if: failure() uses: actions/upload-artifact@v2 with: name: screen-records path: ./screen_record
• adbコマンドを使えばAndroidTestの実⾏の様⼦を 録画できる • 実⾏時の様⼦を知って テスト失敗時の原因追求ができる • 乱⽤するとテスト時間の増⼤に繋がりそう
• ScreenRecordRuleのソースコード https://gist.github.com/mkeeda/30b8cfdcec53859a2cd39cac36d7fc9e 11 まとめ To Be Continued
参考 • Android Debug Bridge (adb) | Android Developers
https://developer.android.com/studio/command-line/adb • UiAutomation | Android Developers https://developer.android.com/reference/android/app/ UiAutomation#executeShellCommand(java.lang.String) • androidx.benchmark.Shell https://cs.android.com/androidx/platform/frameworks/support/+/ androidx-main:benchmark/benchmark-common/src/main/java/androidx/ benchmark/Shell.kt 12