在不同的活动中测试两个文本

Test two text in different activies

在 Activity A 上,我单击一个按钮并调用 Activity B。然后如果来自 Activity B 的 addressNickNameView 在 itemAddressNickNameView

上相同,则我匹配
     onView(withId(R.id.addressNickNameView))
            .check(matches(withId(R.id.itemAddressNickNameView)))`

但是我遇到了这个问题。

android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with id: 2131363004' doesn't match the selected view. Expected: with id: br.com.fastshop.ecommerce.mock.teste:id/itemAddressNickNameView Got: "TextInputEditText{id=2131362631, res-name=component_fast_edittext_edit_text_cpf, visibility=VISIBLE, width=998, height=72, has-focus=false ....

我认为 Matcher 不能像跨活动那样工作。在转到第二个 activity 之前,您需要从第一个 activity 获取值,例如:

@Rule
public ActivityTestRule<ActivityA> mActivityTestRule = new ActivityTestRule<>(ActivityA.class);

@Test
public void test() {
    EditText editTextA = mActivityTestRule.getActivity().findViewById(R.id.itemAddressNickNameView);
    String textA = editTextA.getText().toString();

    // Move to Activity B
    onView(R.id.button).perform(click());

    onView(withId(R.id.addressNickNameView)).check(matches(withText(textA)));
}