Xamrin forms, Set Picker Value in UI Test

Anurag.9113Anurag.9113 USMember ✭✭

I'm trying to set UIPickerView value using UITest and it does not looks like AppQuery can figure out the element to select.

Is there a solution for this?
I have been using this code, courtesy StackOverflow, but it does not seem to solve the problem.
Any help is appreciated.

public static bool SetPicker (this IApp app, int index, string name)
{
if (string.IsNullOrEmpty (name)) {
throw new ArgumentNullException (nameof (name));
}

        if (Device.OS == TargetPlatform.Android) {
            var results = app.Query (c => c.Class ("numberPicker").Invoke ("setValue", index));
            app.Tap (c => c.Marked ("OK"));
            return results.Any ();
        }                 /// Assuming a single column picker            
        // Try to scroll and find the text item only 5 times
        for (int i = 0; i < 5; i++) {
            if (app.Query (m => m.Marked (name)).Length == 0) {
                app.ScrollUp (x => x.Class ("UIPickerTableView"), ScrollStrategy.Programmatically);
                Debug.WriteLine (i);
            } else {
                app.ScrollDown (name);
                app.Tap (x => x.Marked (name));
                break;
            }
        }
        return true;
    }
Tagged:

Posts

  • VictorChelaruVictorChelaru USMember ✭✭

    This seems to be a deficiency in the Xamarin UI test (common problem, no built-in solution). Here's the best I've found:

    https://forums.xamarin.com/discussion/30339/controlling-a-picker-android-numberpicker-ios-uipickerview-from-uitest

  • AndrewCooper.0869AndrewCooper.0869 USUniversity

    Here is an extension method I came up with for setting a simple iOS UIPickerView:

        public static void SetIosPickerValue(this IApp app, string pickerValue)
        {
            string previousFirstValue = null;
            string currentFirstValue = app.Query(x => x.Class("UIPickerTableViewWrapperCell").
                                         Child("UIView").
                                         Child("UILabel")).
                                  Select(x => x.Text).FirstOrDefault();
    
            // first, scroll to the top of the list
            while (currentFirstValue != previousFirstValue)
            {
                app.ScrollUp(x => x.Class("UIPickerTableView"));
                previousFirstValue = currentFirstValue;
                currentFirstValue = app.Query(x => x.Class("UIPickerTableViewWrapperCell").
                                             Child("UIView").
                                             Child("UILabel")).
                                      Select(x => x.Text).FirstOrDefault();
            }
    
            string previousLastValue = null;
            string currentLastValue = app.Query(x => x.Class("UIPickerTableViewWrapperCell").
                                         Child("UIView").
                                         Child("UILabel")).
                                  Select(x => x.Text).FirstOrDefault();
    
            // now, scroll until we find the element
            while (currentLastValue != previousLastValue)
            {
                if (app.Query(m => m.Text(pickerValue)).Length == 0)
                {
                    app.ScrollDown(x => x.Class("UIPickerTableView").Index(0));
                }
                else
                {
                    app.Tap(x => x.Text(pickerValue));
                    break;
                }
                previousLastValue = currentLastValue;
                currentLastValue = app.Query(x => x.Class("UIPickerTableViewWrapperCell").
                                             Child("UIView").
                                             Child("UILabel")).
                                  Select(x => x.Text).LastOrDefault();
            }
        }
    
  • pranartdechpokketpranartdechpokket THMember ✭✭

    I use library named "FormsTest" with Xamarin.UITest (https://github.com/pranart/FormsTest)
    This library will let you get almost every property value from Xamarin.Forms Controls.

Sign In or Register to comment.