Appium 1.6从微调器下拉列表中选择一个选项

时间:2017-05-16 06:06:11

标签: android spinner appium android-spinner

我正在使用不支持findByName()的appium 1.6。当我点击一个微调器时,它会显示一个下拉列表。现在我需要从此下拉列表中选择一个选项。有没有办法做到这一点?

这是下拉列表的代码,显示的选项是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" />

1 个答案:

答案 0 :(得分:0)

这是我正在做的代码示例:

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

和要互动的代码:

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

其中“tapElement()”只是自定义点按功能,您可以选择任何您想要点击的功能(在Appium中至少有4种方法可以执行此操作:-))