You could try the code below.
[Test]
public void timePickerTest()
{
app.Tap("TimePickerElement");
SetTimePicker(1, 16, false);
app.Screenshot("new time set");
app.Back();
}
public void SetTimePicker(int hour, int minute, bool am)
{
string hourString = hour.ToString();
string minuteString = minute.ToString();
DateTime date = DateTime.Now;
DateTime time = new DateTime(date.Year, date.Month, date.Day, hour, minute, 0);
int ampm;
if (am)
{
ampm = 0;
}
else ampm = 1;
if (platform == Platform.iOS)
{
app.Query(x => x.Class("UIPickerView").Invoke("selectRow", time.Hour, "inComponent", 0, "animated", true)); //if time.Hour == 0, than hour is '1'. if time.Hour == 11, than hour is '12'
app.Query(x => x.Class("UIPickerView").Invoke("selectRow", time.Minute, "inComponent", 1, "animated", true)); //if time.Minute == 0, than minutes is '1'. if time.Minute == 59, than minutes is '59'
app.Query(x => x.Class("UIPickerView").Invoke("selectRow", ampm, "inComponent", 2, "animated", true)); //0 == AM and 1 == PM
}
else // Android
{
// switch to Text entry for time
app.Tap("toggle_mode");
// hour
app.ClearText();
app.EnterText(hourString);
// minute
app.ClearText("input_minute");
app.EnterText(minuteString);
// AM/PM
if (!am)
{
app.Tap("AM"); // opens spinner
app.Tap("PM"); // changes to PM
}
app.Tap("OK"); // finalize time
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…