Skip to content

Commit fe4344f

Browse files
ki4070mamykola-mokhnach
authored andcommitted
Added format to device_time as argument (appium#312)
1 parent a40a933 commit fe4344f

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

appium/webdriver/mobilecommand.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ class MobileCommand(object):
6767
UPDATE_SETTINGS = 'updateSettings'
6868
SET_LOCATION = 'setLocation'
6969
GET_LOCATION = 'getLocation'
70-
GET_DEVICE_TIME = 'getDeviceTime'
70+
GET_DEVICE_TIME_GET = 'getDeviceTimeGet'
71+
GET_DEVICE_TIME_POST = 'getDeviceTimePost'
7172
CLEAR = 'clear'
7273
START_RECORDING_SCREEN = 'startRecordingScreen'
7374
STOP_RECORDING_SCREEN = 'stopRecordingScreen'

appium/webdriver/webdriver.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -523,9 +523,26 @@ def update_settings(self, settings):
523523

524524
@property
525525
def device_time(self):
526-
"""Returns the date and time from the device
526+
"""Returns the date and time from the device.
527527
"""
528-
return self.execute(Command.GET_DEVICE_TIME, {})['value']
528+
return self.execute(Command.GET_DEVICE_TIME_GET, {})['value']
529+
530+
def get_device_time(self, format=None):
531+
"""Returns the date and time from the device. (Only available since Appium 1.11.0)
532+
533+
:Args:
534+
- format - (optional) The set of format specifiers. Read https://momentjs.com/docs/
535+
to get the full list of supported datetime format specifiers.
536+
If unset, return :func:`.device_time` as default format is `YYYY-MM-DDTHH:mm:ssZ`,
537+
which complies to ISO-8601
538+
539+
:Usage:
540+
self.driver.get_device_time()
541+
self.driver.get_device_time("YYYY-MM-DD")
542+
"""
543+
if format is None:
544+
return self.device_time
545+
return self.execute(Command.GET_DEVICE_TIME_POST, {'format': format})['value']
529546

530547
@property
531548
def battery_info(self):
@@ -584,7 +601,9 @@ def _addCommands(self):
584601
('POST', '/session/$sessionId/appium/settings')
585602
self.command_executor._commands[Command.LOCATION_IN_VIEW] = \
586603
('GET', '/session/$sessionId/element/$id/location_in_view')
587-
self.command_executor._commands[Command.GET_DEVICE_TIME] = \
604+
self.command_executor._commands[Command.GET_DEVICE_TIME_GET] = \
588605
('GET', '/session/$sessionId/appium/device/system_time')
606+
self.command_executor._commands[Command.GET_DEVICE_TIME_POST] = \
607+
('POST', '/session/$sessionId/appium/device/system_time')
589608
self.command_executor._commands[Command.CLEAR] = \
590609
('POST', '/session/$sessionId/element/$id/clear')

test/unit/webdriver/device/device_time_test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from test.unit.helper.test_helper import appium_command, android_w3c_driver
1616

1717
import httpretty
18+
import json
1819

1920

2021
class TestWebDriverDeviceLock(object):
@@ -28,3 +29,26 @@ def test_device_time(self):
2829
body='{"value": "2019-01-05T14:46:44+09:00"}'
2930
)
3031
assert driver.device_time == '2019-01-05T14:46:44+09:00'
32+
33+
@httpretty.activate
34+
def test_get_device_time(self):
35+
driver = android_w3c_driver()
36+
httpretty.register_uri(
37+
httpretty.GET,
38+
appium_command('/session/1234567890/appium/device/system_time'),
39+
body='{"value": "2019-01-05T14:46:44+09:00"}'
40+
)
41+
assert driver.get_device_time() == '2019-01-05T14:46:44+09:00'
42+
43+
@httpretty.activate
44+
def test_get_formatted_device_time(self):
45+
driver = android_w3c_driver()
46+
httpretty.register_uri(
47+
httpretty.POST,
48+
appium_command('/session/1234567890/appium/device/system_time'),
49+
body='{"value": "2019-01-08"}'
50+
)
51+
assert driver.get_device_time('YYYY-MM-DD') == '2019-01-08'
52+
53+
d = json.loads(httpretty.last_request().body.decode('utf-8'))
54+
assert d['format'] == 'YYYY-MM-DD'

0 commit comments

Comments
 (0)