[forms-devel] How to to do UITest for Xamarin.Forms Picker

Jonathan Peppers Jonathan.Peppers at Hitcents.com
Fri Sep 9 13:59:46 UTC 2016

Hi Jesse,

We have some UITests using Picker.

And we just use:

app.Tap(x => x.Marked(“Text”));

Where “Text” is what you see in the picker.

This works for our app, but we may only have 4 items in the picker. I would recommend using app.Repl() to see if you can query it/or control it from there.

From: forms-devel [mailto:forms-devel-bounces at lists.dot.net] On Behalf Of Jesse Jiang
Sent: Friday, September 9, 2016 4:06 AM
To: forms-devel at lists.dot.net
Subject: [forms-devel] How to to do UITest for Xamarin.Forms Picker


We were blocked many days for setting the UITest for Xamarin.Forms Picker. We googled a lot and find a way to change the Picker’s index like

            app.Tap(c => c.Marked("LoginOption")); // Picker’s ID
            app.WaitForElement(c => c.Button("Done"));
            var items = app.Query(c => c.Class("UIPickerView").Invoke("selectRow", 2, "inComponent", 0, "animated", true));

                app.Tap(c => c.Button("Done"));
It can change the selected item for UIPickerView. But the problem is when I called the last line click Done button, the Picker select the first item every time.

I looked into the source code and found these code in PickerRenderer
                                                            var doneButton = new UIBarButtonItem(UIBarButtonSystemItem.Done, (o, a) =>
                                                                        var s = (PickerSource)_picker.Model;
                                                                        if (s.SelectedIndex == -1 && Element.Items != null && Element.Items.Count > 0)
That’s the reason why alway select the first item.

So we called selectRow function in UIPickerView, but it doesn’t invoke the function in UIPickerViewModel
                                    public override void Selected(UIPickerView picker, nint row, nint component)
                                                if (_renderer.Element.Items.Count == 0)
                                                            SelectedItem = null;
                                                            SelectedIndex = -1;
                                                            SelectedItem = _renderer.Element.Items[(int)row];
                                                            SelectedIndex = (int)row;

Finally, is there a way to change the Picker Selected Item by index in UITest?

Jesse Jiang | Senior Developer
XAM Consulting - Mobile Technology Specialists

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.dot.net/pipermail/forms-devel/attachments/20160909/6a75b6ca/attachment-0001.html>

More information about the forms-devel mailing list