Skip to content

Commit 89d62e4

Browse files
author
Mykola Mokhnach
committed
Add endpoints for screen recording API for iOS and Android
1 parent f9313d2 commit 89d62e4

14 files changed

+687
-26
lines changed

src/main/java/io/appium/java_client/MobileCommand.java

+44-24
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import com.google.common.collect.ImmutableMap;
2020

21+
import io.appium.java_client.screenrecording.BaseStartScreenRecordingOptions;
22+
import io.appium.java_client.screenrecording.BaseStopScreenRecordingOptions;
2123
import org.apache.commons.lang3.StringUtils;
2224
import org.openqa.selenium.remote.CommandInfo;
2325
import org.openqa.selenium.remote.http.HttpMethod;
@@ -53,6 +55,8 @@ public class MobileCommand {
5355
protected static final String GET_PERFORMANCE_DATA;
5456
protected static final String GET_SUPPORTED_PERFORMANCE_DATA_TYPES;
5557

58+
public static final String START_RECORDING_SCREEN;
59+
public static final String STOP_RECORDING_SCREEN;
5660

5761
protected static final String HIDE_KEYBOARD;
5862
protected static final String LOCK;
@@ -82,7 +86,7 @@ public class MobileCommand {
8286
protected static final String SET_SETTINGS;
8387
protected static final String GET_CURRENT_PACKAGE;
8488

85-
public static final Map<String, CommandInfo> commandRepository;
89+
public static final Map<String, CommandInfo> commandRepository;
8690

8791
static {
8892
RESET = "reset";
@@ -104,6 +108,9 @@ public class MobileCommand {
104108
GET_PERFORMANCE_DATA = "getPerformanceData";
105109
GET_SUPPORTED_PERFORMANCE_DATA_TYPES = "getSuppportedPerformanceDataTypes";
106110

111+
START_RECORDING_SCREEN = "startRecordingScreen";
112+
STOP_RECORDING_SCREEN = "stopRecordingScreen";
113+
107114
HIDE_KEYBOARD = "hideKeyboard";
108115
LOCK = "lock";
109116
SHAKE = "shake";
@@ -150,44 +157,47 @@ public class MobileCommand {
150157
commandRepository.put(GET_SETTINGS, getC("/session/:sessionId/appium/settings"));
151158
commandRepository.put(SET_SETTINGS, postC("/session/:sessionId/appium/settings"));
152159
commandRepository.put(GET_DEVICE_TIME, getC("/session/:sessionId/appium/device/system_time"));
153-
commandRepository.put(GET_SESSION,getC("/session/:sessionId/"));
160+
commandRepository.put(GET_SESSION, getC("/session/:sessionId/"));
154161
commandRepository.put(GET_SUPPORTED_PERFORMANCE_DATA_TYPES,
155-
postC("/session/:sessionId/appium/performanceData/types"));
162+
postC("/session/:sessionId/appium/performanceData/types"));
156163
commandRepository.put(GET_PERFORMANCE_DATA,
157-
postC("/session/:sessionId/appium/getPerformanceData"));
158-
164+
postC("/session/:sessionId/appium/getPerformanceData"));
165+
commandRepository.put(START_RECORDING_SCREEN,
166+
postC("/session/:sessionId/appium/start_recording_screen"));
167+
commandRepository.put(STOP_RECORDING_SCREEN,
168+
postC("/session/:sessionId/appium/stop_recording_screen"));
159169
//iOS
160170
commandRepository.put(SHAKE, postC("/session/:sessionId/appium/device/shake"));
161171
commandRepository.put(TOUCH_ID, postC("/session/:sessionId/appium/simulator/touch_id"));
162172
commandRepository.put(TOUCH_ID_ENROLLMENT,
163173
postC("/session/:sessionId/appium/simulator/toggle_touch_id_enrollment"));
164174
//Android
165175
commandRepository.put(CURRENT_ACTIVITY,
166-
getC("/session/:sessionId/appium/device/current_activity"));
176+
getC("/session/:sessionId/appium/device/current_activity"));
167177
commandRepository.put(END_TEST_COVERAGE,
168-
postC("/session/:sessionId/appium/app/end_test_coverage"));
178+
postC("/session/:sessionId/appium/app/end_test_coverage"));
169179
commandRepository.put(GET_DISPLAY_DENSITY, getC("/session/:sessionId/appium/device/display_density"));
170180
commandRepository.put(GET_NETWORK_CONNECTION, getC("/session/:sessionId/network_connection"));
171181
commandRepository.put(GET_SYSTEM_BARS, getC("/session/:sessionId/appium/device/system_bars"));
172182
commandRepository.put(IS_KEYBOARD_SHOWN, getC("/session/:sessionId/appium/device/is_keyboard_shown"));
173183
commandRepository.put(IS_LOCKED, postC("/session/:sessionId/appium/device/is_locked"));
174184
commandRepository.put(LONG_PRESS_KEY_CODE,
175-
postC("/session/:sessionId/appium/device/long_press_keycode"));
185+
postC("/session/:sessionId/appium/device/long_press_keycode"));
176186
commandRepository.put(FINGER_PRINT, postC("/session/:sessionId/appium/device/finger_print"));
177187
commandRepository.put(OPEN_NOTIFICATIONS,
178-
postC("/session/:sessionId/appium/device/open_notifications"));
188+
postC("/session/:sessionId/appium/device/open_notifications"));
179189
commandRepository.put(PRESS_KEY_CODE,
180-
postC("/session/:sessionId/appium/device/press_keycode"));
190+
postC("/session/:sessionId/appium/device/press_keycode"));
181191
commandRepository.put(PUSH_FILE, postC("/session/:sessionId/appium/device/push_file"));
182192
commandRepository.put(SET_NETWORK_CONNECTION,
183-
postC("/session/:sessionId/network_connection"));
193+
postC("/session/:sessionId/network_connection"));
184194
commandRepository.put(START_ACTIVITY,
185-
postC("/session/:sessionId/appium/device/start_activity"));
195+
postC("/session/:sessionId/appium/device/start_activity"));
186196
commandRepository.put(TOGGLE_LOCATION_SERVICES,
187-
postC("/session/:sessionId/appium/device/toggle_location_services"));
197+
postC("/session/:sessionId/appium/device/toggle_location_services"));
188198
commandRepository.put(UNLOCK, postC("/session/:sessionId/appium/device/unlock"));
189-
commandRepository. put(REPLACE_VALUE, postC("/session/:sessionId/appium/element/:id/replace_value"));
190-
commandRepository.put(GET_CURRENT_PACKAGE,getC("/session/:sessionId/appium/device/current_package"));
199+
commandRepository.put(REPLACE_VALUE, postC("/session/:sessionId/appium/element/:id/replace_value"));
200+
commandRepository.put(GET_CURRENT_PACKAGE, getC("/session/:sessionId/appium/device/current_package"));
191201
}
192202

