2017-10-06 66 views

回答

1

试试这个:如果TextView中包含的任何文本

import static android.support.test.espresso.Espresso.onView; 
import static android.support.test.espresso.action.ViewActions.click; 
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.not; 

@RunWith(AndroidJUnit4.class) 
public class MainActivityInstrumentedTest { 

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

    @Test 
    public void checkTextView_isDisplayed_and_notEmpty() throws Exception { 
     // perform a click on the button 
     onView(withId(R.id.button)).perform(click()); 

     // passes if the textView does not match the empty string 
     onView(withId(R.id.textView)).check(matches(not(withText("")))); 
    } 
} 

测试将只通过。

+0

当textView为空时,上面的代码将使测试通过。当textView为空时,我想让测试失败。当它空着时,我不希望它通过。 –

+0

我已更新我的答案,再试一次,让我知道它是否有效 – chornge

+1

是上述编辑的代码工作。 –

相关问题