在uiautomator中从AutoCompleteTextView中选择项目

14

更新

该项目已不再维护,但欢迎提供解决方案。谢谢。


AutoCompleteTextView adapter issue for selecting item

我正在创建一个自动化测试项目,遇到了从AutoCompleteTextView中选择项的问题。

您可以看到截图和视图(全部展开)。AutoCompleteTextView的下拉列表既不出现在视图树中,也无法使用鼠标选择。

我尝试了以下方法来从AutoCompleteTextView适配器中选择项:

  1. UiScrollable locationList = new UiScrollable(new UiSelector().scrollable(true)); locationList.scrollTextIntoView(location);

  2. UiScrollable locationList = new UiScrollable(locationEditText.getSelector()); locationList.scrollTextIntoView(location); 这里的locationEditText是我的AutoCompleteTextView

  3. UiObject selectedLocation = locationList.getChild(new UiSelector().text(location)); selectedLocation.click(); 从locationList中使用传递的字符串不会选择该项。

  4. editLocationResId = "android:id/text1"; UiObject selectedLocation = new UiObject(new UiSelector().resourceId(editLocationResId)); selectedLocation.click(); 来自适配器文本视图的ID也不起作用。

有人能帮我在中选择AutoCompleteTextView中的项吗?或者提供更多方法以得到所需的输出。


我看到很多与此相关的问题,但仍然没有可靠的解决方案!:(这是链接:https://groups.google.com/forum/#!msg/appium-discuss/0XAST8Bs3hw/zW2yRns5ia0J这个问题已经发布很久了。此外,完成此操作的方法是点击坐标。同样,对于不同的设备仍然存在差异。 - DearDhruv
我在许多设备上测试了UIAutomator测试,并发现只有一些设备提供辅助功能以执行自动化。Nexus设备提供比三星/索尼/HTC设备更好的辅助功能,而(Micromax)Canvas设备根本不提供(非常微小的)。 - DearDhruv
7个回答

0

我已经创建并一直在使用最简单和最高效的方法。只需在自动完成文本视图中定位,键入,然后选择所需的值。
希望你也可以使用这种方法。

driver.findElement(By.id(“Element ID”)).clear();
driver.findElement(By.id(“Element ID”)).sendKeys("Type what you want to select");
driver.pressKeyCode(AndroidKeyCode.KEYCODE_PAGE_DOWN);
driver.pressKeyCode(AndroidKeyCode.ENTER);

0
如果你需要选择下拉列表中的第一个元素,我已经找到了一个有用的解决方法:
public void selectFirstElement() {
       TouchAction touchAction = new TouchAction(getAppiumDriver());
       int xTapped = autoCompleteTextView.getLocation().getX() +  autoCompleteTextView.getSize().getWidth() / 2;
       int yTapped = autoCompleteTextView.getLocation().getY() +  autoCompleteTextView.getSize().getHeight() ;
       touchAction.tap(xTapped, yTapped).perform(); }

假设已经声明了一个名为autoCompleteTextView的AutoCompleteTextView,你可以获取其在屏幕上的当前位置,并计算出位于其下方的项目。希望这能帮到你!


0

首先打开选取列表,然后尝试使用UiDevice.pressKey()发送DPAD_DOWN或UP事件并按Enter键选择所需项目。


0

嗯,我找到了一个解决方法(这是针对AutoCompleteTextView的,用于Spinner删除setText()):

UiObject edit = mDevice.findObject(new UiSelector().className(EditText.class).instance(0));
        edit.click();
        edit.setText(name);
        Rect rect = edit.getBounds();
        mDevice.waitForWindowUpdate(Package.DIGI_PACKAGE, 1000);
        mDevice.click(rect.left + 64, rect.bottom + 72);

获取对象,点击它,插入您想要的文本,等待下拉菜单出现,然后在其下方点击一些像素。


0

获取文本框的坐标:

int x = element.getLocation().getX();
int y = elmement.getLocation().getY();

添加数值以获取正确的坐标。

x = x+x1
y = y+y1

使用tap函数进行单击操作。

TouchAction action = new TouchAction(driver);

action.tap(x,y).perform();


0

您可以通过元素的文本来选择要选择的元素。确保在建议列表显示后执行此代码行。

//Get a reference to the autocomplete element
UiObject2 autocomplete = device.findObject(By.res("yourpackage","autocomplete_id"));
//Put a text that will trigger the suggestion list
autocomplete.setText("68623 Lampertheim"); 
//Gain the focus
autocomplete.click();
//Finally, find the element that we want in the suggestion list and click it
device.findObject(By.text("68623 Lampertheim")).click();

希望能有所帮助。


-1

尝试下面的代码,它对我有效。

我正在使用AutoCompleteText来自动完成用户当前所在位置,locationList只是我在strings.xml文件中编写的数组,因此请在此处使用您自己的字符串数组。

  locationList = res.getStringArray(R.array.ticketLocation);

        ArrayAdapter<String> locationAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, locationList);

        AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.txtCountries);
        textView.setThreshold(1);
        textView.setAdapter(locationAdapter);
        textView.setValidator(new Validator());
        textView.setOnFocusChangeListener(new FocusListener());

        textView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // TODO Auto-generated method stub
                TextView ticketLocation = (TextView) view;
                getTicketLocation = ticketLocation.getText().toString();
            }
        });

以下是验证位置字段文本输入的代码,fixText() 方法可以防止用户输入不存在于字符串数组中的文本,例如:如果用户输入了不在您的字符串数组列表中的 "germany",它将被替换为 " ",即空字符串,出现在您的编辑文本输入字段中。

 class Validator implements AutoCompleteTextView.Validator {

        @Override
        public boolean isValid(CharSequence text) {
            // Log.v("Test", "Checking if valid: " + text);
            Arrays.sort(locationList);
            if (Arrays.binarySearch(locationList, text.toString()) > 0) {
                return true;
            }

            return false;
        }

        @Override
        public CharSequence fixText(CharSequence invalidText) {
            // Log.v("Test", "Returning fixed text");

            /*
             * I'm just returning an empty string here, so the field will be
             * blanked, but you could put any kind of action here, like popping
             * up a dialog?
             *
             * Whatever value you return here must be in the list of valid
             * words.
             */
            return "";
        }
    }

    class FocusListener implements View.OnFocusChangeListener {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            // Log.v("Test", "Focus changed");
            if (v.getId() == R.id.txtCountries && !hasFocus) {
                // Log.v("Test", "Performing validation");
                ((AutoCompleteTextView) v).performValidation();
            }
        }
    }

2
感谢您的努力。 但请仔细阅读问题,然后尝试发表评论或回答。 如果您不理解问题,可以提问。 (附注:这是Automator测试代码) - DearDhruv

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接