Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

facebook screenshot-tests-for-android thow NullPointer on make screenshot

In my test

@LargeTest
@RunWith(AndroidJUnit4.class)
public class SimpleActicityTest {


@Rule
public ActivityTestRule<MainActivity> activityActivityTestRule =
        new ActivityTestRule<>(MainActivity.class);

@Test
public void testActivity() throws Exception {
    Activity a = activityActivityTestRule.getActivity();
    Screenshot.snapActivity(activity).setName("s1").record();
}

I have

ava.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.app.Instrumentation.getContext()' on a null object reference
at com.facebook.testing.screenshot.internal.ScreenshotImpl.getInstance(ScreenshotImpl.java:338)
at com.facebook.testing.screenshot.Screenshot.snapActivity(Screenshot.java:45)
at com.mobium.reference.activity.Util.takeScreenshot(Util.java:32)
at com.mobium.reference.activity.SimpleActicityTest.lambda$testLeftMenuTest$1(SimpleActicityTest.java:52)
at com.mobium.reference.activity.SimpleActicityTest.access$lambda$0(SimpleActicityTest.java)
at com.mobium.reference.activity.SimpleActicityTest$$Lambda$1.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5549)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)

source of faceebook's lib getInstance():

public static ScreenshotImpl getInstance() {
        if(sInstance != null) {
            return sInstance;
        } else {
            Class var0 = ScreenshotImpl.class;
            synchronized(ScreenshotImpl.class) {
                if(sInstance != null) {
                    return sInstance;
                } else {
                    Instrumentation instrumentation = Registry.getRegistry().instrumentation;
                    Bundle arguments = Registry.getRegistry().arguments;
                    HostFileSender hostFileSender = new HostFileSender(instrumentation, arguments);
                    sInstance = create(instrumentation.getContext(), arguments, hostFileSender);
                    return sInstance;
                }
            }
        }
    }

It creates Registry and take public field instrumentation, but there are not instrumentation initializations in Registry constructor. How it can work?

like image 333
punksta Avatar asked May 05 '16 16:05

punksta


People also ask

How to do a screenshot test on Android?

A screenshot test is quite similar to a UI test. The only difference is that for assertion the rendered image is compared to a reference image. At the moment, the Android Testing Support Library does not offer any tool to perform screenshot tests. Luckily, Facebook published a screenshot-tests-for-android library a while ago.

How to take a screenshot on Facebook?

Taking a Facebook screenshot on Android device is also quite simple. When you find the Facebook conversation or message that you want to capture, just press the Power and Volume Down button at the same time to capture the screen as an image file.

How does the screenshot testing tool work?

The screenshot testing tool consists of two parts: 1. A core plugin which provides an API for your test code to capture screenshots. When running your tests, the screenshots get stored on the external storage of your device/emulator. 2.

What is @screenshot-tests-for-Android?

screenshot-tests-for-android is a library that can generate fast deterministic screenshots while running instrumentation tests on Android. We mimic Android's measure (), layout () and draw () to generate screenshots on the test thread.


1 Answers

If you want to use AndroidJUnit4 you have to create a custom test runner where the screenshot stuff is initialized.

As described here: https://facebook.github.io/screenshot-tests-for-android/#custom-test-runner

Create a custom test runner in the test directory (androidTest/java/...):

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);
  }
}

In the build.gradle set the testInstrumentationRunner to your custom runner:

defaultConfig {
    // ...
    testInstrumentationRunner "my.package.MyTestRunner" 
}
like image 160
Zarokka Avatar answered Oct 20 '22 14:10

Zarokka