Skip to content

Commit b75e067

Browse files
author
Mykola Mokhnach
authored
Add methods for start/stop screen record API endpoints (#201)
* Add methods for start/stop screen record API endpoints * Fix typo * Add a separate test for Android and get rid of redundant stuff * Tune documentation * Add videoSize arg description * Fix arg name
1 parent f37733d commit b75e067

File tree

4 files changed

+99
-3
lines changed

4 files changed

+99
-3
lines changed

appium/webdriver/mobilecommand.py

+2
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,5 @@ class MobileCommand(object):
6363
SET_LOCATION = 'setLocation'
6464
GET_DEVICE_TIME = 'getDeviceTime'
6565
CLEAR = 'clear'
66+
START_RECORDING_SCREEN = 'startRecordingScreen'
67+
STOP_RECORDING_SCREEN = 'stopRecordingScreen'

appium/webdriver/webdriver.py

+85-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from appium.webdriver.common.multi_action import MultiAction
2525

2626
from selenium.webdriver.common.by import By
27-
from selenium.webdriver.remote.webelement import WebElement
2827
from selenium.webdriver.support.ui import WebDriverWait
2928
from selenium.common.exceptions import TimeoutException, InvalidArgumentException
3029

@@ -968,6 +967,87 @@ def set_location(self, latitude, longitude, altitude):
968967
self.execute(Command.SET_LOCATION, data)
969968
return self
970969

970+
def start_recording_screen(self, **options):
971+
"""
972+
Start asynchronous screen recording process.
973+
974+
:param options: The following options are supported:
975+
- remotePath: The remotePath upload option is the path to the remote location,
976+
where the resulting video from the previous screen recording should be uploaded.
977+
The following protocols are supported: http/https (multipart), ftp.
978+
Missing value (the default setting) means the content of the resulting
979+
file should be encoded as Base64 and passed as the endpoint response value, but
980+
an exception will be thrown if the generated media file is too big to
981+
fit into the available process memory.
982+
This option only has an effect if there is/was an active screen recording session
983+
and forced restart is not enabled (the default setting).
984+
- user: The name of the user for the remote authentication.
985+
Only has an effect if both `remotePath` and `password` are set.
986+
- password: The password for the remote authentication.
987+
Only has an effect if both `remotePath` and `user` are set.
988+
- method: The HTTP method name ('PUT'/'POST'). PUT method is used by default.
989+
Only has an effect if `remotePath` is set.
990+
- timeLimit: The actual time limit of the recorded video in seconds.
991+
The default value for both iOS and Android is 180 seconds (3 minutes).
992+
The maximum value for Android is 3 minutes.
993+
The maximum value for iOS is 10 minutes.
994+
- forcedRestart: Whether to ignore the result of previous capture and start a new recording
995+
immediately (`True` value). By default (`False`) the endpoint will try to catch and return the result of
996+
the previous capture if it's still available.
997+
998+
iOS Specific:
999+
- videoQuality: The video encoding quality: 'low', 'medium', 'high', 'photo'. Defaults to 'medium'.
1000+
Only works for real devices.
1001+
- videoType: The format of the screen capture to be recorded.
1002+
Available formats: 'h264', 'mp4' or 'fmp4'. Default is 'mp4'.
1003+
Only works for Simulator.
1004+
1005+
Android Specific:
1006+
- videoSize: The video size of the generated media file. The format is WIDTHxHEIGHT.
1007+
The default value is the device's native display resolution (if supported),
1008+
1280x720 if not. For best results, use a size supported by your device's
1009+
Advanced Video Coding (AVC) encoder.
1010+
- bitRate: The video bit rate for the video, in megabits per second.
1011+
The default value is 4. You can increase the bit rate to improve video quality,
1012+
but doing so results in larger movie files.
1013+
1014+
:return: Base-64 encoded content of the recorded media file or an empty string
1015+
if the file has been successfully uploaded to a remote location
1016+
(depends on the actual `remotePath` value).
1017+
"""
1018+
if 'password' in options:
1019+
options['pass'] = options['password']
1020+
del options['password']
1021+
return self.execute(Command.START_RECORDING_SCREEN, options)['value']
1022+
1023+
def stop_recording_screen(self, **options):
1024+
"""
1025+
Gather the output from the previously started screen recording to a media file.
1026+
1027+
:param options: The following options are supported:
1028+
- remotePath: The remotePath upload option is the path to the remote location,
1029+
where the resulting video should be uploaded.
1030+
The following protocols are supported: http/https (multipart), ftp.
1031+
Missing value (the default setting) means the content of the resulting
1032+
file should be encoded as Base64 and passed as the endpoint response value, but
1033+
an exception will be thrown if the generated media file is too big to
1034+
fit into the available process memory.
1035+
- user: The name of the user for the remote authentication.
1036+
Only has an effect if both `remotePath` and `password` are set.
1037+
- password: The password for the remote authentication.
1038+
Only has an effect if both `remotePath` and `user` are set.
1039+
- method: The HTTP method name ('PUT'/'POST'). PUT method is used by default.
1040+
Only has an effect if `remotePath` is set.
1041+
1042+
:return: Base-64 encoded content of the recorded media file or an empty string
1043+
if the file has been successfully uploaded to a remote location
1044+
(depends on the actual `remotePath` value).
1045+
"""
1046+
if 'password' in options:
1047+
options['pass'] = options['password']
1048+
del options['password']
1049+
return self.execute(Command.STOP_RECORDING_SCREEN, options)['value']
1050+
9711051
@property
9721052
def device_time(self):
9731053
"""Returns the date and time from the device
@@ -1070,3 +1150,7 @@ def _addCommands(self):
10701150
('GET', '/session/$sessionId/appium/device/system_time')
10711151
self.command_executor._commands[Command.CLEAR] = \
10721152
('POST', '/session/$sessionId/element/$id/clear')
1153+
self.command_executor._commands[Command.START_RECORDING_SCREEN] = \
1154+
('POST', '/session/$sessionId/appium/start_recording_screen')
1155+
self.command_executor._commands[Command.STOP_RECORDING_SCREEN] = \
1156+
('POST', '/session/$sessionId/appium/stop_recording_screen')

test/functional/android/appium_tests.py

+6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ def tearDown(self):
4343
if hasattr(self, 'zipfilename') and os.path.isfile(self.zipfilename):
4444
os.remove(self.zipfilename)
4545

46+
def test_screen_record(self):
47+
self.driver.start_recording_screen(timeLimit=10, forcedRestart=True)
48+
sleep(5)
49+
result = self.driver.stop_recording_screen()
50+
self.assertTrue(len(result) > 0)
51+
4652
def test_lock(self):
4753
self.driver.lock(-1)
4854
try:

test/functional/ios/appium_tests.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
import unittest
1616
from time import sleep
1717

18-
from selenium.common.exceptions import NoSuchElementException
19-
2018
from appium import webdriver
2119
import desired_capabilities
2220

@@ -37,6 +35,12 @@ def test_lock(self):
3735
self.driver.unlock()
3836
self.assertFalse(self.driver.is_locked())
3937

38+
def test_screen_record(self):
39+
self.driver.start_recording_screen()
40+
sleep(5)
41+
result = self.driver.stop_recording_screen()
42+
self.assertTrue(len(result) > 0)
43+
4044
def test_shake(self):
4145
# what can we assert about this?
4246
self.driver.shake()

0 commit comments

Comments
 (0)