Set Date and Time on device

Is there a way to set the time and date on the test device before running a test?
I would like to get a method on Xamarin.UITest.IDevice similar to setLocation (something like setTime(DateTime time) )

Posts

  • +1 in my case i need to get the time, not set it, which i presume is a bit problematic

  • Kenrick95Kenrick95 SGMember
    edited July 2016

    You can get the current time using DateTime.Now; but it only works when the device has the time set to the test server's time (if the device time is different with server time, DateTime.Now will return the test server's time, not the device's time)

    Yeah, I'd like to see this feature too (get/set device time)

    My current workaround for getting the device time is to use backdoor:

    In Tests

    var now = DateTime.Now;
    string dateString = "";
    if (_Platform == Platform.Android)
        dateString = (string)_App.Invoke("GetDeviceTime");
    else if (_Platform == Platform.iOS)
        dateString = (string)_App.Invoke("GetDeviceTime:", "Dummy");
    now = DateTime.ParseExact(dateString, "yyyyMMddHHmmss", null);
    

    In Droid, MainActivity.cs,

    [Export("GetDeviceTime")]
    public string GetDeviceTime()
    {
        return System.DateTime.Now.ToString("yyyyMMddHHmmss");
    }
    

    In iOS, AppDelegate.cs,

    [Export("GetDeviceTime:")]
    public NSString GetDeviceTime(NSString value)
    {
        return new NSString(System.DateTime.Now.ToString("yyyyMMddHHmmss"));
    }
    
  • Ben.2975Ben.2975 USMember

    I've raised a feature request for being able to change the time - feel free to add a vote:
    https://testcloud.ideas.aha.io/ideas/XTA-I-101

  • To set/get the time zone, try first to get a reference to SharpAdbClient package in your project. Once you have it you can get the connected device and execute some shell commands on it. In your case try this :

    DeviceData connectedDevice = AdbClient.Instance.GetDevices()[0];
    // set timezone (works only for rooted devices)
    connectedDevice.ExecuteShellCommand("adb shell setprop persist.sys.timezone America/Chicago", null);
    // get timezone
    connectedDevice.ExecuteShellCommand("adb shell getprop persist.sys.timezone", null);

    For my case i am trying to set time zone for unrooted devices. Any idea of how to do that ?

    Thank you :)

  • @HoussemOuertani said:
    To set/get the timezone / datetime, try first to get a reference to SharpAdbClient package in your project. Once you have it you can get the connected device and execute some shell commands on it. In your case try this :

    DeviceData connectedDevice = AdbClient.Instance.GetDevices()[0];
    // set datetime (works only for rooted devices)
    connectedDevice.ExecuteShellCommand("adb shell date -s "YYYYMMDD.HHmmss"", null);
    // set timezone (works only for rooted devices)
    connectedDevice.ExecuteShellCommand("adb shell setprop persist.sys.timezone America/Chicago", null);
    // get timezone
    connectedDevice.ExecuteShellCommand("adb shell getprop persist.sys.timezone", null);

    For my case i am trying to set time zone for unrooted devices. Any idea of how to do that ?

    Thank you :)

Sign In or Register to comment.