在 Android 视图中使用 UI 元素进行测试

发布于 2024-09-16 14:24:39 字数 2265 浏览 4 评论 0原文

我正在尝试使用以下测试用例测试简单的 UI, 主要思想是在测试中设置一些 UI 文本(以模仿用户输入),然后主动单击事件。

public class StackTestCase 
extends ActivityInstrumentationTestCase2<Stack> 
{
private StackDemo mActivity;

private EditText eaten;
    public StuckTestCase() {
         super("com.crocodil.software.stack", Stack.class);


    }
    public StuckTestCase(Class<Stack> activityClass) {
         super("com.crocodil.software.stack", activityClass);

    }
    protected void setUp() throws Exception {
        super.setUp();
        mActivity = this.getActivity();
        mCount = (Button) mActivity.findViewById(com.crocodil.software.stack.R.id.action);
        eaten = (EditText) mActivity.findViewById(com.crocodil.software.stack.R.id.eaten);

    }

    public void testPreconditions() {
        assertNotNull(mStatus);
      }

    public void testSimpleDefaults(){
        double status = Double.valueOf(mStatus.getText().toString());
        eaten.setText(2);
        mCount.performClick();
        assertEquals((status-2),Double.valueOf(mStatus.getText().toString()));
    }

}

运行结果是异常 -

android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRoot.checkThread(ViewRoot.java:2802)
at android.view.ViewRoot.playSoundEffect(ViewRoot.java:2581)
at android.view.View.playSoundEffect(View.java:8516)
at android.view.View.performClick(View.java:2407)
at com.crocodil.software.stack.test.StackTestCase.testSimpleDefaults(StackTestCase.java:46)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194)
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:186)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:520)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)

每次访问 UI 元素时都会发生这种情况,我无法通过使用句柄或异步任务来避免它?有什么建议吗?

I am attempting to test simple UI with the following test case,
The main idea is to set in the test some of the UI text (to mimic user input) and then actively click an event.

public class StackTestCase 
extends ActivityInstrumentationTestCase2<Stack> 
{
private StackDemo mActivity;

private EditText eaten;
    public StuckTestCase() {
         super("com.crocodil.software.stack", Stack.class);


    }
    public StuckTestCase(Class<Stack> activityClass) {
         super("com.crocodil.software.stack", activityClass);

    }
    protected void setUp() throws Exception {
        super.setUp();
        mActivity = this.getActivity();
        mCount = (Button) mActivity.findViewById(com.crocodil.software.stack.R.id.action);
        eaten = (EditText) mActivity.findViewById(com.crocodil.software.stack.R.id.eaten);

    }

    public void testPreconditions() {
        assertNotNull(mStatus);
      }

    public void testSimpleDefaults(){
        double status = Double.valueOf(mStatus.getText().toString());
        eaten.setText(2);
        mCount.performClick();
        assertEquals((status-2),Double.valueOf(mStatus.getText().toString()));
    }

}

the running result is the exception -

android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRoot.checkThread(ViewRoot.java:2802)
at android.view.ViewRoot.playSoundEffect(ViewRoot.java:2581)
at android.view.View.playSoundEffect(View.java:8516)
at android.view.View.performClick(View.java:2407)
at com.crocodil.software.stack.test.StackTestCase.testSimpleDefaults(StackTestCase.java:46)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194)
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:186)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:520)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)

This happens on each access to the UI elements and i was unable to avoid it by using handles or async task ? any suggestions?

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

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

发布评论

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

评论(2

燃情 2024-09-23 14:24:39

这是一个老问题,但无论如何我都会给你一个答案,以防有人偶然发现它。

除了主线程(UI 线程)之外,您不能从任何地方更改 UI 小部件的状态。您的 PerformClick 必须像这样完成:

mActivity.runOnUiThread(new Runnable() {
  @Override
  public void run() {
    mCount.performClick();
  }
});

但这还不是全部,您还需要通过添加以下行来将仪器测试与 ui 同步:

getInstrumentation().waitForIdleSync();

同步行通常紧接在 runOnUiThread() 代码之后放置。

This is an old question, but I'm giving you an answer anyway, in case someone stumbles upon it.

You are not allowed to change states of UI widgets from anywhere but the main thread (UI thread). Your performClick must be done like this:

mActivity.runOnUiThread(new Runnable() {
  @Override
  public void run() {
    mCount.performClick();
  }
});

But that is not all, you will also need to sync your instrumentation test with the ui, by adding the following line:

getInstrumentation().waitForIdleSync();

The sync line is usually placed immediately after the runOnUiThread() code.

寄居者 2024-09-23 14:24:39

问题是您只能从 UI 线程触摸/更改视图。您可以将可运行程序用作 Rober 概述或注释。

您可以使用 @ 在 UI 线程上运行测试中的所有方法UiThreadTest注释

更新:

此 @interface 在 API 级别 24 中已弃用
请改用 UiThreadTest 。应使用 Android 测试支持库编写新测试。

@UiThreadTest
public void testSimpleDefaults(){
     double status = Double.valueOf(mStatus.getText().toString());
     eaten.setText(2);
     mCount.performClick();
     assertEquals((status-2),Double.valueOf(mStatus.getText().toString()));
}

The issue is that you can only touch / change views from the UI thread. You can use a runnable as Rober outlined or an annotation.

You can run all methods inside a test on the UI thread using the @UiThreadTest annotation:

Update:

This @interface was deprecated in API level 24.
Use UiThreadTest instead. New tests should be written using the Android Testing Support Library.

@UiThreadTest
public void testSimpleDefaults(){
     double status = Double.valueOf(mStatus.getText().toString());
     eaten.setText(2);
     mCount.performClick();
     assertEquals((status-2),Double.valueOf(mStatus.getText().toString()));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文