Skip to content

Instantly share code, notes, and snippets.

@zwliew
Created February 26, 2019 08:07
Show Gist options
  • Save zwliew/e67dd86b8e9f37fe40505e8629c7a4b3 to your computer and use it in GitHub Desktop.
Save zwliew/e67dd86b8e9f37fe40505e8629c7a4b3 to your computer and use it in GitHub Desktop.
UI Testing the Get Hacking Android App
import androidx.test.espresso.intent.Intents;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.intent.Intents.intended;
import static androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
@RunWith(AndroidJUnit4.class)
public class IntroductionActivityTest {
@Rule
public final ActivityScenarioRule<IntroductionActivity> scenarioRule
= new ActivityScenarioRule<>(IntroductionActivity.class);
@Before
public void setUp() {
Intents.init();
}
@After
public void tearDown() {
Intents.release();
}
@Test
public void onLaunch_displaysAllViews() {
onView(withId(R.id.single_intro_text)).check(matches(isDisplayed()));
onView(withId(R.id.single_intro_end_button)).check(matches(isDisplayed()));
}
@Test
public void onClickButton_showsMainActivity() {
onView(withId(R.id.single_intro_end_button)).perform(click());
intended(hasComponent(MainActivity.class.getName()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment