2

I'm struggling with instrumental ui test of a PlaceAutocomplete API, provided by Google.

What I'm trying to test is ability to open AutoComplete Activity (fired with proper button), enter some text, pick result from given list and check if picked position is listed on the recyclerview.

I'm trying to target this EditText below :(

Screenshot of ui automater

with:

   onView(withClassName(equalTo(EditText.class.getName())))
                .inRoot(withDecorView(not(is(homeActivityActivityTestRule.getActivity().getWindow().getDecorView()))))
                .perform(typeTextIntoFocusedView("kotek"), closeSoftKeyboard());

And other variations, like withText "Szukaj" or with com.google.android.gms.R.id :) without luck.

Help will be appreciated! Thank You!

piotrek1543
  • 19,130
  • 7
  • 81
  • 94
KrzysztofB
  • 101
  • 11

4 Answers4

2

This view is auto-generated by Google Api.

If it's true, sorry I haven't any experience with Places API, it would mean that Espresso is unable to find this view.

Solution: Espresso Test Recorder

You can install the latest Android Studio 2.2-RC2 (don't remove the previous) from: http://tools.android.com/download/studio/builds/2-2-rc to check brand new Espresso Test Recorder (check: http://tools.android.com/tech-docs/test-recorder) and try to get this view by click on this to generate code, but I said this might not help.

Solution: Espresso with uiatomator

My doubts about it come from Espresso framework limitation - it depends on actual context of app, it means that it may not recognize intents, genereated code or notifications.

Try to use typical instrumentation framework like Google's uiatomator. There's no problem to use it along with Espresso UI testing framework.

Read: http://qathread.blogspot.com/2015/05/espresso-uiautomator-perfect-tandem.html

More info you would find here: http://google.github.io/android-testing-support-%20/docs/uiautomator/

Solution: Espresso with Robotium or Robotium Test Recorder

If you find uiatomator a pretty hard to learn you can also use another instrumentation test framework called Robotium, which has clean and conchise syntax with some powerfull functions like taking screenshots.

It can work along with Espresso. Check last paragraph of this article: https://github.com/codepath/android_guides/wiki/UI-Testing-with-Robotium

It also has its own recorder: http://robotium.com/products/robotium-recorder. Try it for free, but I doubt that you would find it useful, as Robotium code is really simple to learn.

Hope it will help

Community
  • 1
  • 1
piotrek1543
  • 19,130
  • 7
  • 81
  • 94
  • Unfortunately Espresso Test Recorder also failed. Your suspicion about seeing only current(app) context is correct. Well...I think I'll pass on this one and try to refactor this feature in order to communicate with Google Api without using external widgets. But hey! Tool is amazing! Thank you! – KrzysztofB Sep 07 '16 at 16:51
  • @Krzysztof Don't miss Robotium and uiautomator. they are also powerful. If you could reward my answer and what,s more - time and knowledge, i would be happy :-) Glad i could help – piotrek1543 Sep 07 '16 at 16:58
0

Install Android studio 2.2 Beta, Try the Espresso test recorder. It will automatically generate an Espresso test case for you.

Rachit Mishra
  • 6,101
  • 4
  • 30
  • 51
0

Try with this one :-

  Espresso.onView(ViewMatchers.withId(R.id.places_autocomplete_edit_text))
            .perform(ViewActions.typeText("<ADDRESS>"))
        Thread.sleep(3000);
  onView(withText("<ADDRESS FROM SUGESSION>"))
            .inRoot(withDecorView(Matchers.not(activityTestRule.activity.window.decorView)))
            .perform(click())

Tested with :- Android Studio:3.5.2 | espresso-core:3.1.1 | places:1.1.0

Deven
  • 3,078
  • 1
  • 32
  • 34
0

Autocompleater component seems to be not accessible using espresso, one solution is to send typing then clicking through a shell

getInstrumentation().getUiAutomation().executeShellCommand("input text n")
sleep(2000)
getInstrumentation().getUiAutomation().executeShellCommand("input tap " + Math.round(width / 2f) + " " + Math.round(height / 4f))
Jacques Giraudel
  • 352
  • 4
  • 19