0

I have a number of screens with a spinner on the actionbar. When the user changes the spinner option, the list below refreshes with the appropriate data. The activity spawns an async task whenever the user changes the spinner option. But my expresso test seems to fail whenever the option is clicked as the test is looking for a specific post in the list. If I add a sleep, it works fine and finds the post. How do I make the expresso test wait for that async task ?

user1018916
  • 348
  • 4
  • 16

1 Answers1

0

Use the get method for an AsyncTask which (from the docs):

Waits if necessary for the computation to complete, and then retrieves its result.

Should be something like:

try {
  MyAsyncTask myTask = new MyAsyncTask();
  myTask.execute(myParamObject);
  MyResultObject result = myTask.get();
} catch (CancellationException e) {
    Log.e("TAG", e.getMessage(), e);
} catch (InterruptedException e) {
    Log.e("TAG", e.getMessage(), e);
} catch (ExecutionException e) {
    Log.e("TAG", e.getMessage(), e);
}
petey
  • 16,914
  • 6
  • 65
  • 97
  • Thanks, I will try that. I am new to writing the expresso tests. In the documention, it mentions "By default, Espresso waits for UI events in the current message queue to be handled and for default AsyncTasks to complete before it moves on to the next test operation". How does expresso know that an async task has been spawned on a button click and it needs to wait for that to complete before checking for existence of an entry in the list ? – user1018916 Jul 17 '17 at 20:27
  • Yea, it is linda hard to know the exact reason w/o knowing how your test is setup. Perhaps adding some code to you question would help any future answerer. – petey Jul 17 '17 at 20:55