2121import org .openqa .selenium .*;
2222import org .openqa .selenium .remote .*;
2323
24+ import javax .xml .bind .DatatypeConverter ;
2425import java .net .URL ;
2526import java .util .LinkedHashSet ;
2627import java .util .List ;
@@ -45,6 +46,10 @@ public AppiumDriver(URL remoteAddress, Capabilities desiredCapabilities){
4546 .put (KEY_EVENT , postC ("/session/:sessionId/appium/device/keyevent" ))
4647 .put (CURRENT_ACTIVITY , getC ("/session/:sessionId/appium/device/current_activity" ))
4748 .put (SET_VALUE , postC ("/session/:sessionId/appium/element/:id/value" ))
49+ .put (PULL_FILE , postC ("/session/:sessionId/appium/device/pull_file" ))
50+ .put (HIDE_KEYBOARD , postC ("/session/:sessionId/appium/device/hide_keyboard" ))
51+ .put (PUSH_FILE , postC ("/session/:sessionId/appium/device/push_file" ))
52+ .put (RUN_APP_IN_BACKGROUND , postC ("/session/:sessionId/appium/app/background" ))
4853 ;
4954 ImmutableMap <String , CommandInfo > mobileCommands = builder .build ();
5055
@@ -71,19 +76,39 @@ protected Response execute(String command) {
7176 }
7277
7378
79+ /**
80+ * Reset the currently running app for this session
81+ */
7482 public void resetApp () {
7583 execute (MobileCommand .RESET );
7684 }
7785
86+ /**
87+ * Get all defined Strings from an Android app
88+ *
89+ * @return a string of all the localized strings defined in the app
90+ */
7891 public String getAppStrings () {
7992 Response response = execute (GET_STRINGS );
8093 return response .getValue ().toString ();
8194 }
8295
96+ /**
97+ * Send a key event to the device
98+ *
99+ * @param key code for the key pressed on the device
100+ */
83101 public void sendKeyEvent (int key ) {
84102 sendKeyEvent (key , null );
85103 }
86104
105+ /**
106+ * Send a key event along with an Android metastate to an Android device
107+ * Metastates are things like *shift* to get uppercase characters
108+ *
109+ * @param key code for the key pressed on the Android device
110+ * @param metastate metastate for the keypress
111+ */
87112 public void sendKeyEvent (int key , Integer metastate ) {
88113 ImmutableMap .Builder builder = ImmutableMap .builder ();
89114 builder .put ("keycode" , key );
@@ -92,11 +117,65 @@ public void sendKeyEvent(int key, Integer metastate) {
92117 execute (KEY_EVENT , parameters );
93118 }
94119
120+ /**
121+ * Get the current activity being run on the mobile device
122+ */
95123 public String currentActivity () {
96124 Response response = execute (CURRENT_ACTIVITY );
97125 return response .getValue ().toString ();
98126 }
99127
128+ /**
129+ *
130+ * @param remotePath On Android and iOS, this is either the path to the file (relative to the root of the app's file system).
131+ * On iOS only, if path starts with /AppName.app, which will be replaced with the application's .app directory
132+ * @return A byte array of Base64 encoded data.
133+ */
134+ public byte [] pullFile (String remotePath ) {
135+ Response response = execute (PULL_FILE , ImmutableMap .of ("path" , remotePath ));
136+ String base64String = response .getValue ().toString ();
137+
138+ return DatatypeConverter .parseBase64Binary (base64String );
139+ }
140+
141+ /**
142+ * Save base64 encoded data as a file on the remote mobile device.
143+ * This is an Android only method.
144+ * @param remotePath Path to file to write data to on remote device
145+ * @param base64Data Base64 encoded byte array of data to write to remote device
146+ */
147+ public void pushFile (String remotePath , byte [] base64Data ) {
148+ ImmutableMap .Builder builder = ImmutableMap .builder ();
149+ builder .put ("path" , remotePath ).put ("data" , base64Data );
150+ execute (PUSH_FILE , builder .build ());
151+ }
152+
153+ /**
154+ * Hides the keyboard if it is showing.
155+ * This is an iOS only command.
156+ */
157+ public void hideKeyboard () {
158+ execute (HIDE_KEYBOARD );
159+ }
160+
161+ /**
162+ * Hides the keyboard by pressing the button specified by keyName if it is showing.
163+ * This is an iOS only command.
164+ * @param keyName The button pressed by the mobile driver to attempt hiding the keyboard
165+ */
166+ public void hideKeyboard (String keyName ) {
167+ execute (HIDE_KEYBOARD , ImmutableMap .of ("keyName" , keyName ));
168+ }
169+
170+ /**
171+ * Runs the current app as a background app for the number of seconds requested.
172+ * This is a synchronous method, it returns after the back has been returned to the foreground.
173+ * @param seconds Number of seconds to run App in background
174+ */
175+ public void runAppInBackground (int seconds ) {
176+ execute (RUN_APP_IN_BACKGROUND , ImmutableMap .of ("seconds" , seconds ));
177+ }
178+
100179
101180 @ Override
102181 public WebDriver context (String name ) {
0 commit comments