193203
/**
@@ -246,8 +256,8 @@ public static AppiumCommandInfo deleteC(String url) {
246256
*/
247257
public static Map.Entry<String, Map<String, ?>> hideKeyboardCommand(String strategy,
248258
String keyName) {
249-
String[] parameters = new String[] {"strategy", "key"};
250-
Object[] values = new Object[] {strategy, keyName};
259+
String[] parameters = new String[]{"strategy", "key"};
260+
Object[] values = new Object[]{strategy, keyName};
251261
return new AbstractMap.SimpleEntry<>(
252262
HIDE_KEYBOARD, prepareArguments(parameters, values));
253263
}
@@ -260,7 +270,7 @@ public static AppiumCommandInfo deleteC(String url) {
260270
* @return built {@link ImmutableMap}.
261271
*/
262272
public static ImmutableMap<String, Object> prepareArguments(String param,
263-
Object value) {
273+
Object value) {
264274
ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
265275
builder.put(param, value);
266276
return builder.build();
@@ -274,7 +284,7 @@ public static ImmutableMap<String, Object> prepareArguments(String param,
274284
* @return built {@link ImmutableMap}.
275285
*/
276286
public static ImmutableMap<String, Object> prepareArguments(String[] params,
277-
Object[] values) {
287+
Object[] values) {
278288
ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
279289
for (int i = 0; i < params.length; i++) {
280290
if (!StringUtils.isBlank(params[i]) && (values[i] != null)) {
@@ -308,8 +318,8 @@ public static ImmutableMap<String, Object> prepareArguments(String[] params,
308318
*/
309319
public static Map.Entry<String, Map<String, ?>> pressKeyCodeCommand(int key,
310320
Integer metastate) {
311-
String[] parameters = new String[] {"keycode", "metastate"};
312-
Object[] values = new Object[] {key, metastate};
321+
String[] parameters = new String[]{"keycode", "metastate"};
322+
Object[] values = new Object[]{key, metastate};
313323
return new AbstractMap.SimpleEntry<>(
314324
PRESS_KEY_CODE, prepareArguments(parameters, values));
315325
}
@@ -338,8 +348,8 @@ public static ImmutableMap<String, Object> prepareArguments(String[] params,
338348
*/
339349
public static Map.Entry<String, Map<String, ?>> longPressKeyCodeCommand(int key,
340350
Integer metastate) {
341-
String[] parameters = new String[] {"keycode", "metastate"};
342-
Object[] values = new Object[] {key, metastate};
351+
String[] parameters = new String[]{"keycode", "metastate"};
352+
Object[] values = new Object[]{key, metastate};
343353
return new AbstractMap.SimpleEntry<>(
344354
LONG_PRESS_KEY_CODE, prepareArguments(parameters, values));
345355
}
@@ -349,7 +359,7 @@ public static ImmutableMap<String, Object> prepareArguments(String[] params,
349359
* device locking.
350360
*
351361
* @param duration for how long to lock the screen for. Minimum time resolution is one second
352-
* @return a key-value pair. The key is the command name. The value is a
362+
* @return a key-value pair. The key is the command name. The value is a
353363
* {@link java.util.Map} command arguments.
354364
*/
355365
public static Map.Entry<String, Map<String, ?>> lockDeviceCommand(Duration duration) {
@@ -402,4 +412,14 @@ public static ImmutableMap<String, Object> prepareArguments(String[] params,
402412
Object[] values = new Object[]{remotePath, new String(base64Data, StandardCharsets.UTF_8)};
403413
return new AbstractMap.SimpleEntry<>(PUSH_FILE, prepareArguments(parameters, values));
404414
}
415+
416+
public static Map.Entry<String, Map<String, ?>> startRecordingScreenCommand(BaseStartScreenRecordingOptions opts) {
417+
return new AbstractMap.SimpleEntry<>(START_RECORDING_SCREEN,
418+
prepareArguments("options", opts.build()));
419+
}
420+
421+
public static Map.Entry<String, Map<String, ?>> stopRecordingScreenCommand(BaseStopScreenRecordingOptions opts) {
422+
return new AbstractMap.SimpleEntry<>(STOP_RECORDING_SCREEN,
423+
prepareArguments("options", opts.build()));
424+
}
405425
}

src/main/java/io/appium/java_client/android/AndroidDriver.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import io.appium.java_client.LocksDevice;
2727
import io.appium.java_client.PressesKeyCode;
2828
import io.appium.java_client.remote.MobilePlatform;
29+
import io.appium.java_client.screenrecording.CanRecordScreen;
2930
import io.appium.java_client.service.local.AppiumDriverLocalService;
3031
import io.appium.java_client.service.local.AppiumServiceBuilder;
3132
import org.openqa.selenium.Capabilities;
@@ -50,7 +51,7 @@ public class AndroidDriver<T extends WebElement>
5051
extends AppiumDriver<T>
5152
implements PressesKeyCode, HasNetworkConnection, PushesFiles, StartsActivity,
5253
FindsByAndroidUIAutomator<T>, LocksDevice, HasAndroidSettings, HasDeviceDetails,
53-
HasSupportedPerformanceDataType, AuthenticatesByFinger {
54+
HasSupportedPerformanceDataType, AuthenticatesByFinger, CanRecordScreen {
5455

5556
private static final String ANDROID_PLATFORM = MobilePlatform.ANDROID;
5657

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* See the NOTICE file distributed with this work for additional
5+
* information regarding copyright ownership.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.appium.java_client.android;
18+
19+
import com.google.common.collect.ImmutableMap;
20+
import io.appium.java_client.screenrecording.BaseStartScreenRecordingOptions;
21+
22+
import java.time.Duration;
23+
import java.util.Map;
24+
25+
import static java.util.Optional.ofNullable;
26+
27+
public class AndroidStartScreenRecordingOptions
28+
extends BaseStartScreenRecordingOptions<AndroidStartScreenRecordingOptions> {
29+
private Integer bitRate;
30+
private String videoSize;
31+
32+
public static AndroidStartScreenRecordingOptions startScreenRecordingOptions() {
33+
return new AndroidStartScreenRecordingOptions();
34+
}
35+
36+
/**
37+
* The video bit rate for the video, in megabits per second.
38+
* The default value is 4. You can increase the bit rate to improve video quality,
39+
* but doing so results in larger movie files.
40+
* The value of 5000000 equals to 5Mb/sec.
41+
*
42+
* @param bitRate The actual bit rate (Mb/s).
43+
* @return self instance for chaining.
44+
*/
45+
public AndroidStartScreenRecordingOptions withBitRate(int bitRate) {
46+
this.bitRate = bitRate;
47+
return this;
48+
}
49+
50+
/**
51+
* The video size of the generated media file. The format is WIDTHxHEIGHT.
52+
* The default value is the device's native display resolution (if supported),
53+
* 1280x720 if not. For best results,
54+
* use a size supported by your device's Advanced Video Coding (AVC) encoder.
55+
*
56+
* @param videoSize The actual video size: WIDTHxHEIGHT.
57+
* @return self instance for chaining.
58+
*/
59+
public AndroidStartScreenRecordingOptions withVideoSize(String videoSize) {
60+
this.videoSize = videoSize;
61+
return this;
62+
}
63+
64+
/**
65+
* The maximum recording time. The default and maximum value is 180 seconds (3 minutes).
66+
* Setting values greater than this or less than zero will cause an exception. The minimum
67+
* time resolution unit is one second.
68+
*
69+
* @param timeLimit The actual time limit of the recorded video.
70+
* @return self instance for chaining.
71+
*/
72+
@Override
73+
public AndroidStartScreenRecordingOptions withTimeLimit(Duration timeLimit) {
74+
return super.withTimeLimit(timeLimit);
75+
}
76+
77+
@Override
78+
public Map<String, Object> build() {
79+
final ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
80+
builder.putAll(super.build());
81+
ofNullable(bitRate).map(x -> builder.put("bitRate", x));
82+
ofNullable(videoSize).map(x -> builder.put("videoSize", x));
83+
return builder.build();
84+
}
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* See the NOTICE file distributed with this work for additional
5+
* information regarding copyright ownership.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.appium.java_client.android;
18+
19+
import io.appium.java_client.screenrecording.BaseStopScreenRecordingOptions;
20+
21+
public class AndroidStopScreenRecordingOptions extends
22+
BaseStopScreenRecordingOptions<AndroidStopScreenRecordingOptions> {
23+
24+
public static AndroidStopScreenRecordingOptions stopScreenRecordingOptions() {
25+
return new AndroidStopScreenRecordingOptions();
26+
}
27+
28+
}

src/main/java/io/appium/java_client/ios/IOSDriver.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import io.appium.java_client.HidesKeyboardWithKeyName;
2727
import io.appium.java_client.LocksDevice;
2828
import io.appium.java_client.remote.MobilePlatform;
29+
import io.appium.java_client.screenrecording.CanRecordScreen;
2930
import io.appium.java_client.service.local.AppiumDriverLocalService;
3031
import io.appium.java_client.service.local.AppiumServiceBuilder;
3132
import org.openqa.selenium.Alert;
@@ -55,7 +56,7 @@ public class IOSDriver<T extends WebElement>
5556
extends AppiumDriver<T>
5657
implements HidesKeyboardWithKeyName, ShakesDevice, HasIOSSettings,
5758
FindsByIosUIAutomation<T>, LocksDevice, PerformsTouchID, FindsByIosNSPredicate<T>,
58-
FindsByIosClassChain<T>, PushesFiles {
59+
FindsByIosClassChain<T>, PushesFiles, CanRecordScreen {
5960

6061
private static final String IOS_PLATFORM = MobilePlatform.IOS;
6162

0 commit comments

Comments
 (0)