Skip to content

Commit

Permalink
Merge pull request #814 from mykola-mokhnach/record_screen
Browse files Browse the repository at this point in the history
Add endpoints for screen recording API for iOS and Android
  • Loading branch information
TikhomirovSergey committed Feb 7, 2018
2 parents f9313d2 + 89d62e4 commit b1770f6
Show file tree
Hide file tree
Showing 14 changed files with 687 additions and 26 deletions.
68 changes: 44 additions & 24 deletions src/main/java/io/appium/java_client/MobileCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import com.google.common.collect.ImmutableMap;

import io.appium.java_client.screenrecording.BaseStartScreenRecordingOptions;
import io.appium.java_client.screenrecording.BaseStopScreenRecordingOptions;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.remote.CommandInfo;
import org.openqa.selenium.remote.http.HttpMethod;
Expand Down Expand Up @@ -53,6 +55,8 @@ public class MobileCommand {
protected static final String GET_PERFORMANCE_DATA;
protected static final String GET_SUPPORTED_PERFORMANCE_DATA_TYPES;

public static final String START_RECORDING_SCREEN;
public static final String STOP_RECORDING_SCREEN;

protected static final String HIDE_KEYBOARD;
protected static final String LOCK;
Expand Down Expand Up @@ -82,7 +86,7 @@ public class MobileCommand {
protected static final String SET_SETTINGS;
protected static final String GET_CURRENT_PACKAGE;

public static final Map<String, CommandInfo> commandRepository;
public static final Map<String, CommandInfo> commandRepository;

static {
RESET = "reset";
Expand All @@ -104,6 +108,9 @@ public class MobileCommand {
GET_PERFORMANCE_DATA = "getPerformanceData";
GET_SUPPORTED_PERFORMANCE_DATA_TYPES = "getSuppportedPerformanceDataTypes";

START_RECORDING_SCREEN = "startRecordingScreen";
STOP_RECORDING_SCREEN = "stopRecordingScreen";

HIDE_KEYBOARD = "hideKeyboard";
LOCK = "lock";
SHAKE = "shake";
Expand Down Expand Up @@ -150,44 +157,47 @@ public class MobileCommand {
commandRepository.put(GET_SETTINGS, getC("/session/:sessionId/appium/settings"));
commandRepository.put(SET_SETTINGS, postC("/session/:sessionId/appium/settings"));
commandRepository.put(GET_DEVICE_TIME, getC("/session/:sessionId/appium/device/system_time"));
commandRepository.put(GET_SESSION,getC("/session/:sessionId/"));
commandRepository.put(GET_SESSION, getC("/session/:sessionId/"));
commandRepository.put(GET_SUPPORTED_PERFORMANCE_DATA_TYPES,
postC("/session/:sessionId/appium/performanceData/types"));
postC("/session/:sessionId/appium/performanceData/types"));
commandRepository.put(GET_PERFORMANCE_DATA,
postC("/session/:sessionId/appium/getPerformanceData"));

postC("/session/:sessionId/appium/getPerformanceData"));
commandRepository.put(START_RECORDING_SCREEN,
postC("/session/:sessionId/appium/start_recording_screen"));
commandRepository.put(STOP_RECORDING_SCREEN,
postC("/session/:sessionId/appium/stop_recording_screen"));
//iOS
commandRepository.put(SHAKE, postC("/session/:sessionId/appium/device/shake"));
commandRepository.put(TOUCH_ID, postC("/session/:sessionId/appium/simulator/touch_id"));
commandRepository.put(TOUCH_ID_ENROLLMENT,
postC("/session/:sessionId/appium/simulator/toggle_touch_id_enrollment"));
//Android
commandRepository.put(CURRENT_ACTIVITY,
getC("/session/:sessionId/appium/device/current_activity"));
getC("/session/:sessionId/appium/device/current_activity"));
commandRepository.put(END_TEST_COVERAGE,
postC("/session/:sessionId/appium/app/end_test_coverage"));
postC("/session/:sessionId/appium/app/end_test_coverage"));
commandRepository.put(GET_DISPLAY_DENSITY, getC("/session/:sessionId/appium/device/display_density"));
commandRepository.put(GET_NETWORK_CONNECTION, getC("/session/:sessionId/network_connection"));
commandRepository.put(GET_SYSTEM_BARS, getC("/session/:sessionId/appium/device/system_bars"));
commandRepository.put(IS_KEYBOARD_SHOWN, getC("/session/:sessionId/appium/device/is_keyboard_shown"));
commandRepository.put(IS_LOCKED, postC("/session/:sessionId/appium/device/is_locked"));
commandRepository.put(LONG_PRESS_KEY_CODE,
postC("/session/:sessionId/appium/device/long_press_keycode"));
postC("/session/:sessionId/appium/device/long_press_keycode"));
commandRepository.put(FINGER_PRINT, postC("/session/:sessionId/appium/device/finger_print"));
commandRepository.put(OPEN_NOTIFICATIONS,
postC("/session/:sessionId/appium/device/open_notifications"));
postC("/session/:sessionId/appium/device/open_notifications"));
commandRepository.put(PRESS_KEY_CODE,
postC("/session/:sessionId/appium/device/press_keycode"));
postC("/session/:sessionId/appium/device/press_keycode"));
commandRepository.put(PUSH_FILE, postC("/session/:sessionId/appium/device/push_file"));
commandRepository.put(SET_NETWORK_CONNECTION,
postC("/session/:sessionId/network_connection"));
postC("/session/:sessionId/network_connection"));
commandRepository.put(START_ACTIVITY,
postC("/session/:sessionId/appium/device/start_activity"));
postC("/session/:sessionId/appium/device/start_activity"));
commandRepository.put(TOGGLE_LOCATION_SERVICES,
postC("/session/:sessionId/appium/device/toggle_location_services"));
postC("/session/:sessionId/appium/device/toggle_location_services"));
commandRepository.put(UNLOCK, postC("/session/:sessionId/appium/device/unlock"));
commandRepository. put(REPLACE_VALUE, postC("/session/:sessionId/appium/element/:id/replace_value"));
commandRepository.put(GET_CURRENT_PACKAGE,getC("/session/:sessionId/appium/device/current_package"));
commandRepository.put(REPLACE_VALUE, postC("/session/:sessionId/appium/element/:id/replace_value"));
commandRepository.put(GET_CURRENT_PACKAGE, getC("/session/:sessionId/appium/device/current_package"));
}

