我正在尝试为Xamarin.Forms项目创建UITest。在Android上进行测试。 我尝试使用Xamarin forum上提供的一些inf,但没有运气。
Picker的国家/地区列表:
List<country> countryList = new List<country>({id="GB", name="United Kingdom"}, {id="US", name="United States"}.... etc);
Picker代码:
var countryPicker = new Picker();
countryPicker.SetBinding(Picker.SelectedIndexProperty, "myIndex");
countryPicker.BindingContext = getCountry;
countryPicker.BackgroundColor = Color.White;
countryPicker.AutomationId = "countryPicker";
countryPicker.SelectedIndexChanged += ((s, e) =>
{
foreach (var element in countryList)
{
if (element.name.Equals(countryPicker.Items[countryPicker.SelectedIndex]))
{
getCountry.myCountry = element.id;
}
}
});
//adding all the countries to the list
foreach (var item in countryList)
{
countryPicker.Items.Add(item.name);
}
nunit代码:
app.Query(c=>c.Marked("countryPicker").Invoke("selectValue", "United States"));
答案:查询标记(&#34; countryPicker&#34;)。调用(&#34; selectValue&#34;,&#34;美国&#34;)给出1个结果。 但没有任何事情发生 - 选择没有完成! 我试图为&#34; US&#34;选择价值。 - 同样,没有结果。
我错过了什么吗?
答案 0 :(得分:2)
Xamarin.Form
Picker,Select,&amp;测试结果(仅限Android):// Open up the picker
app.Tap(x => x.Marked("countryPicker"));
// Try to scroll and find the text item only 5 times
for (int i = 0; i< 5; i++)
{
if (app.Query(m => m.Text("United States")).Length == 0)
app.ScrollDown(x => x.Id("select_dialog_listview"));
else
{
app.Tap(x => x.Text("United States"));
break;
}
}
Assert.IsTrue(app.Query("countryPicker")[0].Text == "United States");
Xamarin.Form
Picker,Select,&amp;测试结果(仅限iOS):/// Open up the picker
app.Tap(x => x.Marked("countryPicker"));
// Assuming a single column picker
var picker = app.Query(x => x.Class(pickerClass).Index(0));
// Try to scroll and find the text item only 5 times
for (int i = 0; i < 5; i++)
{
if (app.Query(m => m.Text("United States")).Length == 0)
app.ScrollDown(x => x.Class("UIPickerTableView").Index(0));
else
{
app.Tap(x => x.Text("United States"));
break;
}
}
app.Tap(x => x.Class("UIToolbarTextButton"));
Assert.IsTrue(app.Query("countryPicker")[0].Text == "United States");
答案 1 :(得分:1)
以下解决方案现在也可用,并且当选择器的值超出上面设置的5个滚动限制时,这些解决方案也不会中断。在此示例中,我使用了NumberPicker,但是相同的方法也可以用于CountryPicker:
if (platform == Platform.iOS) {
app.Query(x => x.Class("UIPickerView").Invoke("selectRow", 3, "inComponent", 0, "animated", true));
// This is a hack to move the item so that Xamarin Forms will generate the event to update the UI
var rect = app.Query(x => x.Class("UIPickerTableView").Index(0).Child()).First().Rect;
app.DragCoordinates(rect.CenterX, rect.CenterY, rect.CenterX, rect.CenterY + 20);
app.Tap(c => c.Text("Done"));
}
else {
//NOTE: On Android the values appear to be out by one - setting 4 selects the 3rd value.
var results = app.Query(c => c.Class("numberPicker").Invoke("setValue", 4));
var text = app.Query("PickerControl")[0].Text;
app.Tap(c => c.Text(text));
//You can also do it like this on Android:
// app.ScrollDownTo(m => m.Text("White"), x => x.Id("select_dialog_listview"));
// app.Tap(x => x.Text("White"));
// Assert.IsTrue(app.Query("PickerControl")[0].Text == "White");
}
您可以在打开选择器后使用这些解决方案。
特别值得注意的是iOS上的小“微调”-如果您使用selectRow
选择选择器值,则UI不会标记为已更新。这种黑客行为会触发必要的事件,并更新用户界面。