2016-06-06 7 views
1

Ich möchte auf mehrere Aktivitäten in meiner Instrumententests zugreifen. z. B. Login -> Suche-> Listing-> Detail AktivitätAndroid, EspressoTesting mit mehreren Aktivitäten

Ich habe bis 'Listing activity' erreicht, aber ich möchte die Detailseite des Listing Activity Element [1] aufrufen.

Unten ist mein Code für

@RunWith(AndroidJUnit4.class) 
    public class ContactSearchScreeenTest extends ActivityInstrumentationTestCase2<ContactSearchScreen> { 

    public ContactSearchScreeenTest() { 
     super(ContactSearchScreen.class); 
    } 

    @Rule 
    public ActivityTestRule<ContactSearchScreen> mActivityRule = 
      new ActivityTestRule<>(ContactSearchScreen.class); 

    @Override 
    protected void setUp() throws Exception { 
     super.setUp(); 
    } 

    @Test 
    public void sendToSearchResultActivity() 
    { 

     onView(withId(R.id.etSearchName)) 
         .perform(typeText("ssasa"), pressKey(KeyEvent.KEYCODE_SEARCH)); 


     GlobalClass globalVariable = (GlobalClass) mActivityRule.getActivity().getApplicationContext(); 
     globalVariable.setSearchStr("ssasa"); 

     mActivityRule.getActivity().callForNextSearchActivity(); 

    } 

} 

zusätzliche funktionelle

@Override 
    public void callForNextSearchActivity() { 
    Intent intent = new Intent(getBaseContext(), SearchResultsActivity.class); 
    final GlobalClass globalVariable = (GlobalClass) getApplicationContext(); 
    globalVariable.setSearchStr(getSearchStringFromSearchEditText()); 
    startActivity(intent); 
    overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left); 

} 

Ist es möglich, Mehr Aktivität Schicht in Espresso-Tests zu haben? Wenn ja .. Wie?

Antwort

2

Ja, es ist möglich. In einer der Proben haben sie dies demonstriert.

https://code.google.com/p/android-test-kit/source/browse/testapp_test/src/main/java/com/google/android/apps/common/testing/ui/testapp/BasicTest.java#52][1]

public void testTypingAndPressBack() { 
// Close soft keyboard after type to avoid issues on devices with soft keyboard. 
onView(withId(R.id.sendtext_simple)) 
    .perform(typeText("Have a cup of Espresso."), closeSoftKeyboard()); 

onView(withId(R.id.send_simple)) 
    .perform(click()); 

// Clicking launches a new activity that shows the text entered above. You don't need to do 
// anything special to handle the activity transitions. Espresso takes care of waiting for the 
// new activity to be resumed and its view hierarchy to be laid out. 
onView(withId(R.id.display_data)) 
    .check(matches(withText(("Have a cup of Espresso.")))); 

// Going back to the previous activity - lets make sure our text was perserved. 
pressBack(); 

onView(withId(R.id.sendtext_simple)) 
    .check(matches(withText(containsString("Espresso")))); 
} 

den Inline-Kommentar lesen.