如何解决Xamarin.UITest:如何在 DatePicker 组件上设置日期?
我想为我的应用编写一些功能测试。我在 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",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","animated",true)); // brings month in scope
app.Tap(month); // actually selects month
app.Query(x => x.Class("UIPickerView").Invoke("selectRow",date.Day,true));
app.Tap(day);
app.Query(x => x.Class("UIPickerView").Invoke("selectRow",date.Year,2,true));
app.Tap(year);
} else // Android
{
app.Query(x => x.Class("DatePicker").Invoke("updateDate",date.Day));
app.Tap("OK");
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。