Espresso2.0 typeText(string)bug?

时间:2015-05-27 05:49:05

标签: android-espresso

我使用的是Espresso2.0。

String str = "Hello_world";
onView( withId(R.id.et_jumpfrom) )
        .perform(  typeText(str) ,  closeSoftKeyboard()  );
onView( withId(R.id.btn_jumpform_reshow))
        .perform( click() );

// check the result
onView( withId(R.id.tv_jumpfrom_display))
        .check( matches( withText(str) ));

当我使用

typeText("hello")

check()结果没问题。

但是,当我使用

typeText("Hello_world");

检查失败。

我检查了错误消息,发现EditText实际上是键入的

H'e'l'l'o_'w'o'r'l'd

这真的很奇怪。我该如何解决这个问题?

以下是我的全部代码:

  1. 的build.gradle

    dependencies {
        // Testing-only dependencies
        androidTestCompile 'com.android.support.test:runner:0.2'
        androidTestCompile ('com.android.support.test.espresso:espresso-core:2.1')
    }
    
  2. 测试

    import static android.support.test.espresso.Espresso.onView;
    import static android.support.test.espresso.Espresso.pressBack;
    import static android.support.test.espresso.action.ViewActions.click;
    import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
    import static android.support.test.espresso.action.ViewActions.typeText;
    import static android.support.test.espresso.assertion.ViewAssertions.matches;
    import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
    import static android.support.test.espresso.matcher.ViewMatchers.withId;
    import static android.support.test.espresso.matcher.ViewMatchers.withText;
    import static org.hamcrest.Matchers.containsString;
    
    @RunWith(AndroidJUnit4.class)
    @LargeTest
    public class JumpActivityTest {
        public String str = "Espresso_Test";
    
        @Rule
        public ActivityTestRule<JumpFromActivity> actvRule = new ActivityTestRule<>(JumpFromActivity.class);
    
        @Test
        public void changeText_sameActv() {
            onView(withId(R.id.et_jumpfrom))
                    .perform(typeText(str), closeSoftKeyboard());
            onView(withId(R.id.btn_jumpform_reshow))
                    .perform(click());
    
            // check the result
            onView(withId(R.id.tv_jumpfrom_display))
                    .check(matches(withText(str)));
        }     
    
    }
    
  3. 期望

    输入了EditText&#34; Espress_Test&#34;

  4. 实际输出

    android.support.test.espresso.base.DefaultFailureHandler$AssertionFailedWithCauseError: 'with text: is "Espresso_Test"' doesn't match the selected view.
    Expected: with text: is "Espresso_Test"
    Got: "TextView{id=2131165188, res-name=tv_jumpfrom_display, visibility=VISIBLE, width=353, height=59, has-focus=false, has-focusable=false, has-window-focus=true, is-clickable=false, is-enabled=true, is-focused=false, is-focusable=false, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=183.0, y=96.0, text=E's'p're's'so'_T'e'st, input-type=0, ime-target=false, has-links=false}"
    
    at dalvik.system.VMStack.getThreadStackTrace(Native Method)
    at java.lang.Thread.getStackTrace(Thread.java:579)
    at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:82)
    ...
    
  5. 然后我看到EditText中的文本实际上是

    text=E's'p're's'so'_T'e'st
    

1 个答案:

答案 0 :(得分:0)

这是因为IME。

baidu IME有这个问题。

谷歌IME会好起来的。