How to test for the appearance of a Toast message

前端 未结 9 1459
一生所求
一生所求 2020-12-16 09:55

Would anyone know how to test for the appearance of a Toast message on an Activity?

I\'m using code similar to what the OP posted on this question for testing my pro

9条回答
  •  轮回少年
    2020-12-16 10:20

    We actually can now test for toast messages using robolectric. The example below is how our team is doing this for now:

        @Test
        public void ccButtonDisplaysToast() throws NullPointerException {
            Button ccRedButton = (Button) findViewById(R.id.cc_red);
            cc_red.performClick(); --> this calls the actual onClickListener implementation which has the toast.
            ShadowLooper.idleMainLooper(YOUR_TIME_HERE); --> This may help you.
            assertThat(ShadowToast.getTextOfLatestToast().toString(), equalTo("TEST_YOUR_TEXT_HERE"));
        }
    

    Hope this helps

提交回复
热议问题