2017-05-04 51 views
0

當用戶點擊按鈕時,我需要espresso檢查吐司消息彈出。例如。它會顯示'添加到收藏夾'或'從收藏夾中刪除',字符串從string.xml下面。吐司消息可能會有所不同,所以我只能檢查吐司消息是否包含字符串......下面我添加了我嘗試過的espresso代碼,但它不起作用。任何人有更好的主意?espresso檢查部分消息文本的吐司消息

onView(withText(containsString("added to favorite"))).inRoot(withDecorView(not(mActivityTestRule.getActivity().getWindow().getDecorView()))).check(matches(isDisplayed())); 

//string.xml 
<string name="message_add_favorite">%1$s %2$s added to favorite.</string> 
    <string name="message_remove_favorite">%1$s %2$s removed from favorite</string> 

//logcat 
Started running tests 

android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with text: a string containing "added to favorite" 

View Hierarchy: 
+>LinearLayout{id=-1, visibility=VISIBLE, width=749, height=129, has-focus=false, has-focusable=false, has-window-focus=false, 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=0.0, y=0.0, child-count=1} 
| 
+->AppCompatTextView{id=16908299, res-name=message, visibility=VISIBLE, width=669, height=49, has-focus=false, has-focusable=false, has-window-focus=false, 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=40.0, y=40.0, text=Pizza Smith @ Galaxis removed from favourites, input-type=0, ime-target=false, has-links=false} 
| 
at dalvik.system.VMStack.getThreadStackTrace(Native Method) 
at java.lang.Thread.getStackTrace(Thread.java:580) 
at android.support.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:92) 
at android.support.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:56) 
at android.support.test.espresso.ViewInteraction.runSynchronouslyOnUiThread(ViewInteraction.java:184) 
at android.support.test.espresso.ViewInteraction.check(ViewInteraction.java:158) 
at com.korvac.liquid.presentation.intro.DiscountTest.test_08FavoriteTest(DiscountTest.java:161) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55) 
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:270) 
at org.junit.rules.RunRules.evaluate(RunRules.java:20) 
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
at org.junit.runners.Suite.runChild(Suite.java:128) 
at org.junit.runners.Suite.runChild(Suite.java:27) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
at org.junit.runner.JUnitCore.run(JUnitCore.java:115) 
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59) 
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262) 
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1846) 
+0

究竟是什麼不起作用?你會得到哪些例外? (我試過它在一個演示應用程序,它似乎工作得很好) – stamanuel

+0

@stamanuel添加logcat,在測試過程中,我可以看到與xxx添加到收藏夾彈出消息的吐司消息 – kggoh

回答

0

你可以試試這個onView(allOf(withText(containsString("added to favorite")))).check(matches(isDisplayed()));