From a9fdfdeebf5b88b88fe88ccdcf8949c27246345f Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Thu, 7 Sep 2017 21:49:31 +0200 Subject: [PATCH] Add pushFile support to IOSDriver --- .../io/appium/java_client/MobileCommand.java | 15 +++++ .../android/AndroidMobileCommandHelper.java | 16 ----- .../java_client/android/PushesFiles.java | 4 +- .../io/appium/java_client/ios/IOSDriver.java | 2 +- .../appium/java_client/ios/PushesFiles.java | 64 +++++++++++++++++++ 5 files changed, 82 insertions(+), 19 deletions(-) create mode 100644 src/main/java/io/appium/java_client/ios/PushesFiles.java diff --git a/src/main/java/io/appium/java_client/MobileCommand.java b/src/main/java/io/appium/java_client/MobileCommand.java index 7ab77cc90..d4a57044f 100644 --- a/src/main/java/io/appium/java_client/MobileCommand.java +++ b/src/main/java/io/appium/java_client/MobileCommand.java @@ -357,4 +357,19 @@ public static ImmutableMap prepareArguments(String[] params, return new AbstractMap.SimpleEntry<>(SET_SETTINGS, prepareArguments("settings", prepareArguments(setting.toString(), value))); } + + /** + * This method forms a {@link java.util.Map} of parameters for the + * file pushing + * + * @param remotePath Path to file to write data to on remote device + * @param base64Data Base64 encoded byte array of data to write to remote device + * @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> pushFileCommand(String remotePath, byte[] base64Data) { + String[] parameters = new String[] {"path", "data"}; + Object[] values = new Object[] {remotePath, base64Data}; + return new AbstractMap.SimpleEntry<>(PUSH_FILE, prepareArguments(parameters, values)); + } } diff --git a/src/main/java/io/appium/java_client/android/AndroidMobileCommandHelper.java b/src/main/java/io/appium/java_client/android/AndroidMobileCommandHelper.java index 3cfa8ff8c..9a13d73ce 100644 --- a/src/main/java/io/appium/java_client/android/AndroidMobileCommandHelper.java +++ b/src/main/java/io/appium/java_client/android/AndroidMobileCommandHelper.java @@ -196,22 +196,6 @@ public class AndroidMobileCommandHelper extends MobileCommand { OPEN_NOTIFICATIONS, ImmutableMap.of()); } - /** - * This method forms a {@link java.util.Map} of parameters for the - * file pushing - * - * @param remotePath Path to file to write data to on remote device - * @param base64Data Base64 encoded byte array of data to write to remote device - * @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> pushFileCommandCommand(String remotePath, - byte[] base64Data) { - String[] parameters = new String[] {"path", "data"}; - Object[] values = new Object[] {remotePath, base64Data}; - return new AbstractMap.SimpleEntry<>(PUSH_FILE, prepareArguments(parameters, values)); - } - /** * This method forms a {@link java.util.Map} of parameters for the * setting of device network connection. diff --git a/src/main/java/io/appium/java_client/android/PushesFiles.java b/src/main/java/io/appium/java_client/android/PushesFiles.java index e9708e5fa..4fd386383 100644 --- a/src/main/java/io/appium/java_client/android/PushesFiles.java +++ b/src/main/java/io/appium/java_client/android/PushesFiles.java @@ -17,7 +17,7 @@ package io.appium.java_client.android; import static com.google.common.base.Preconditions.checkNotNull; -import static io.appium.java_client.android.AndroidMobileCommandHelper.pushFileCommandCommand; +import static io.appium.java_client.MobileCommand.pushFileCommand; import io.appium.java_client.CommandExecutionHelper; import io.appium.java_client.ExecutesMethod; @@ -37,7 +37,7 @@ public interface PushesFiles extends InteractsWithFiles, ExecutesMethod { * @param base64Data Base64 encoded byte array of data to write to remote device */ default void pushFile(String remotePath, byte[] base64Data) { - CommandExecutionHelper.execute(this, pushFileCommandCommand(remotePath, base64Data)); + CommandExecutionHelper.execute(this, pushFileCommand(remotePath, base64Data)); } /** diff --git a/src/main/java/io/appium/java_client/ios/IOSDriver.java b/src/main/java/io/appium/java_client/ios/IOSDriver.java index 995c6370c..90abbafb9 100644 --- a/src/main/java/io/appium/java_client/ios/IOSDriver.java +++ b/src/main/java/io/appium/java_client/ios/IOSDriver.java @@ -53,7 +53,7 @@ public class IOSDriver extends AppiumDriver implements HidesKeyboardWithKeyName, ShakesDevice, HasIOSSettings, FindsByIosUIAutomation, LocksIOSDevice, PerformsTouchID, FindsByIosNSPredicate, - FindsByIosClassChain { + FindsByIosClassChain, PushesFiles { private static final String IOS_PLATFORM = MobilePlatform.IOS; diff --git a/src/main/java/io/appium/java_client/ios/PushesFiles.java b/src/main/java/io/appium/java_client/ios/PushesFiles.java new file mode 100644 index 000000000..961723fa5 --- /dev/null +++ b/src/main/java/io/appium/java_client/ios/PushesFiles.java @@ -0,0 +1,64 @@ +/* + * 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.ios; + +import io.appium.java_client.CommandExecutionHelper; +import io.appium.java_client.ExecutesMethod; +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.io.FileUtils; + +import java.io.File; +import java.io.IOException; + +import static com.google.common.base.Preconditions.checkNotNull; +import static io.appium.java_client.MobileCommand.pushFileCommand; + +public interface PushesFiles extends ExecutesMethod { + + /** + * Saves base64 encoded data as a media file on the remote mobile device. + * This method is only supported on Simulator running Xcode SDK 8.1+. + * + * @param remotePath Path to file to write data to on remote device + * Only the filename part matters there, so the remote end + * can figure out which type of media data it is and save + * it into a proper folder on the target device. Check + * 'xcrun simctl addmedia' output to get more details on + * supported media types + * @param base64Data Base64 encoded byte array of media file data to write to remote device + */ + default void pushFile(String remotePath, byte[] base64Data) { + CommandExecutionHelper.execute(this, pushFileCommand(remotePath, base64Data)); + } + + /** + * Saves base64 encoded data as a media file on the remote mobile device. + * This method is only supported on Simulator running Xcode SDK 8.1+. + * + * @param remotePath See the documentation on {@link #pushFile(String, byte[])} + * @param file Is an existing local file to be written to the remote device + * @throws IOException when there are problems with a file or current file system + */ + default void pushFile(String remotePath, File file) throws IOException { + checkNotNull(file, "A reference to file should not be NULL"); + if (!file.exists()) { + throw new IOException(String.format("The given file %s doesn't exist", file.getAbsolutePath())); + } + pushFile(remotePath, Base64.encodeBase64(FileUtils.readFileToByteArray(file))); + } + +}