nunit UITest xamarin.forms:如何滚动选择器?
nunit UITest xamarin.forms: how to scroll a picker?
我正在尝试为 Xamarin.Forms 项目创建 UITest。在 Android 上进行测试。
我尝试使用 Xamarin forum 上可用的一些信息,但没有成功。
选择器的国家/地区列表:
List<country> countryList = new List<country>({id="GB", name="United Kingdom"}, {id="US", name="United States"}.... etc);
选择器代码:
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);
}
n单位代码:
app.Query(c=>c.Marked("countryPicker").Invoke("selectValue", "United States"));
答案:对 Marked("countryPicker").Invoke("selectValue", "United States") 的查询给出了 1 个结果。
但是什么也没有发生 - selection 还没有完成!
我已经尝试将 select 值设置为 "US" - 同样,没有结果。
我错过了什么吗?
打开 Xamarin.Form
选择器,Select,& 测试结果(仅限 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
选择器,Select,& 测试结果(仅限 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");
以下解决方案现在也可用,并且当您的选择器的值超出上面设置的 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 上的小 "nudge" - 如果您 select 使用 selectRow
的选择器值,则 UI 不会标记为更新。此 hack 会触发必要的事件并更新 UI。
我正在尝试为 Xamarin.Forms 项目创建 UITest。在 Android 上进行测试。 我尝试使用 Xamarin forum 上可用的一些信息,但没有成功。
选择器的国家/地区列表:
List<country> countryList = new List<country>({id="GB", name="United Kingdom"}, {id="US", name="United States"}.... etc);
选择器代码:
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);
}
n单位代码:
app.Query(c=>c.Marked("countryPicker").Invoke("selectValue", "United States"));
答案:对 Marked("countryPicker").Invoke("selectValue", "United States") 的查询给出了 1 个结果。 但是什么也没有发生 - selection 还没有完成! 我已经尝试将 select 值设置为 "US" - 同样,没有结果。
我错过了什么吗?
打开 Xamarin.Form
选择器,Select,& 测试结果(仅限 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
选择器,Select,& 测试结果(仅限 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");
以下解决方案现在也可用,并且当您的选择器的值超出上面设置的 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 上的小 "nudge" - 如果您 select 使用 selectRow
的选择器值,则 UI 不会标记为更新。此 hack 会触发必要的事件并更新 UI。