Facebook的Android截图测试在进行截图时会抛出NullPointerException异常。

7
在我的测试中
@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)

Facebook的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;
                }
            }
        }
    }

它创建了Registry并获取了public的字段instrumentation,但是Registry构造函数中没有仪器初始化。它如何工作?

1个回答

14

如果您想使用AndroidJUnit4,则必须创建一个自定义测试运行程序,并在其中初始化截图内容。

如此所述: https://facebook.github.io/screenshot-tests-for-android/#custom-test-runner

在测试目录(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);
  }
}
在build.gradle中将testInstrumentationRunner设置为您的自定义运行程序:
defaultConfig {
    // ...
    testInstrumentationRunner "my.package.MyTestRunner" 
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接