【问题标题】:Espresso match selected spinner textEspresso 匹配选定的微调器文本
【发布时间】:2015-12-08 10:37:14
【问题描述】:

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: a string contains "sachet"' 与所选内容不匹配 看法。 预期:带有文本:包含“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;
    }

}

【问题讨论】:

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


    【解决方案1】:

    我发现了问题。在 PackageType 类中,我必须重写 toString() 方法(当然,只要有“名称”,其他版本也是可能的):

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

    【讨论】:

      猜你喜欢
      • 2015-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-12
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      相关资源
      最近更新 更多