14

UPDATE

This project is no longer maintained today, but solutions are most welcomed. Thanks.


AutoCompleteTextView adapter issue for selecting item

I am creating an automation test project in which I have issue to select the item from the AutoCompleteTextView.

You can see the snap and the views[all expanded] it has. The dropdown of the AutoCompleteTextView does not appear in view tree nor I am able to select using mouse.

I have tried below approaches to select the item form from the AutoCompleteTextView adapter:

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

  2. UiScrollable locationList = new UiScrollable(locationEditText.getSelector()); locationList.scrollTextIntoView(location); Here locationEditText is my AutoCompleteTextView

  3. UiObject selectedLocation = locationList.getChild(new UiSelector().text(location)); selectedLocation.click(); from the locationList it does not select the item with the string passed.

  4. editLocationResId = "android:id/text1"; UiObject selectedLocation = new UiObject(new UiSelector().resourceId(editLocationResId)); selectedLocation.click(); The id from the adpter textview does not work either.

Can anybody help me with selecting the item from the AutoCompleteTextView in uiautomator? Or the more approaches get the desire output.

DearDhruv
  • 778
  • 13
  • 34
  • As I seen many question related to it, But still not reliable solutions! :( here is the link: https://groups.google.com/forum/#!msg/appium-discuss/0XAST8Bs3hw/zW2yRns5ia0J the question has been posted a long ago. In addition the approach to get this done is to click the co-ordinates. Again still differs for different devices. – DearDhruv Aug 11 '14 at 06:48
  • I have tested UIAutomator tests in many devices and found that only some of the devices are providing the accessibility in order to perform automation. Nexus devices provides better accessibility than Samsung/Sony/HTC devices where (Micromax)Canvas devices does not give it at all (very minor). – DearDhruv Oct 07 '14 at 06:20

7 Answers7

0

Get the cordinates of text field :

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

Add values to get the right cordinates.

x = x+x1
y = y+y1

Use tap function to click on it.

TouchAction action = new TouchAction(driver);

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

shakun tyagi
  • 107
  • 7
0

first open the pick-up list, then try to use UiDevice.pressKey(), to send a DPAD_DOWN OR UP event and press Enter to select the item you want.

Ryan_D
  • 11
  • 1
0

If you need to press the first element from the dropdown list, I have found a useful workaround:

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

Given an AutoCompleteTextView declared as autoCompleteTextView, you can get the current location on the screen and calculate the item which is gonna be located below it. Hope it helps!

0

Well, i found a turn around(this is for autocompletetextview, for spinner delete 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);

Get object, click it, insert text that you want,wait for dropdown to appear then click under it with some pixels

Dragos Rachieru
  • 602
  • 8
  • 21
0

I had created and have been using the simplest and the most efficient way. Just locate, type and then select the desired value in autocomplete text view.
Hope it will work for you as well.

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);
Federico Grandi
  • 6,785
  • 5
  • 30
  • 50
0

You can find the element that you want to select using its text. Make sure that this line of code is executed after the suggestion list has been shown.

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

Hope it helps.

pglez82
  • 489
  • 5
  • 11
-1

try the code below it worked for me.

I am using the the AutoCompleteText to auto complete the location where the user is currently in, the locationList is nothing but an array that i wrote in the strings.xml file, so use your own string array here.

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

and below is the code for validating the text input in the location field, the fixText() method prevent the user from typing text that does not exist in your string array, for instance: if the user type "germany" which does not exist in your string array list, it will be replaced by " " which is an empty string inside your edittext input field

 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();
            }
        }
    }
Amadeus
  • 7
  • 3
  • 2
    Thanks for the effort. But please do read Question carefully and then try to make comment or Answer. You can ask if you do not understand the Question. [(P.S) It is Automator test code] – DearDhruv Mar 10 '15 at 07:25