使用 android espresso 自动进行深度链接

Automating deep linking using android espresso

我想编写 espresso 脚本来测试深度 linking,但不知道如何开始。寻找可以帮助我获得更多想法的解决方案,可能是关于如何开始的逐步过程。

例如:我正在寻找一个场景,例如您在 gmail 中收到一个 link,点击哪个用户应该被定向到移动应用程序。我如何开始使用浓缩咖啡测试类似的东西?

提前致谢。

从 activity 规则开始

 @Rule
 public ActivityTestRule<YourAppMainActivity> mActivityRule =
            new ActivityTestRule<>(YourAppMainActivity.class, true, false);

然后你想要一些东西来解析来自 link 和 return 意图的 uri

String uri = "http://your_deep_link_from_gmail"; 
private Intent getDeepLinkIntent(String uri){
        Intent intent = new Intent(Intent.ACTION_VIEW,
                Uri.parse(uri))
                .setPackage(getTargetContext().getPackageName());


        return intent;
    }

然后您希望 activity 规则启动意图

Intent intent = getDeepLinkIntent(deepLinkUri);
mActivityRule.launchActivity(intent);

IntentTestRule 不能正常工作。所以我会尝试这样 ActivityTestRule :

public ActivityTestRule<MyActivity> activityTestRule = new ActivityTestRule<MyActivity>(MyActivity.class, false, false);

然后我将编写适当的 UI 单元测试,如下所示:

@Test
public void testDeeplinkingFilledValue(){
        Intent intent = new Intent(InstrumentationRegistry.getInstrumentation()
                .getTargetContext(), MyActivity.class );

        Uri data = new Uri.Builder().appendQueryParameter("clientName", "Client123").build();
        intent.setData(data);

        Intents.init();
        activityTestRule.launchActivity(intent);


        intended(allOf(
                hasComponent(new ComponentName(getTargetContext(), MyActivity.class)),
                hasExtras(allOf(
                        hasEntry(equalTo("clientName"), equalTo("Client123"))
                ))));
        Intents.release();
}

有了这个,您将测试具有给定查询参数的深层链接实际上是否被处理深层链接 Intent 的 activity 正确检索。