Updating an EditText with Espresso

前端 未结 5 1432
慢半拍i
慢半拍i 2020-12-30 19:00

I\'m attempting to update an EditText as part of an Espresso test with:

onView(allOf(withClassName(endsWith(\"EditText\")), withText(is(\"Test\"         


        
相关标签:
5条回答
  • 2020-12-30 19:35

    To set value in EditText with Espresso simple like this

    onView(withId(R.id.yourIdEditText)).perform(typeText("Your Text"))

    0 讨论(0)
  • 2020-12-30 19:41

    You can use the replaceText method.

    onView(allOf(withClassName(endsWith("EditText")), withText(is("Test"))))
        .perform(replaceText("Another test"));
    
    0 讨论(0)
  • 2020-12-30 19:47

    Three things to try:

    1. You can run performs in succession.

    onView(...)
        .perform(clearText(), typeText("Some Text"));
    

    2. There is a recorded issue on the Espresso page which was marked as invalid (but is still very much a bug). A workaround for this is to pause the test in-between performs.

    public void test01(){
        onView(...).perform(clearText(), typeText("Some Text"));
        pauseTestFor(500);
        onView(...).perform(clearText(), typeText("Some Text"));
    }
    
    private void pauseTestFor(long milliseconds) {
        try {
            Thread.sleep(milliseconds);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    

    3. Are you absolutely sure that your EditText contains the text, "Test"?

    0 讨论(0)
  • 2020-12-30 19:51

    You could try two things. First I would try to use

    onView(withId(<id>).perform... 
    

    This way you would always have access to the EditText field even when other EditText fields are on the screen.

    If that's not an option, you could split up your perform calls.

    onView(allOf(withClassName(endsWith("EditText")),withText(is("Test")))).perform(clearText());
    onView(withClassName(endsWith("EditText"))).perform(click());
    onView(withClassName(endsWith("EditText"))).perform(typeText("Another Test");
    
    0 讨论(0)
  • 2020-12-30 19:52

    I was having a similar issue and solved it using the containsString matcher and Class.getSimpleName(). Like this:

    onView(withClassName(containsString(PDFViewPagerIVZoom.class.getSimpleName()))).check(matches(isDisplayed()));
    

    You can see the full code here

    0 讨论(0)
提交回复
热议问题