Skip to content
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
30 changes: 14 additions & 16 deletions src/main/java/io/appium/java_client/PullsFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@
package io.appium.java_client;

import com.google.common.collect.ImmutableMap;
import org.openqa.selenium.remote.Response;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

import static io.appium.java_client.MobileCommand.PULL_FILE;
import static io.appium.java_client.MobileCommand.PULL_FOLDER;

public interface PullsFiles extends ExecutesMethod {

/**
Expand All @@ -33,15 +29,16 @@ public interface PullsFiles extends ExecutesMethod {
* built with debuggable flag enabled in order to get access to its container
* on the internal file system.
*
* @param remotePath If the path starts with <em>@applicationId/</em>/ prefix, then the file
* will be pulled from the root of the corresponding application container.
* Otherwise, the root folder is considered as / on Android and
* on iOS it is a media folder root (real devices only).
* @param remotePath Path to file to read data from the remote device.
* Check the documentation on `mobile: pullFile`
* extension for more details on possible values
* for different platforms.
* @return A byte array of Base64 encoded data.
*/
default byte[] pullFile(String remotePath) {
Response response = execute(PULL_FILE, ImmutableMap.of("path", remotePath));
String base64String = response.getValue().toString();
String base64String = CommandExecutionHelper.executeScript(this, "mobile: pullFile", ImmutableMap.of(
"remotePath", remotePath
));
return Base64.getDecoder().decode(base64String.getBytes(StandardCharsets.UTF_8));
}

Expand All @@ -51,15 +48,16 @@ default byte[] pullFile(String remotePath) {
* built with debuggable flag enabled in order to get access to its container
* on the internal file system.
*
* @param remotePath If the path starts with <em>@applicationId/</em> prefix, then the folder
* will be pulled from the root of the corresponding application container.
* Otherwise, the root folder is considered as / on Android and
* on iOS it is a media folder root (real devices only).
* @param remotePath Path to a folder to read data from the remote device.
* Check the documentation on `mobile: pullFolder`
* extension for more details on possible values
* for different platforms.
* @return A byte array of Base64 encoded zip archive data.
*/
default byte[] pullFolder(String remotePath) {
Response response = execute(PULL_FOLDER, ImmutableMap.of("path", remotePath));
String base64String = response.getValue().toString();
String base64String = CommandExecutionHelper.executeScript(this, "mobile: pullFolder", ImmutableMap.of(
"remotePath", remotePath
));
return Base64.getDecoder().decode(base64String.getBytes(StandardCharsets.UTF_8));
}

Expand Down
33 changes: 13 additions & 20 deletions src/main/java/io/appium/java_client/PushesFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,40 @@

package io.appium.java_client;

import com.google.common.collect.ImmutableMap;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

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 system.
* Saves base64-encoded data as a file on the remote system.
*
* @param remotePath Path to file to write data to on remote device
* Only the filename part matters there on Simulator, 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.
* If the path starts with <em>@applicationId/</em> prefix, then the file
* will be pushed to the root of the corresponding application container.
* @param remotePath Path to file to write data to on remote device.
* Check the documentation on `mobile: pushFile`
* extension for more details on possible values
* for different platforms.
* @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));
CommandExecutionHelper.executeScript(this, "mobile: pushFile", ImmutableMap.of(
"remotePath", remotePath,
"payload", new String(base64Data, StandardCharsets.UTF_8)
));
}

/**
* Saves base64 encoded data as a media file on the remote system.
* Sends the file to the remote device.
*
* @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
* @throws IOException when there are problems with a file on 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.getEncoder().encode(FileUtils.readFileToByteArray(file)));
}

Expand Down