Espresso匹配选定的微调文本

时间:2015-12-08 10:37:15

标签: testing android-spinner android-espresso android-instrumentation

the answer here之后,我尝试检查是否选择了某个微调文本。微调器出现在对话框中,所以我尝试了:

onView(withId(R.id.package_spinner)).inRoot(isDialog()).check(matches(withSpinnerText(containsString("sachet"))));

但是这不起作用,我收到以下错误消息:

  

android.support.test.espresso.base.DefaultFailureHandler $ AssertionFailedWithCauseError:   'with text:包含“sachet”的字符串与所选的字符串不匹配   视图。       预期:带文字:包含“香包”的字符串       得到:“AppCompatSpinner {id = 2131624039,res-name = package_spinner,visibility = VISIBLE,width = 620,height = 75,has-focus = false,   has-focusable = true,has-window-focus = true,is-clickable = true,   is-enabled = true,is-focused = false,is-focusable = true,   is-layout-requested = false,is-selected = false,   root-is-layout-requested = false,has-input-connection = false,x = 0.0,   y = 75.0,child-count = 1}“

“is-selected = false”是否意味着它找不到的微调器?这是在使用API​​ 18的真实设备(非仿真器)上运行。在Espresso运行期间以及手动测试时,微调器正确设置为“sachet”。为什么Espresso有问题?

不确定这是否相关,但是微调器适用于以下类型的对象:

public class PackageType {
    private int id;
    private String name;

    private final Context ctx;

    public PackageType(Context context) { this.ctx=context; }
    public PackageType(String name, Context context) {
        super();
        this.ctx = context;
        setName(name);
    }

    // setters
    public void setId(int i) { this.id = i; }
    public void setName(String u) {
        this.name = u;
    }


    // getters
    public int getId() { return id; }
    public String getName() { return name; }
}

并且微调器适配器看起来像:

class SpinnerPackageTypeAdapter extends ArrayAdapter<PackageType> {
    private final List<PackageType> packageTypes;
    private final Context mContext;

    public SpinnerPackageTypeAdapter(Context context, int resource, List<PackageType> packageTypes) {
        super(context, resource, packageTypes);
        this.mContext = context;
        this.packageTypes = packageTypes;
    }

    public PackageType getItem(int position) { return packageTypes.get(position); }

    public long getItemId(int position) { return position; }

    // this is for the passive state of the spinner
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // use dynamically created TextView, but could reference custom layout
        TextView label = new TextView(mContext);
        label.setTextColor(Color.BLACK);
        label.setTextSize(mContext.getResources().getDimension(R.dimen.list_row_font_size));
        label.setGravity(Gravity.CENTER);
        label.setText(getItem(position).getName());

        return label;
    }

    // this is for the chooser dropped down spinner
    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        TextView label = (TextView) View.inflate(mContext,R.layout.row_spinner,null);
        label.setText(getItem(position).getName());
        return label;
    }

}

1 个答案:

答案 0 :(得分:0)

我发现了问题。在类PackageType中,我必须覆盖toString()方法(当然,只要“name”在其中,其他版本就可以):

@Override
    public String toString() {
        return "PackageType [id=" + id + ", name=" + name + "]";
    }