Aditya Mukesh
Aditya Mukesh

Reputation: 92

Appium 1.6 select an option from a spinner drop-down

I am using appium 1.6 which does not support findByName(). When I click on a spinner it shows a dropdown. Now I need to select one of the option from this dropdown. Is there any way to do this?

This is the code for the dropdown and the options shown which are editText.

<com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner
            android:id="@+id/warehouse_spinner__add_product"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/warehouse_spinner_prompt"
            android:prompt="@string/warehouse_spinner_prompt"
            app:met_floatingLabel="normal" />

        <com.rengwuxian.materialedittext.MaterialEditText
            android:id="@+id/product_quantity_edittext__add_product"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/quantity"
            android:inputType="numberDecimal"
            app:met_floatingLabel="highlight" />

Upvotes: 0

Views: 1356

Answers (1)

Aleksei
Aleksei

Reputation: 36

here is example of code i am doing:

@HowToUseLocators(androidAutomation = LocatorGroupStrategy.CHAIN)
@AndroidFindBy(id = "spinnerCountry")
@AndroidFindBy(className = "android.widget.TextView")
private AndroidElement spinnerCountryText;
@AndroidFindBy(id = "spinnerCountry")
private AndroidElement spinnerCountry;

and code to interact:

public boolean setCountry(String text) {
        System.out.println("   setCountry(): - " + text);
        if (testStringsEquals(getCountry(), text))
            return true;
        if (tapElement(spinnerCountry)) {
            try {
                return tapElement(driver.findElement(MobileBy
                        .AndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView("
                                + "new UiSelector().text(\"" + text + "\"));")));
            } catch (Exception e) {
                System.out.println("   setCountry(): country NOT found in list");
            }
        } else {
            System.out.println("   setCountry(): country input NOT found");
        }

        return false;
    }

public String getCountry() {
    try {
        return spinnerCountryText.getText();
    } catch (Exception e) {
        return null;
    } }

where "tapElement()" is just custom tap function you can choose any you know to tap (at least 4 ways to do this in Appium :-) )

Upvotes: 0

Related Questions