如何测试 Toast 消息的外观

发布于 2024-08-24 12:50:27 字数 243 浏览 5 评论 0原文

有人知道如何测试 Activity 上 Toast 消息的外观吗?

我使用的代码类似于OP在 上发布的代码这个问题用于测试我的程序从一个活动到下一个活动的流程。我还希望能够测试特定活动的 Toast 消息。

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 program flow from one activity to the next. I'd also like to be able to test for toast messages on particular activities.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

征棹 2024-08-31 12:50:28

对于那些现在在 2019 年使用 AndroidX Test API 并使用自定义 Toast 布局的用户,请尝试以下操作 (Kotlin):

@RunWith(AndroidJUnit4:class)
class ActivityUnitTest {
    private lateinit var scenario: ActivityScenario<MainActivity>

    @Before fun setUp() {
        scenario = ActivityScenario.launch(MainActivity::class.java)
    }

    @Test fun shouldDisplayToastErrorMessageIfSearchFieldIsEmpty() {
        scenario.onActivity { activity ->
            activity.id_of_button.performClick()
            assertThat(
                ShadowToast.getLatestToast().view.id_of_textview.text.toString(),
                equalTo("Text to be tested")
            )
        }
    }
}

For those now using the AndroidX Test API in 2019 and using a custom layout for toasts, try this (Kotlin):

@RunWith(AndroidJUnit4:class)
class ActivityUnitTest {
    private lateinit var scenario: ActivityScenario<MainActivity>

    @Before fun setUp() {
        scenario = ActivityScenario.launch(MainActivity::class.java)
    }

    @Test fun shouldDisplayToastErrorMessageIfSearchFieldIsEmpty() {
        scenario.onActivity { activity ->
            activity.id_of_button.performClick()
            assertThat(
                ShadowToast.getLatestToast().view.id_of_textview.text.toString(),
                equalTo("Text to be tested")
            )
        }
    }
}
不喜欢何必死缠烂打 2024-08-31 12:50:28

那么(科特林):

onView(withText("Text of the toast"))
        .inRoot(withDecorView(not(`is`(window.decorView))))
        .check(matches(isDisplayed()))

What about (Kotlin):

onView(withText("Text of the toast"))
        .inRoot(withDecorView(not(`is`(window.decorView))))
        .check(matches(isDisplayed()))
如若梦似彩虹 2024-08-31 12:50:28

我这样使用它:

wait_for_text("Notification message to be verified", timeout: 30)

这是一种替代方法,部分达到了目的。

I am using it like:

wait_for_text("Notification message to be verified", timeout: 30)

This is an alternate way which partially serves the purpose.

因为看清所以看轻 2024-08-31 12:50:27

谁知道如何测试
Toast 消息的外观
活动?

你不能——抱歉。我的意思是,无法询问 Android“嘿,Toast 正在显示吗?它是什么样子的?”。

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

You can't -- sorry. By which, I mean there is no way to ask Android "hey, is a Toast showing? and what does it look like?".

冷心人i 2024-08-31 12:50:27

实际上,我们现在可以使用 robolectric 来测试 toast 消息。下面的示例是我们团队目前的做法:

    @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"));
    }

希望这会有所帮助

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

还给你自由 2024-08-31 12:50:27

嗯,实际上可以测试吐司的外观。只需创建一个Toast 的子类(例如MyOwnToast)并在您的程序中使用它来代替Toast。在此子类中,您可以覆盖 show() 方法来通知您正在显示 Toast。

此外,您可以将 Toast 以 ToastDatabase 单例的形式存储在 show() 方法中,您可以在其中访问 Toast 及其在显示和销毁后的视图(尚未使用 Toast 进行测试,但我经常这样做)活动的结果意图是在它们被销毁后使它们可用于进一步的测试 - 因此使用 Toast 实现这一点应该没有问题)。

请注意:也许您必须克隆 Toast 对象或其对应的 ToastDatabase 视图,因为在 Toast 被销毁后它可能会为 null。希望这有帮助!

Hm, actually there is a possibility to test the appearance of a toast. Simply create a subclass of Toast (e.g. MyOwnToast) and use this one in your program instead of Toast. In this subclass you can overwrite the show() method to notify you, that the Toast is being shown.

Additionally you can store the Toast within the show() method in kind of a ToastDatabase singleton from where you can access the Toast and it's view also after it has been shown and destroyed (haven't tested this with Toasts, but I often do that with the result intents of activities to keep them available for further tests after they have been destroyed - so it should be no problem to implement this with Toasts).

Beware: maybe you have to clone the Toast object or it's corresponding view for the ToastDatabase because probably it will be null after the Toast has been destroyed. Hope this helps!

浅听莫相离 2024-08-31 12:50:27

我检查一下,以下有效:

if(someToast == null)
    someToast = Toast.makeText(this, "sdfdsf", Toast.LENGTH_LONG);
boolean isShown = someToast.getView().isShown();

I check, the following works:

if(someToast == null)
    someToast = Toast.makeText(this, "sdfdsf", Toast.LENGTH_LONG);
boolean isShown = someToast.getView().isShown();
赴月观长安 2024-08-31 12:50:27

您可以检查是否通过消息显示了 toast

ShadowToast.showedToast("expected message")

如果您使用的是自定义 toast,

ShadowToast.showedToast("expected message", R.id.yourToastId)

You could check that the toast was shown by message

ShadowToast.showedToast("expected message")

If you are using a custom toast

ShadowToast.showedToast("expected message", R.id.yourToastId)
冬天旳寂寞 2024-08-31 12:50:27

您可以选择 Robolectric 测试框架。要检查 toast,您可以按如下方式使用它:

assertTrue(ShadowToast.showedCustomToast("message", R.id.message)); //R.id.message: textView ID

you can choose Robolectric test framework.For checking toast, you can use it as below:

assertTrue(ShadowToast.showedCustomToast("message", R.id.message)); //R.id.message: textView ID
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文