2017-05-19 21 views
3

我想編寫espresso腳本來測試深層鏈接,但不知道如何開始。尋找能夠幫助我獲得更多想法的解決方案,可能一步一步地開始如何開始。使用android espresso自動執行深層鏈接

例如:我正在尋找一個場景,就像您在gmail中獲取一個鏈接,然後點擊哪個用戶應該指向移動應用程序。我如何開始使用espresso測試這樣的事情?

在此先感謝。

+0

[如何在Android中編寫深度鏈接測試?](https://stackoverflow.com/questions/42951216/how-to-write-tests-for-deep-links-in-android) – Ixx

回答

1

開始與活動規則

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

然後你想要的東西來解析從鏈接的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; 
    } 

然後你要查看的活動規則推出的意圖

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

那麼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的活動正確檢索的。