gpt4 book ai didi

android-espresso - 使用 espresso 处理由 AutoCompleteTextView 填充的选项

转载 作者:行者123 更新时间:2023-12-02 17:12:47 33 4
gpt4 key购买 nike

我正在尝试为 Android 应用程序编写 Espresso 测试,并且需要知道如何自动化使用 AutoCompleteTextView 的部分。下面提到了我的应用程序使用的代码:

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:text="Name a fruit from (Apple Banana Cherry Date Grape Kiwi Mango Pear)" />

<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView"
android:layout_marginLeft="36dp"
android:layout_marginTop="17dp"
android:ems="10"
android:text="">

<requestFocus />
</AutoCompleteTextView>

MainActivity.java

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class MainActivity extends Activity {
String[] fruits = {"Apple", "Apples2", "Apples3", "Date", "Grape", "Kiwi", "Mango", "Pear"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//Creating the instance of ArrayAdapter containing list of fruit names
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this, android.R.layout.select_dialog_item, fruits);
//Getting the instance of AutoCompleteTextView
AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
actv.setThreshold(1);//will start working from first character
actv.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
actv.setTextColor(Color.RED);

}
}

我尝试了一些解决方案,但都没有成功。我能够使用以下代码获取计数

public class AutoCompleteTextViewItemCountAssertion implements ViewAssertion {
private final int expectedCount;

public AutoCompleteTextViewItemCountAssertion(int expectedCount) {
this.expectedCount = expectedCount;
}

@Override
public void check(View view, NoMatchingViewException noViewFoundException) {
if (noViewFoundException != null) {
throw noViewFoundException;
}
AutoCompleteTextView tv = (AutoCompleteTextView) view;
assertThat(tv.getAdapter().getCount(),is(expectedCount));
}
}

但是,我希望能够单击特定选项并在不同位置获取选项文本

也尝试了以下解决方案,但没有帮助:

onData(equalTo("ITEM")).inRoot(RootMatchers.isPlatformPopup()).perform(click());
onData(instanceOf("Whatever your arrayadapter contains".class)).inRoot(RootMatchers.withDecorView(not(is(mActivityRule.getActivity().getWindow().getDecorView())))).perform(ViewActions.click());
onData(anything()).atPosition(1).perform(click());

最佳答案

我的情况类似:我有一个带有单个 AutoCompleteTextView 的自定义对话框:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_dialog_layout_design_user_input"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/userInputDialogAutoComplete"
android:inputType="textMultiLine|textVisiblePassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

我的解决方案(found here):

   onView(withText(containsString("Mister Dummy")))
.inRoot(isPlatformPopup())
.perform(click());

问候

关于android-espresso - 使用 espresso 处理由 AutoCompleteTextView 填充的选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48202240/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com