简体   繁体   中英

Write Android Espresso test

I need to write a UI test to validate that clicking the floating action button results in displaying the SecondActivity.

public class MainActivityTest {

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

  @Before
  public void setUp() throws Exception {

  }

  @Test
  public void onClick() throws Exception {
      onView(withId(R.id.button)).perform(click());
  }
}

Is it enough?

And how can I validate that the Activity properly displays the text contents of an incoming object: name, age, phone number?

I have just started using espresso(

Is it enough?

No, it is not enough. This code onView(withId(R.id.button)).perform(click()); only performs a click on the button, but there is nothing that verifies that the application behaved correctly after that.

To verify that an intent to open the SecondActivity was created, you need to use Espresso Intents .

how can I validate that the Activity properly displays the text contents of an incoming object

You can use something like:

onView(withId(R.id.textView)).check(matches(withText("Expected text")));

Take a look at the Espresso Cheatsheet for more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM