facebook / screenshot-tests-for-android

Generate fast deterministic screenshots during Android instrumentation tests
http://facebook.github.io/screenshot-tests-for-android
Apache License 2.0
1.74k stars 229 forks source link

Screenshot within Espresso were too small #287

Closed liweijian closed 3 years ago

liweijian commented 3 years ago

I'm trying to use Screenshot within Espresso using this demo: https://github.com/cortinico/kotlin-android-template

It should look like this if all goes well:

And:

The espresso case that I modified:

    @Test
    @Throws(Throwable::class)
    fun typeANumber_resultIsDisplayed() {
        onView(withId(R.id.edit_text_factorial)).perform(typeText("1"), closeSoftKeyboard())
        val targetContext = InstrumentationRegistry.getInstrumentation().targetContext
        val inflater = LayoutInflater.from(targetContext)
        val view = inflater.inflate(R.layout.activity_main, null, false)

        ViewHelpers.setupView(view).setExactWidthDp(300).layout()
        Screenshot.snap(view).setName("before_click").record()

        onView(withId(R.id.button_compute)).perform(click())
        Screenshot.snap(view).setName("after_click").record()
        onView(withId(R.id.text_result)).check(matches(isDisplayed()))
        onView(withId(R.id.text_result)).check(matches(withText("1")))
    }

With the MyTestRunner.java:

package com.ncorti.kotlin.template.app;

import android.os.Bundle;

import androidx.test.runner.AndroidJUnitRunner;

import com.facebook.testing.screenshot.ScreenshotRunner;

public class MyTestRunner extends AndroidJUnitRunner {
    @Override
    public void onCreate(Bundle args) {
        ScreenshotRunner.onCreate(this, args);
        super.onCreate(args);
    }

    @Override
    public void finish(int resultCode, Bundle results) {
        ScreenshotRunner.onDestroy();
        super.finish(resultCode, results);
    }
}

However, I got these weird size files: image

Like, before_click_1_0.png: before_click_1_0

And after_click_1_0.png: after_click_1_0

It seems that we only capture partial of the screenshot, I was wondering if maybe anything that I had missed?

oradkovsky commented 3 years ago

What you are observing is exactly correct. If you look into com.facebook.testing.screenshot.internal.ScreenshotImpl, you will notice parameter called like so:

private int mTileSize = 512;

This is what makes all these slices happen. You can reconfigure the whole thing to be bigger but no need. If you want to see complete image of your screen capture, you need to either use recording task or verification task. Former will essentially create complete images of what you see. Latter one will try to compare with baseline, producing expected, actual, *diff images if issues exist.