Skip to content

Commit 0130270

Browse files
Merge pull request #721 from mykola-mokhnach/push_file_ios
Add pushFile support to IOSDriver
2 parents c9b80c8 + a9fdfde commit 0130270

File tree

5 files changed

+82
-19
lines changed

5 files changed

+82
-19
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,4 +357,19 @@ public static ImmutableMap<String, Object> prepareArguments(String[] params,
357357
return new AbstractMap.SimpleEntry<>(SET_SETTINGS, prepareArguments("settings",
358358
prepareArguments(setting.toString(), value)));
359359
}
360+
361+
/**
362+
* This method forms a {@link java.util.Map} of parameters for the
363+
* file pushing
364+
*
365+
* @param remotePath Path to file to write data to on remote device
366+
* @param base64Data Base64 encoded byte array of data to write to remote device
367+
* @return a key-value pair. The key is the command name. The value is a
368+
* {@link java.util.Map} command arguments.
369+
*/
370+
public static Map.Entry<String, Map<String, ?>> pushFileCommand(String remotePath, byte[] base64Data) {
371+
String[] parameters = new String[] {"path", "data"};
372+
Object[] values = new Object[] {remotePath, base64Data};
373+
return new AbstractMap.SimpleEntry<>(PUSH_FILE, prepareArguments(parameters, values));
374+
}
360375
}

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -196,22 +196,6 @@ public class AndroidMobileCommandHelper extends MobileCommand {
196196
OPEN_NOTIFICATIONS, ImmutableMap.<String, Object>of());
197197
}
198198

199-
/**
200-
* This method forms a {@link java.util.Map} of parameters for the
201-
* file pushing
202-
*
203-
* @param remotePath Path to file to write data to on remote device
204-
* @param base64Data Base64 encoded byte array of data to write to remote device
205-
* @return a key-value pair. The key is the command name. The value is a
206-
* {@link java.util.Map} command arguments.
207-
*/
208-
public static Map.Entry<String, Map<String, ?>> pushFileCommandCommand(String remotePath,
209-
byte[] base64Data) {
210-
String[] parameters = new String[] {"path", "data"};
211-
Object[] values = new Object[] {remotePath, base64Data};
212-
return new AbstractMap.SimpleEntry<>(PUSH_FILE, prepareArguments(parameters, values));
213-
}
214-
215199
/**
216200
* This method forms a {@link java.util.Map} of parameters for the
217201
* setting of device network connection.

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

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

1919
import static com.google.common.base.Preconditions.checkNotNull;
20-
import static io.appium.java_client.android.AndroidMobileCommandHelper.pushFileCommandCommand;
20+
import static io.appium.java_client.MobileCommand.pushFileCommand;
2121

2222
import io.appium.java_client.CommandExecutionHelper;
2323
import io.appium.java_client.ExecutesMethod;
@@ -37,7 +37,7 @@ public interface PushesFiles extends InteractsWithFiles, ExecutesMethod {
3737
* @param base64Data Base64 encoded byte array of data to write to remote device
3838
*/
3939
default void pushFile(String remotePath, byte[] base64Data) {
40-
CommandExecutionHelper.execute(this, pushFileCommandCommand(remotePath, base64Data));
40+
CommandExecutionHelper.execute(this, pushFileCommand(remotePath, base64Data));
4141
}
4242

4343
/**

src/main/java/io/appium/java_client/ios/IOSDriver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class IOSDriver<T extends WebElement>
5353
extends AppiumDriver<T>
5454
implements HidesKeyboardWithKeyName, ShakesDevice, HasIOSSettings,
5555
FindsByIosUIAutomation<T>, LocksIOSDevice, PerformsTouchID, FindsByIosNSPredicate<T>,
56-
FindsByIosClassChain<T> {
56+
FindsByIosClassChain<T>, PushesFiles {
5757

5858
private static final String IOS_PLATFORM = MobilePlatform.IOS;
5959

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License");
3+
* you may not use this file except in compliance with the License.
4+
* See the NOTICE file distributed with this work for additional
5+
* information regarding copyright ownership.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.appium.java_client.ios;
18+
19+
import io.appium.java_client.CommandExecutionHelper;
20+
import io.appium.java_client.ExecutesMethod;
21+
import org.apache.commons.codec.binary.Base64;
22+
import org.apache.commons.io.FileUtils;
23+
24+
import java.io.File;
25+
import java.io.IOException;
26+
27+
import static com.google.common.base.Preconditions.checkNotNull;
28+
import static io.appium.java_client.MobileCommand.pushFileCommand;
29+
30+
public interface PushesFiles extends ExecutesMethod {
31+
32+
/**
33+
* Saves base64 encoded data as a media file on the remote mobile device.
34+
* This method is only supported on Simulator running Xcode SDK 8.1+.
35+
*
36+
* @param remotePath Path to file to write data to on remote device
37+
* Only the filename part matters there, so the remote end
38+
* can figure out which type of media data it is and save
39+
* it into a proper folder on the target device. Check
40+
* 'xcrun simctl addmedia' output to get more details on
41+
* supported media types
42+
* @param base64Data Base64 encoded byte array of media file data to write to remote device
43+
*/
44+
default void pushFile(String remotePath, byte[] base64Data) {
45+
CommandExecutionHelper.execute(this, pushFileCommand(remotePath, base64Data));
46+
}
47+
48+
/**
49+
* Saves base64 encoded data as a media file on the remote mobile device.
50+
* This method is only supported on Simulator running Xcode SDK 8.1+.
51+
*
52+
* @param remotePath See the documentation on {@link #pushFile(String, byte[])}
53+
* @param file Is an existing local file to be written to the remote device
54+
* @throws IOException when there are problems with a file or current file system
55+
*/
56+
default void pushFile(String remotePath, File file) throws IOException {
57+
checkNotNull(file, "A reference to file should not be NULL");
58+
if (!file.exists()) {
59+
throw new IOException(String.format("The given file %s doesn't exist", file.getAbsolutePath()));
60+
}
61+
pushFile(remotePath, Base64.encodeBase64(FileUtils.readFileToByteArray(file)));
62+
}
63+
64+
}

0 commit comments

Comments
 (0)