使用 Espresso 测试 Google Android 个地点自动完成

Testing Google Android Places Autocomplete with Espresso

我正在努力使用由 Google 提供的 PlaceAutocomplete API 的工具 ui 测试。

我要测试的是打开自动完成 Activity(使用正确的按钮触发)、输入一些文本、从给定列表中选择结果并检查所选择的位置是否列在回收站视图中的能力。

我正在尝试定位下面的这个 EditText :(

与:

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

和其他变体,如 withText "Szukaj" 或 with com.google.android.gms.R.id :) 运气不好。

不胜感激!谢谢!

安装 Android studio 2.2 Beta,试用 Espresso 测试记录器。它会自动为您生成 Espresso 测试用例。

This view is auto-generated by Google Api.

如果这是真的,抱歉我对 Places 没有任何经验API,这意味着 Espresso 无法找到这个视图。

解决方案:Espresso Test Recorder

您可以从以下位置安装最新的 Android Studio 2.2-RC2(不要删除以前的):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) 并尝试通过单击此生成代码来获取此视图,但是我说这可能没用。

解决方案:Espressouiatomator

我对它的怀疑来自 Espresso 框架限制 - 它取决于应用程序的实际上下文,这意味着它可能无法识别意图、生成的代码或通知。

尝试使用像 Google 的 uiatomator 这样的典型检测框架。配合EspressoUI测试框架使用没有问题。

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

您可以在此处找到更多信息:http://google.github.io/android-testing-support-%20/docs/uiautomator/

解决方案:EspressoRobotiumRobotium Test Recorder

如果您发现 uiatomator 很难学,您还可以使用另一个名为 Robotium 的仪器测试框架,它具有简洁明了的语法和一些强大的功能,例如截屏。

它可以与 Espresso 搭配使用。查看本文最后一段:https://github.com/codepath/android_guides/wiki/UI-Testing-with-Robotium

它还有自己的记录器:http://robotium.com/products/robotium-recorder。免费试用,但我怀疑您会觉得它有用,因为 Robotium 代码非常容易学习。

希望对您有所帮助

试试这个:-

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

测试:- Android Studio:3.5.2 |浓缩咖啡核心:3.1.1 | places:1.1.0

Autocompleater 组件似乎无法使用 espresso 访问,一种解决方案是发送输入然后单击 shell

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