1

I am trying to access a search bar of my app using the id and then entering search text and submit but as I try to grab the view, my test case fails and throws Exception:

Adding full logs

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)

Below is my code :

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

I am new to automation testing for Android so have no idea. Please suggest. Also if the above method I am using is right enough to do the job or not?

Here is my test class

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();

}

 }
roger_that
  • 9,493
  • 18
  • 66
  • 102
  • Generally this error occurs whenever you try to access a View item from Thread code. – user3301551 Feb 13 '14 at 08:03
  • I am unable to get this "Thread code" as to why the error? Obviously a thread would run when i launch my test case so ultimately view is being fetched from thread only. So how to fix it? – roger_that Feb 13 '14 at 08:05
  • Also when I change the code to access EditText rather than a View, it shows it cannot find the EditText with the id. The code is `EditView searchBar = solo.getEditText("com.my.aut.R.id.search_bar_id")` – roger_that Feb 13 '14 at 08:08

2 Answers2

2

Try to integrate this code to your code:

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();

}

}
Flavio Capaccio
  • 706
  • 4
  • 15
0

depending on what class are you extending (usually ActivityInstrumentationTestCase2) for your unit tests you should be able to call

getActivity() 

and on the returned object you can call runOnUiThread to make changes to the UI.

you might want to give it a look

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • `ActivityInstrumentationTestCase2` is the super class. I am trying to find a way to access the search bar which is nothing but EditView and be able to fill up my search query and hit search key. – roger_that Feb 13 '14 at 08:13
  • if it is the super class, that means that you are extending it. That means that you can call getActivity – Blackbelt Feb 13 '14 at 08:14
  • Actually, its blackbox testing. I don't have the source code so i can't import MainActivity class of my AUT – roger_that Feb 13 '14 at 08:39
  • can you post your testing class? – Blackbelt Feb 13 '14 at 08:40