Skip to content

Commit 5694912

Browse files
authored
fix: Add fallback commands for file management APIs (#1910)
1 parent 1cc687f commit 5694912

File tree

2 files changed

+50
-16
lines changed

2 files changed

+50
-16
lines changed

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

+36-11
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@
1919
import com.google.common.collect.ImmutableMap;
2020

2121
import java.nio.charset.StandardCharsets;
22+
import java.util.AbstractMap;
2223
import java.util.Base64;
2324

2425
import static com.google.common.base.Preconditions.checkNotNull;
26+
import static io.appium.java_client.MobileCommand.PULL_FILE;
27+
import static io.appium.java_client.MobileCommand.PULL_FOLDER;
2528

26-
public interface PullsFiles extends ExecutesMethod {
29+
public interface PullsFiles extends ExecutesMethod, CanRememberExtensionPresence {
2730

2831
/**
2932
* Pull a file from the remote system.
@@ -38,11 +41,22 @@ public interface PullsFiles extends ExecutesMethod {
3841
* @return A byte array of Base64 encoded data.
3942
*/
4043
default byte[] pullFile(String remotePath) {
41-
String base64String = checkNotNull(
42-
CommandExecutionHelper.executeScript(this, "mobile: pullFile", ImmutableMap.of(
43-
"remotePath", remotePath
44-
))
45-
);
44+
final String extName = "mobile: pullFile";
45+
String base64String;
46+
try {
47+
base64String = checkNotNull(
48+
CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName,
49+
ImmutableMap.of("remotePath", remotePath)
50+
)
51+
);
52+
} catch (UnsupportedOperationException e) {
53+
// TODO: Remove the fallback
54+
base64String = checkNotNull(
55+
CommandExecutionHelper.execute(markExtensionAbsence(extName),
56+
new AbstractMap.SimpleEntry<>(PULL_FILE, ImmutableMap.of("path", remotePath))
57+
)
58+
);
59+
}
4660
return Base64.getDecoder().decode(base64String.getBytes(StandardCharsets.UTF_8));
4761
}
4862

@@ -59,11 +73,22 @@ default byte[] pullFile(String remotePath) {
5973
* @return A byte array of Base64 encoded zip archive data.
6074
*/
6175
default byte[] pullFolder(String remotePath) {
62-
String base64String = checkNotNull(
63-
CommandExecutionHelper.executeScript(this, "mobile: pullFolder", ImmutableMap.of(
64-
"remotePath", remotePath
65-
))
66-
);
76+
final String extName = "mobile: pullFolder";
77+
String base64String;
78+
try {
79+
base64String = checkNotNull(
80+
CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName,
81+
ImmutableMap.of("remotePath", remotePath)
82+
)
83+
);
84+
} catch (UnsupportedOperationException e) {
85+
// TODO: Remove the fallback
86+
base64String = checkNotNull(
87+
CommandExecutionHelper.execute(markExtensionAbsence(extName),
88+
new AbstractMap.SimpleEntry<>(PULL_FOLDER, ImmutableMap.of("path", remotePath))
89+
)
90+
);
91+
}
6792
return Base64.getDecoder().decode(base64String.getBytes(StandardCharsets.UTF_8));
6893
}
6994

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

+14-5
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@
1818

1919
import com.google.common.collect.ImmutableMap;
2020
import org.apache.commons.io.FileUtils;
21+
import org.openqa.selenium.UnsupportedCommandException;
2122

2223
import java.io.File;
2324
import java.io.IOException;
2425
import java.nio.charset.StandardCharsets;
2526
import java.util.Base64;
2627

27-
public interface PushesFiles extends ExecutesMethod {
28+
import static io.appium.java_client.MobileCommand.pushFileCommand;
29+
30+
public interface PushesFiles extends ExecutesMethod, CanRememberExtensionPresence {
2831

2932
/**
3033
* Saves base64-encoded data as a file on the remote system.
@@ -36,10 +39,16 @@ public interface PushesFiles extends ExecutesMethod {
3639
* @param base64Data Base64 encoded byte array of media file data to write to remote device
3740
*/
3841
default void pushFile(String remotePath, byte[] base64Data) {
39-
CommandExecutionHelper.executeScript(this, "mobile: pushFile", ImmutableMap.of(
40-
"remotePath", remotePath,
41-
"payload", new String(base64Data, StandardCharsets.UTF_8)
42-
));
42+
final String extName = "mobile: pushFile";
43+
try {
44+
CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName, ImmutableMap.of(
45+
"remotePath", remotePath,
46+
"payload", new String(base64Data, StandardCharsets.UTF_8)
47+
));
48+
} catch (UnsupportedCommandException e) {
49+
// TODO: Remove the fallback
50+
CommandExecutionHelper.execute(markExtensionAbsence(extName), pushFileCommand(remotePath, base64Data));
51+
}
4352
}
4453

4554
/**

0 commit comments

Comments
 (0)