Skip to content

Commit a1fdf10

Browse files
author
Mykola Mokhnach
committed
Add endpoints for lock/unlock
1 parent 81b48f2 commit a1fdf10

5 files changed

Lines changed: 42 additions & 15 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ dist
1717

1818
.cache
1919
__pycache__
20+
.idea

appium/webdriver/mobilecommand.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ class MobileCommand(object):
4949
CLOSE_APP = 'closeApp'
5050
END_TEST_COVERAGE = 'endTestCoverage'
5151
LOCK = 'lock'
52+
UNLOCK = 'unlock'
53+
IS_LOCKED = 'isLocked'
5254
SHAKE = 'shake'
5355
TOUCH_ID = 'touchId'
5456
TOGGLE_TOUCH_ID_ENROLLMENT = 'toggleTouchIdEnrollment'

appium/webdriver/webdriver.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -640,17 +640,32 @@ def end_test_coverage(self, intent, path):
640640
return self.execute(Command.END_TEST_COVERAGE, data)['value']
641641

642642
def lock(self, seconds):
643-
"""Lock the device for a certain period of time. iOS only.
643+
"""Lock the device. No changes are made if the device is already unlocked.
644644
645645
:Args:
646-
- the duration to lock the device, in seconds
646+
- seconds - the duration to lock the device, in seconds.
647+
The device is going to be locked forever until `unlock` is called
648+
if it equals or is less than zero, otherwise this call blocks until
649+
the timeout expires and unlocks the screen automatically.
647650
"""
648-
data = {
649-
'seconds': seconds,
650-
}
651-
self.execute(Command.LOCK, data)
651+
self.execute(Command.LOCK, {'seconds': seconds})
652652
return self
653653

654+
def unlock(self):
655+
"""Unlock the device. No changes are made if the device
656+
is already locked.
657+
"""
658+
self.execute(Command.UNLOCK)
659+
return self
660+
661+
def is_locked(self):
662+
"""Checks whether the device is locked.
663+
664+
:returns:
665+
Either True or False
666+
"""
667+
return self.execute(Command.IS_LOCKED)['value']
668+
654669
def shake(self):
655670
"""Shake the device.
656671
"""
@@ -850,6 +865,10 @@ def _addCommands(self):
850865
('POST', '/session/$sessionId/appium/app/end_test_coverage')
851866
self.command_executor._commands[Command.LOCK] = \
852867
('POST', '/session/$sessionId/appium/device/lock')
868+
self.command_executor._commands[Command.UNLOCK] = \
869+
('POST', '/session/$sessionId/appium/device/unlock')
870+
self.command_executor._commands[Command.IS_LOCKED] = \
871+
('POST', '/session/$sessionId/appium/device/is_locked')
853872
self.command_executor._commands[Command.SHAKE] = \
854873
('POST', '/session/$sessionId/appium/device/shake')
855874
self.command_executor._commands[Command.TOUCH_ID] = \

test/functional/android/appium_tests.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ def tearDown(self):
4343
if hasattr(self, 'zipfilename') and os.path.isfile(self.zipfilename):
4444
os.remove(self.zipfilename)
4545

46+
def test_lock(self):
47+
self.driver.lock(-1)
48+
try:
49+
self.assertTrue(self.driver.is_locked())
50+
finally:
51+
self.driver.unlock()
52+
self.assertFalse(self.driver.is_locked())
53+
4654
def test_app_strings(self):
4755
strings = self.driver.app_strings()
4856
self.assertEqual(u'You can\'t wipe my data, you are a monkey!', strings[u'monkey_wipe_data'])

test/functional/ios/appium_tests.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,12 @@ def tearDown(self):
3030
self.driver.quit()
3131

3232
def test_lock(self):
33-
el = self.driver.find_element_by_id('ButtonsExplain')
34-
self.assertIsNotNone(el)
35-
self.driver.lock(0)
36-
self.assertRaises(NoSuchElementException, self.driver.find_element_by_id, 'ButtonsExplain')
37-
sleep(10)
38-
39-
# # this does not seem to ever unlock, so the assertion fails
40-
# el = self.driver.find_element_by_id('ButtonsExplain')
41-
# self.assertIsNotNone(el)
33+
self.driver.lock(-1)
34+
try:
35+
self.assertTrue(self.driver.is_locked())
36+
finally:
37+
self.driver.unlock()
38+
self.assertFalse(self.driver.is_locked())
4239

4340
def test_shake(self):
4441
# what can we assert about this?

0 commit comments

Comments
 (0)