【问题标题】:Xamarin.UITest: how to set date on DatePicker component?Xamarin.UITest:如何在 DatePicker 组件上设置日期?
【发布时间】:2021-02-23 21:24:36
【问题描述】:

我想在我的应用上编写一些功能测试。我在 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

【问题讨论】:

    标签: android xamarin testing xamarin.forms emulation


    【解决方案1】:

    首先,添加带有 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");
    }
    }
    

    【讨论】:

    • 我在回答中写了相同的解决方案.. 它不起作用
    • @Corleone 好奇怪,我用上面的代码,效果很好,你能试试我的代码测试一下吗?
    猜你喜欢
    • 2017-10-27
    • 2018-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    • 2011-09-21
    • 1970-01-01
    相关资源
    最近更新 更多