android - 编写 Android Espresso 测试

标签 android testing android-espresso

我需要编写一个 UI 测试来验证单击 float 操作按钮是否会导致显示 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());
  }
}

够了吗?

我如何验证 Activity 是否正确显示传入对象的文本内容:姓名、年龄、电话号码?

我刚开始使用 Espresso (

最佳答案

Is it enough?

不,这还不够。这段代码 onView(withId(R.id.button)).perform(click());仅对按钮执行单击,但之后没有任何东西可以验证应用程序是否正确运行。

要验证打开 SecondActivity 的 Intent 是否已创建,您需要使用 Espresso Intents .

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

你可以使用类似的东西:

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

看看 Espresso Cheatsheet了解更多。

关于android - 编写 Android Espresso 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44810315/

相关文章:

android - 图像加载器不工作

android - 如何在导航栏上方和后面有 View 的同时拥有透明的导航栏?

java - 收集PlaceholderFragment中的数据并发送给Activity

testing - 如何从 "main()"测试 "go test"例程?

javascript - 如何计算 Protractor 中下拉选项的总数

android - 在 Espresso 中单击具有相同 ID 的多个 View 之一

android - 如何在 Espresso 测试中调用自定义 View 的方法?

Android 应用程序链接失败,SingleHostAsyncVerifier 返回失败

python - 在使用 Django TestCase 进行测试时如何检查 ValidationError 异常?

android - 使用 espresso 测试软键盘是否可见