1

我正在尝试使用 访问我的应用程序的搜索栏,id然后输入搜索文本并提交,但是当我尝试获取视图时,我的测试用例失败并抛出异常:

添加完整日志

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:5932)
at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:873)
at android.view.ViewGroup.invalidateChild(ViewGroup.java:4253)
at android.view.View.invalidate(View.java:10663)
at android.widget.TextView.invalidateRegion(TextView.java:4609)
at android.widget.TextView.invalidateCursor(TextView.java:4552)
at android.widget.TextView.spanChange(TextView.java:7434)
at android.widget.TextView$ChangeWatcher.onSpanAdded(TextView.java:9130)
at android.text.SpannableStringBuilder.sendSpanAdded(SpannableStringBuilder.java:979)
at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:688)
at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:588)
at android.text.Selection.setSelection(Selection.java:76)
at android.text.Selection.setSelection(Selection.java:87)
at android.text.method.ArrowKeyMovementMethod.initialize(ArrowKeyMovementMethod.java:302)
at android.widget.TextView.setText(TextView.java:3777)
at android.widget.TextView.setText(TextView.java:3647)
at android.widget.EditText.setText(EditText.java:80)
at android.widget.TextView.setText(TextView.java:3622)
at com.expedia.search.test.SearchActivityTester.testAllCountries(SearchActivityTester.java:48)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)

下面是我的代码:

View searchBar = solo.getView("search_edit_text"); //get the element
searchBar.performClick(); //click on search bar
solo.sendKeys("My Search Query"); //enter my query
solo.sendKey(KeyEvent.KEYCODE_SEARCH);  //hit search on softkeyboard

我是 Android 自动化测试的新手,所以不知道。请建议。另外,我使用的上述方法是否足以胜任这项工作?

这是我的测试课

public class SearchActivityTester extends ActivityInstrumentationTestCase2 {

private static final String LAUNCH_ACTIVITY_NAME = "com.my.aut.activity.SearchActivity";

private Solo solo;

private static Class<?> splashActivityClass;
static {
    try {
        splashActivityClass = Class.forName(LAUNCH_ACTIVITY_NAME);
    }
    catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
}

public SearchActivityTester() throws ClassNotFoundException {
    super(splashActivityClass);
}

@Override
protected void setUp() throws Exception {
    solo = new Solo(getInstrumentation(), getActivity());
}

public void testAllCountries() {
        solo.sleep(7000);
        solo.clickOnText("Countries");
        Log.d("clicked", "clicked on hotels");
        solo.sleep(2000);
        View searchBar = solo.getView("search_edit_text"); //get the element
           searchBar.performClick(); //click on search bar
           solo.sendKeys("My Search Query"); //enter my query
           solo.sendKey(KeyEvent.KEYCODE_SEARCH);  //hit search on softkeyboard


        List<ListView> list = solo.getCurrentViews(ListView.class);
        Log.d("****************", list.get(1).getId()+"");

        solo.sleep(3000);
}


@Override
public void tearDown() throws Exception {
    solo.finishOpenedActivities();

}

 }
4

2 回答 2

2

尝试将此代码集成到您的代码中:

private StackDemo mActivity;

public SearchActivityTester() throws ClassNotFoundException {
    super(splashActivityClass);
}

@Override
protected void setUp() throws Exception {
    solo = new Solo(getInstrumentation(), getActivity());
    mActivity = this.getActivity();
}

public void testAllCountries() {
    solo.sleep(7000);
    solo.clickOnText("Countries");
    Log.d("clicked", "clicked on hotels");
    solo.sleep(2000);

    mActivity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            View searchBar = solo.getView("search_edit_text"); //get the element
            searchBar.performClick(); //click on search bar
        }
    });



    solo.sendKeys("My Search Query"); //enter my query
    solo.sendKey(KeyEvent.KEYCODE_SEARCH);  //hit search on softkeyboard


    List<ListView> list = solo.getCurrentViews(ListView.class);
    Log.d("****************", list.get(1).getId()+"");

    solo.sleep(3000);
}


@Override
public void tearDown() throws Exception {
    solo.finishOpenedActivities();

}

}
于 2014-02-13T09:49:53.500 回答
0

取决于你ActivityInstrumentationTestCase2为你的单元测试扩展什么类(通常)你应该能够调用

getActivity() 

在返回的对象上,您可以调用runOnUiThread以更改 UI。

你可能想看看

于 2014-02-13T08:11:28.747 回答