LoginSignup
4
3

More than 3 years have passed since last update.

Firebase Test Labのcloudtestingscreenshotter_libで意図したスクリーンショットが撮れているかをローカルで確認する

Posted at

Firebase Test Labでスクリーンショットを撮るために cloudtestingscreenshotter_lib というライブラリが提供されています。(ライブラリのDL、導入方法などは下記公式ページへ→ Firebase Test Lab インストゥルメンテーション テストのスクリーンショットを作成する

このライブラリを使用することで、Firebase Test Labでスクリーンショットを撮ることができますが、テストコードを作成する過程で 意図したスクリーンショットが撮れているか を毎回Test Labで試すのはちょっとイケておりません。(無料枠だと尚更)

ということで、 cloudtestingscreenshotter_lib で意図したスクリーンショットが撮れているかをローカルで確認する方法をご紹介。(※ここでのローカルとは、Firebase Test Labではなく、PCに繋いだ実機やエミュレーターのことを指します。)

スクリーンショットは /sdcard/screenshots/ に保存されている

はい、これが全てですが、 cloudtestingscreenshotter_libScreenShotter.takeScreenshot で撮影したスクリーンショットは /sdcard/screenshots/ に保存されます。

ですので、スクリーンショットを撮影した後に /sdcard/screenshots/ に保存された画像を見て、意図したスクリーンショットかどうかを確認すればOK。

↓ のようなテストコードの場合


package com.example

@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
    @Test
    fun testIsIntendedScreenshot() {
        // ・・・

        // スクリーンショットを撮る
        ScreenShotter.takeScreenshot("s", activity)
    }
}

↓ のような感じに、スクリーンショット画像が /sdcard/screenshots/ に保存される。
スクリーンショット 2019-08-24 11.20.04.png

これで無駄にTest Labを走らせることなく、意図したスクリーンショットが撮れているか確認することができますね。

ちなみに

スクリーンショットを保存するためには WRITE_EXTERNAL_STORAGE のパーミッションが付与されてないといけないため、 GrantPermissionRule などを使用して、適宜パーミッションが付与される必要があったりします。(Firebase Test Labでは無くても問題ないようだった :thinking:

@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {

    @get:Rule
    var grantPermissionRule: GrantPermissionRule = GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE)
4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3