Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
331 views
in Technique[技术] by (71.8m points)

c# - Is there a way to set the time format in xamarin.uitest?

I'm testing a time picker and I want to test one time the AM,PM format and one time the 24 hours format, is there a way to set android date format via the xamarin.uitest project ? this is how you do it manually


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...