Skip to content

Commit ab041b1

Browse files
The additional method pushFile(remotePath, file)
It is the minor change This method was added for the sake of user's convenience.
1 parent 608e10a commit ab041b1

3 files changed

Lines changed: 48 additions & 6 deletions

File tree

src/main/java/io/appium/java_client/android/AndroidDriver.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package io.appium.java_client.android;
1818

1919
import static com.google.common.base.Preconditions.checkArgument;
20+
import static com.google.common.base.Preconditions.checkNotNull;
2021
import static io.appium.java_client.MobileCommand.CURRENT_ACTIVITY;
2122
import static io.appium.java_client.MobileCommand.END_TEST_COVERAGE;
2223
import static io.appium.java_client.MobileCommand.GET_NETWORK_CONNECTION;
@@ -42,13 +43,17 @@
4243
import io.appium.java_client.remote.MobilePlatform;
4344
import io.appium.java_client.service.local.AppiumDriverLocalService;
4445
import io.appium.java_client.service.local.AppiumServiceBuilder;
46+
import org.apache.commons.codec.binary.Base64;
47+
import org.apache.commons.io.FileUtils;
4548
import org.apache.commons.lang3.StringUtils;
4649
import org.openqa.selenium.Capabilities;
4750
import org.openqa.selenium.WebDriverException;
4851
import org.openqa.selenium.WebElement;
4952
import org.openqa.selenium.remote.Response;
5053
import org.openqa.selenium.remote.http.HttpClient;
5154

55+
import java.io.File;
56+
import java.io.IOException;
5257
import java.net.URL;
5358
import java.util.List;
5459

@@ -284,17 +289,22 @@ static String uiScrollable(String uiSelector) {
284289
execute(SET_NETWORK_CONNECTION, getCommandImmutableMap(parameters, values));
285290
}
286291

287-
/**
288-
* @param remotePath Path to file to write data to on remote device.
289-
* @param base64Data Base64 encoded byte array of data to write to remote device.
290-
* @see PushesFiles#pushFile(String, byte[])
291-
*/
292292
@Override public void pushFile(String remotePath, byte[] base64Data) {
293293
String[] parameters = new String[] {"path", "data"};
294294
Object[] values = new Object[] {remotePath, base64Data};
295295
execute(PUSH_FILE, getCommandImmutableMap(parameters, values));
296296
}
297297

298+
@Override public void pushFile(String remotePath, File file) throws IOException {
299+
checkNotNull(file, "A reference to file should not be NULL");
300+
if (!file.exists()) {
301+
throw new IOException("The given file "
302+
+ file.getAbsolutePath() + " doesn't exist");
303+
}
304+
pushFile(remotePath,
305+
Base64.encodeBase64(FileUtils.readFileToByteArray(file)));
306+
}
307+
298308
@Override public void startActivity(String appPackage, String appActivity,
299309
String appWaitPackage,
300310
String appWaitActivity, String intentAction,

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,26 @@
1818

1919
import io.appium.java_client.InteractsWithFiles;
2020

21+
import java.io.File;
22+
import java.io.IOException;
23+
2124
public interface PushesFiles extends InteractsWithFiles {
2225

2326
/**
24-
* Save base64 encoded data as a file on the remote mobile device.
27+
* Saves base64 encoded data as a file on the remote mobile device.
2528
*
2629
* @param remotePath Path to file to write data to on remote device
2730
* @param base64Data Base64 encoded byte array of data to write to remote device
2831
*/
2932
void pushFile(String remotePath, byte[] base64Data);
3033

34+
/**
35+
* Saves given file as a file on the remote mobile device.
36+
*
37+
* @param remotePath Path to file to write data to on remote device
38+
* @param file is a file to write to remote device
39+
* @throws IOException when there are problems with a file or current file system
40+
*/
41+
void pushFile(String remotePath, File file) throws IOException;
42+
3143
}

src/test/java/io/appium/java_client/android/AndroidDriverTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@
2222

2323
import io.appium.java_client.AppiumSetting;
2424
import org.apache.commons.codec.binary.Base64;
25+
import org.apache.commons.io.FileUtils;
2526
import org.junit.Test;
2627
import org.openqa.selenium.ScreenOrientation;
2728
import org.openqa.selenium.html5.Location;
2829

30+
import java.io.File;
31+
2932
public class AndroidDriverTest extends BaseAndroidTest {
3033

3134
@Test public void getDeviceTimeTest() {
@@ -59,6 +62,23 @@ public class AndroidDriverTest extends BaseAndroidTest {
5962
returnDataDecoded);
6063
}
6164

65+
@Test public void pushTempFileTest() throws Exception {
66+
File temp = File.createTempFile("Temp_", "_test");
67+
try {
68+
FileUtils.writeStringToFile(temp, "The eventual code is no "
69+
+ "more than the deposit of your understanding. ~E. W. Dijkstra", "UTF-8", true);
70+
driver.pushFile("/data/local/tmp/remote2.txt", temp);
71+
byte[] returnData = driver.pullFile("/data/local/tmp/remote2.txt");
72+
String returnDataDecoded = new String(Base64.decodeBase64(returnData));
73+
assertEquals(
74+
"The eventual code is no more than the deposit of "
75+
+ "your understanding. ~E. W. Dijkstra",
76+
returnDataDecoded);
77+
} finally {
78+
FileUtils.forceDelete(temp);
79+
}
80+
}
81+
6282
@Test public void ignoreUnimportantViews() {
6383
driver.ignoreUnimportantViews(true);
6484
boolean ignoreViews =

0 commit comments

Comments
 (0)