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

Jesse Jiang jesse at xam-consulting.com
Fri Sep 9 09:06:11 UTC 2016


Hello,

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)
							UpdatePickerSelectedIndex(0);
						UpdatePickerFromModel(s);
						entry.ResignFirstResponder();
					});
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;
				}
				else
				{
					SelectedItem = _renderer.Element.Items[(int)row];
					SelectedIndex = (int)row;
				}
				_renderer.UpdatePickerFromModel(this);
			}

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

Thanks
Jesse Jiang | Senior Developer 

XAM Consulting - Mobile Technology Specialists

www.xam-consulting.com <http://www.xam-consulting.com/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.dot.net/pipermail/forms-devel/attachments/20160909/98de4890/attachment.html>


More information about the forms-devel mailing list