Search code examples
androidautomationandroidviewclient

Couldn't distinguish NumberPicker views by AndroidViewClient


In the application I'm trying to automate with AndroidViewClient there are widgets implemented with NumberPicker. Using AndroidViewClient / culebra I can't distinguish the values / texts presented on those NumberPickers.


Solution

  • Due to how NumberPicker is implemented it does not appear in the dump showing its values. There may be other solutions but the one that comes first to my mind is to create a wrapper and set the content description as the value (this is done by some other Views too).

    package net.simonvt.numberpicker.samples;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.widget.FrameLayout;
    
    import net.simonvt.numberpicker.NumberPicker;
    import net.simonvt.numberpicker.NumberPicker.OnValueChangeListener;
    
    /**
     * @author diego
     */
    public class NumberPickerHolder extends FrameLayout {
    
        protected static final String TAG = "NumberPickerHolder";
    
        private NumberPicker mNumberPicker;
    
        final OnValueChangeListener mOnValueChangedListener = new OnValueChangeListener() {
    
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                final String cd = Integer.toString(newVal);
                Log.d(TAG, "picker=" + picker + " content description set to " + cd);
                setContentDescription(cd);
            }
        };
    
        /**
         * @param context
         */
        public NumberPickerHolder(Context context) {
            super(context);
            init(context);
        }
    
        /**
         * @param context
         * @param attrs
         */
        public NumberPickerHolder(Context context, AttributeSet attrs) {
            super(context, attrs);
            init(context);
        }
    
        /**
         * @param context
         * @param attrs
         * @param defStyle
         */
        public NumberPickerHolder(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init(context);
        }
    
        private void init(Context context) {
            mNumberPicker = new NumberPicker(context);
            mNumberPicker.setOnValueChangedListener(mOnValueChangedListener);
            android.view.ViewGroup.LayoutParams lp = getLayoutParams();
            if (lp == null) {
                lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            }
            addView(mNumberPicker, lp);
        }
    
        public void setMaxValue(int i) {
            mNumberPicker.setMaxValue(i);
        }
    
        public void setMinValue(int i) {
            mNumberPicker.setMinValue(i);
        }
    
    }
    

    Remember to use this new class in your layouts

        <!--   <net.simonvt.numberpicker.NumberPicker -->
        <net.simonvt.numberpicker.samples.NumberPickerHolder
            android:id="@+id/numberPicker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    

    Then, in your culebra script you can get the value obtaining the content description

    # class=android.widget.FrameLayout
    net_simonvt_numberpicker_samples___id_numberPicker = vc.findViewByIdOrRaise("net.simonvt.numberpicker.samples:id/numberPicker")
    print net_simonvt_numberpicker_samples___id_numberPicker.getContentDescription()