Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pushFile support to IOSDriver #721

Merged
merged 1 commit into from
Sep 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/main/java/io/appium/java_client/MobileCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -357,4 +357,19 @@ public static ImmutableMap<String, Object> 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<String, Map<String, ?>> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,22 +196,6 @@ public class AndroidMobileCommandHelper extends MobileCommand {
OPEN_NOTIFICATIONS, ImmutableMap.<String, Object>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<String, Map<String, ?>> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/appium/java_client/ios/IOSDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class IOSDriver<T extends WebElement>
extends AppiumDriver<T>
implements HidesKeyboardWithKeyName, ShakesDevice, HasIOSSettings,
FindsByIosUIAutomation<T>, LocksIOSDevice, PerformsTouchID, FindsByIosNSPredicate<T>,
FindsByIosClassChain<T> {
FindsByIosClassChain<T>, PushesFiles {

private static final String IOS_PLATFORM = MobilePlatform.IOS;

Expand Down
64 changes: 64 additions & 0 deletions src/main/java/io/appium/java_client/ios/PushesFiles.java
Original file line number Diff line number Diff line change
@@ -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)));
}

}