【问题标题】:Android uiautomator: how to get EditText field by index?Android uiautomator:如何按索引获取 EditText 字段?
【发布时间】:2013-06-30 12:15:05
【问题描述】:

UiSelector 允许您通过文本、textStartsWith、包含等获取 EditText 字段:

   UiObject obj = new UiObject(new UiSelector().text(text));

http://developer.android.com/tools/help/uiautomator/UiSelector.html

但是如何获得第二个文本字段?碰巧该字段没有任何文本或描述。如果我只是这样做

   UiObject textField = new UiObject(new UiSelector().index(index));

然后它返回一个不是带有索引的 EditText 的字段。

谢谢!

【问题讨论】:

标签: android testing automation android-uiautomator


【解决方案1】:

试试

UiObject textField = new UiObject(new UiSelector()
   .className("android.widget.EditText").instance(1)

【讨论】:

  • 谢谢。我以前试过。问题是它不搜索整个屏幕。即,如果 EditText 在 UI 层次结构中向下 3 级,则调用 UiObject textField = new UiObject(new UiSelector() .className("android.widget.EditText").instance(2) 将导致异常:TestSearch 中的错误: com.android.uiautomator.core.UiObjectNotFoundException: UiSelector[CLASS=android.widget.EditText, INSTANCE=1] at com.android.uiautomator.core.UiObject.getText(UiObject.java:481) at com.magnet.mobileserver .tests.android.ui.app.TemplateApp.testSearch(TemplateApp.java:44)
【解决方案2】:

你可以试试这个-

 UiSelector(). className("android.widget.ImageView") .enabled(true). instance(1). setText("Hola!");

 UiObject editText = new UiObject(new UiSelector(). className("android.widget.EditText"));
 editText.setText("Hola!");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-12
    • 1970-01-01
    • 2011-01-30
    • 1970-01-01
    • 2022-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多