显示警告对话框,同时运行的Andr​​iod测试用例 [英] Display alert dialog while running an Andriod test case

查看:196
本文介绍了显示警告对话框,同时运行的Andr​​iod测试用例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Robotium自动化应用程序的测试。有没有办法在执行一个特定的测试情况下,我可以显示一个警告框。

I am using Robotium to automate testing of an application. Is there a way I can display an alert box while executing a particular test case.

感谢。

推荐答案

Robotium测试没有对UI线程运行,所以在测试方法的任何code将在最好的情况下,只是没有在最坏的情况下工作,抛出和错误,并导致测试失败。

Robotium tests do not run on the UI Thread, so any code in a test method will in the best case just not work and in the worst case throw and error and cause your test to fail.

要与你需要在UI线程运行code的测试方法里面的UI交互。这可以通过编写code里面的一个Runnable,并发送该Runnable到当前活动的 runOnUiThread()方法来实现。 Robotium的独奏类有 getCurrentActivity()的方法,这将允许该执行。这里有一个如何你会使用这个技术表现出吐司的例子。

To interact with the UI from inside a test method you need to run your code on the UI Thread. This can be done by writing that code inside a Runnable, and sending that Runnable to the runOnUiThread() method of the current Activity. Robotium's solo class has the getCurrentActivity() method, which will allow this execution. Here's an example of how you'd show a Toast using this technique.

public void testDisplayToastInActivity() throws Exception
{
    Runnable runnable = new Runnable
    {
        @Override
        public void run()
        {
            Toast.makeText(solo.getCurrentActivity(), "Hello World", Toast.LENGTH_LONG).show();
        }
    }

    solo.getCurrentActivity().runOnUiThread(runnable);
 }

您可以使用 runOnUiThread()执行与您互动的活动很多其他操作,如创建警报对话框,如果你想比面包更多的东西。不过,我建议对做任何这一点,即使你可以。 Robotium和其他测试框架是为了判断您的应用程序code的执行的正确性,而不应超出注入与应用程序交互的方式将用户的任何逻辑或UI修改行为。您的测试将是清洁的,如果你从你的任何测试outpu并记录他们的logcat或文件。

You can use runOnUiThread() to perform many other actions that interact with your Activity, such as creating Alert Dialogs, if you want something more than a Toast. However, I'd suggest against doing any of this, even though you can. Robotium and other testing frameworks are meant to judge the correctness of your application code's execution, and should not inject any logic or UI modifying behavior beyond interacting with your application the way a user would. Your tests will be cleaner if you take any outpu from your tests and log them to Logcat or a file.

这篇关于显示警告对话框,同时运行的Andr​​iod测试用例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