/**
Expand Down Expand Up @@ -246,8 +256,8 @@ public static AppiumCommandInfo deleteC(String url) {
*/
public static Map.Entry<String, Map<String, ?>> hideKeyboardCommand(String strategy,
String keyName) {
String[] parameters = new String[] {"strategy", "key"};
Object[] values = new Object[] {strategy, keyName};
String[] parameters = new String[]{"strategy", "key"};
Object[] values = new Object[]{strategy, keyName};
return new AbstractMap.SimpleEntry<>(
HIDE_KEYBOARD, prepareArguments(parameters, values));
}
Expand All @@ -260,7 +270,7 @@ public static AppiumCommandInfo deleteC(String url) {
* @return built {@link ImmutableMap}.
*/
public static ImmutableMap<String, Object> prepareArguments(String param,
Object value) {
Object value) {
ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
builder.put(param, value);
return builder.build();
Expand All @@ -274,7 +284,7 @@ public static ImmutableMap<String, Object> prepareArguments(String param,
* @return built {@link ImmutableMap}.
*/
public static ImmutableMap<String, Object> prepareArguments(String[] params,
Object[] values) {
Object[] values) {
ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
for (int i = 0; i < params.length; i++) {
if (!StringUtils.isBlank(params[i]) && (values[i] != null)) {
Expand Down Expand Up @@ -308,8 +318,8 @@ public static ImmutableMap<String, Object> prepareArguments(String[] params,
*/
public static Map.Entry<String, Map<String, ?>> pressKeyCodeCommand(int key,
Integer metastate) {
String[] parameters = new String[] {"keycode", "metastate"};
Object[] values = new Object[] {key, metastate};
String[] parameters = new String[]{"keycode", "metastate"};
Object[] values = new Object[]{key, metastate};
return new AbstractMap.SimpleEntry<>(
PRESS_KEY_CODE, prepareArguments(parameters, values));
}
Expand Down Expand Up @@ -338,8 +348,8 @@ public static ImmutableMap<String, Object> prepareArguments(String[] params,
*/
public static Map.Entry<String, Map<String, ?>> longPressKeyCodeCommand(int key,
Integer metastate) {
String[] parameters = new String[] {"keycode", "metastate"};
Object[] values = new Object[] {key, metastate};
String[] parameters = new String[]{"keycode", "metastate"};
Object[] values = new Object[]{key, metastate};
return new AbstractMap.SimpleEntry<>(
LONG_PRESS_KEY_CODE, prepareArguments(parameters, values));
}
Expand All @@ -349,7 +359,7 @@ public static ImmutableMap<String, Object> prepareArguments(String[] params,
* device locking.
*
* @param duration for how long to lock the screen for. Minimum time resolution is one second
* @return a key-value pair. The key is the command name. The value is a
* @return a key-value pair. The key is the command name. The value is a
* {@link java.util.Map} command arguments.
*/
public static Map.Entry<String, Map<String, ?>> lockDeviceCommand(Duration duration) {
Expand Down Expand Up @@ -402,4 +412,14 @@ public static ImmutableMap<String, Object> prepareArguments(String[] params,
Object[] values = new Object[]{remotePath, new String(base64Data, StandardCharsets.UTF_8)};
return new AbstractMap.SimpleEntry<>(PUSH_FILE, prepareArguments(parameters, values));
}

public static Map.Entry<String, Map<String, ?>> startRecordingScreenCommand(BaseStartScreenRecordingOptions opts) {
return new AbstractMap.SimpleEntry<>(START_RECORDING_SCREEN,
prepareArguments("options", opts.build()));
}

public static Map.Entry<String, Map<String, ?>> stopRecordingScreenCommand(BaseStopScreenRecordingOptions opts) {
return new AbstractMap.SimpleEntry<>(STOP_RECORDING_SCREEN,
prepareArguments("options", opts.build()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.appium.java_client.LocksDevice;
import io.appium.java_client.PressesKeyCode;
import io.appium.java_client.remote.MobilePlatform;
import io.appium.java_client.screenrecording.CanRecordScreen;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;
import org.openqa.selenium.Capabilities;
Expand All @@ -50,7 +51,7 @@ public class AndroidDriver<T extends WebElement>
extends AppiumDriver<T>
implements PressesKeyCode, HasNetworkConnection, PushesFiles, StartsActivity,
FindsByAndroidUIAutomator<T>, LocksDevice, HasAndroidSettings, HasDeviceDetails,
HasSupportedPerformanceDataType, AuthenticatesByFinger {
HasSupportedPerformanceDataType, AuthenticatesByFinger, CanRecordScreen {

private static final String ANDROID_PLATFORM = MobilePlatform.ANDROID;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.appium.java_client.android;

import com.google.common.collect.ImmutableMap;
import io.appium.java_client.screenrecording.BaseStartScreenRecordingOptions;

import java.time.Duration;
import java.util.Map;

import static java.util.Optional.ofNullable;

public class AndroidStartScreenRecordingOptions
extends BaseStartScreenRecordingOptions<AndroidStartScreenRecordingOptions> {
private Integer bitRate;
private String videoSize;

public static AndroidStartScreenRecordingOptions startScreenRecordingOptions() {
return new AndroidStartScreenRecordingOptions();
}

/**
* The video bit rate for the video, in megabits per second.
* The default value is 4. You can increase the bit rate to improve video quality,
* but doing so results in larger movie files.
* The value of 5000000 equals to 5Mb/sec.
*
* @param bitRate The actual bit rate (Mb/s).
* @return self instance for chaining.
*/
public AndroidStartScreenRecordingOptions withBitRate(int bitRate) {
this.bitRate = bitRate;
return this;
}

/**
* The video size of the generated media file. The format is WIDTHxHEIGHT.
* The default value is the device's native display resolution (if supported),
* 1280x720 if not. For best results,
* use a size supported by your device's Advanced Video Coding (AVC) encoder.
*
* @param videoSize The actual video size: WIDTHxHEIGHT.
* @return self instance for chaining.
*/
public AndroidStartScreenRecordingOptions withVideoSize(String videoSize) {
this.videoSize = videoSize;
return this;
}

/**
* The maximum recording time. The default and maximum value is 180 seconds (3 minutes).
* Setting values greater than this or less than zero will cause an exception. The minimum
* time resolution unit is one second.
*
* @param timeLimit The actual time limit of the recorded video.
* @return self instance for chaining.
*/
@Override
public AndroidStartScreenRecordingOptions withTimeLimit(Duration timeLimit) {
return super.withTimeLimit(timeLimit);
}

@Override
public Map<String, Object> build() {
final ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder();
builder.putAll(super.build());
ofNullable(bitRate).map(x -> builder.put("bitRate", x));
ofNullable(videoSize).map(x -> builder.put("videoSize", x));
return builder.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.appium.java_client.android;

import io.appium.java_client.screenrecording.BaseStopScreenRecordingOptions;

public class AndroidStopScreenRecordingOptions extends
BaseStopScreenRecordingOptions<AndroidStopScreenRecordingOptions> {

public static AndroidStopScreenRecordingOptions stopScreenRecordingOptions() {
return new AndroidStopScreenRecordingOptions();
}

}
3 changes: 2 additions & 1 deletion src/main/java/io/appium/java_client/ios/IOSDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.appium.java_client.HidesKeyboardWithKeyName;
import io.appium.java_client.LocksDevice;
import io.appium.java_client.remote.MobilePlatform;
import io.appium.java_client.screenrecording.CanRecordScreen;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;
import org.openqa.selenium.Alert;
Expand Down Expand Up @@ -55,7 +56,7 @@ public class IOSDriver<T extends WebElement>
extends AppiumDriver<T>
implements HidesKeyboardWithKeyName, ShakesDevice, HasIOSSettings,
FindsByIosUIAutomation<T>, LocksDevice, PerformsTouchID, FindsByIosNSPredicate<T>,
FindsByIosClassChain<T>, PushesFiles {
FindsByIosClassChain<T>, PushesFiles, CanRecordScreen {

private static final String IOS_PLATFORM = MobilePlatform.IOS;

Expand Down
Loading

0 comments on commit b1770f6

Please sign in to comment.