我正在尝试使用 访问我的应用程序的搜索栏,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();
}
}