我想在我的应用程序上写一些功能测试。我有一个简单的窗口上的DatePicker组件:
<DatePicker AutomationId="PlanWyborDaty" Date="{Binding Data, Mode=TwoWay}" Format="dddd, dd-MM-yyyy" Margin="8, 0" DateSelected="OnDateChange" />
我在我的测试方法中尝试了这些代码:
app.Query(x => x.Marked("PlanWyborDaty").Invoke("updateDate", 2020, 9, 2));
app.Query(x => x.Class("DatePicker").Invoke("updateDate", 2020, 9, 2));
,但日期在选择器仍然是相同的。我找了很多,只找到了这些方法。什么好主意吗?
模拟器是Pixel 2 API 28 Android 9.0
首先,使用AutomationId=" DatePicker "添加DatePicker控件
<DatePicker AutomationId="datepicker" />
然后在UItest.cs中使用以下代码:
[Test]
public void DatePickerTest()
{
app.Tap(c => c.Marked("datepicker"));
SetDatePicker(new DateTime(2018, 1, 1));
}
public void SetDatePicker(DateTime date)
{
// needed to tap & thus select the items
string month = DateTimeFormatInfo.CurrentInfo.GetMonthName(date.Month);
string day = date.Day.ToString();
string year = date.Year.ToString();
if (platform == Platform.iOS)
{
//Invoke the native method selectRow()
app.Query(x => x.Class("UIPickerView").Invoke("selectRow", date.Month, "inComponent", 0, "animated", true)); // brings month in scope
app.Tap(month); // actually selects month
app.Query(x => x.Class("UIPickerView").Invoke("selectRow", date.Day, "inComponent", 1, "animated", true));
app.Tap(day);
app.Query(x => x.Class("UIPickerView").Invoke("selectRow", date.Year, "inComponent", 2, "animated", true));
app.Tap(year);
} else // Android
{
app.Query(x => x.Class("DatePicker").Invoke("updateDate", date.Year, date.Month, date.Day));
app.Tap("OK");
}
}