From e317610394e5e9464943fc58975e2c36d9d1aded Mon Sep 17 00:00:00 2001 From: Valery Yatsynovich Date: Tue, 9 May 2023 14:48:59 +0300 Subject: [PATCH] fix: Add fallback commands for file management APIs --- .../io/appium/java_client/PullsFiles.java | 47 ++++++++++++++----- .../io/appium/java_client/PushesFiles.java | 19 ++++++-- 2 files changed, 50 insertions(+), 16 deletions(-) diff --git a/src/main/java/io/appium/java_client/PullsFiles.java b/src/main/java/io/appium/java_client/PullsFiles.java index 0042b0997..4b60a5748 100644 --- a/src/main/java/io/appium/java_client/PullsFiles.java +++ b/src/main/java/io/appium/java_client/PullsFiles.java @@ -19,11 +19,14 @@ import com.google.common.collect.ImmutableMap; import java.nio.charset.StandardCharsets; +import java.util.AbstractMap; import java.util.Base64; import static com.google.common.base.Preconditions.checkNotNull; +import static io.appium.java_client.MobileCommand.PULL_FILE; +import static io.appium.java_client.MobileCommand.PULL_FOLDER; -public interface PullsFiles extends ExecutesMethod { +public interface PullsFiles extends ExecutesMethod, CanRememberExtensionPresence { /** * Pull a file from the remote system. @@ -38,11 +41,22 @@ public interface PullsFiles extends ExecutesMethod { * @return A byte array of Base64 encoded data. */ default byte[] pullFile(String remotePath) { - String base64String = checkNotNull( - CommandExecutionHelper.executeScript(this, "mobile: pullFile", ImmutableMap.of( - "remotePath", remotePath - )) - ); + final String extName = "mobile: pullFile"; + String base64String; + try { + base64String = checkNotNull( + CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName, + ImmutableMap.of("remotePath", remotePath) + ) + ); + } catch (UnsupportedOperationException e) { + // TODO: Remove the fallback + base64String = checkNotNull( + CommandExecutionHelper.execute(markExtensionAbsence(extName), + new AbstractMap.SimpleEntry<>(PULL_FILE, ImmutableMap.of("path", remotePath)) + ) + ); + } return Base64.getDecoder().decode(base64String.getBytes(StandardCharsets.UTF_8)); } @@ -59,11 +73,22 @@ default byte[] pullFile(String remotePath) { * @return A byte array of Base64 encoded zip archive data. */ default byte[] pullFolder(String remotePath) { - String base64String = checkNotNull( - CommandExecutionHelper.executeScript(this, "mobile: pullFolder", ImmutableMap.of( - "remotePath", remotePath - )) - ); + final String extName = "mobile: pullFolder"; + String base64String; + try { + base64String = checkNotNull( + CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName, + ImmutableMap.of("remotePath", remotePath) + ) + ); + } catch (UnsupportedOperationException e) { + // TODO: Remove the fallback + base64String = checkNotNull( + CommandExecutionHelper.execute(markExtensionAbsence(extName), + new AbstractMap.SimpleEntry<>(PULL_FOLDER, ImmutableMap.of("path", remotePath)) + ) + ); + } return Base64.getDecoder().decode(base64String.getBytes(StandardCharsets.UTF_8)); } diff --git a/src/main/java/io/appium/java_client/PushesFiles.java b/src/main/java/io/appium/java_client/PushesFiles.java index 813dcb5b1..1bfd7f8eb 100644 --- a/src/main/java/io/appium/java_client/PushesFiles.java +++ b/src/main/java/io/appium/java_client/PushesFiles.java @@ -18,13 +18,16 @@ import com.google.common.collect.ImmutableMap; import org.apache.commons.io.FileUtils; +import org.openqa.selenium.UnsupportedCommandException; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Base64; -public interface PushesFiles extends ExecutesMethod { +import static io.appium.java_client.MobileCommand.pushFileCommand; + +public interface PushesFiles extends ExecutesMethod, CanRememberExtensionPresence { /** * Saves base64-encoded data as a file on the remote system. @@ -36,10 +39,16 @@ public interface PushesFiles extends ExecutesMethod { * @param base64Data Base64 encoded byte array of media file data to write to remote device */ default void pushFile(String remotePath, byte[] base64Data) { - CommandExecutionHelper.executeScript(this, "mobile: pushFile", ImmutableMap.of( - "remotePath", remotePath, - "payload", new String(base64Data, StandardCharsets.UTF_8) - )); + final String extName = "mobile: pushFile"; + try { + CommandExecutionHelper.executeScript(assertExtensionExists(extName), extName, ImmutableMap.of( + "remotePath", remotePath, + "payload", new String(base64Data, StandardCharsets.UTF_8) + )); + } catch (UnsupportedCommandException e) { + // TODO: Remove the fallback + CommandExecutionHelper.execute(markExtensionAbsence(extName), pushFileCommand(remotePath, base64Data)); + } } /**