1717
1818package io .appium .java_client ;
1919
20- import static io .appium .java_client .MobileCommand .CLOSE_APP ;
21- import static io .appium .java_client .MobileCommand .COMPLEX_FIND ;
22- import static io .appium .java_client .MobileCommand .CURRENT_ACTIVITY ;
23- import static io .appium .java_client .MobileCommand .END_TEST_COVERAGE ;
24- import static io .appium .java_client .MobileCommand .GET_NETWORK_CONNECTION ;
25- import static io .appium .java_client .MobileCommand .GET_SETTINGS ;
26- import static io .appium .java_client .MobileCommand .GET_STRINGS ;
27- import static io .appium .java_client .MobileCommand .HIDE_KEYBOARD ;
28- import static io .appium .java_client .MobileCommand .INSTALL_APP ;
29- import static io .appium .java_client .MobileCommand .IS_APP_INSTALLED ;
30- import static io .appium .java_client .MobileCommand .IS_LOCKED ;
31- import static io .appium .java_client .MobileCommand .KEY_EVENT ;
32- import static io .appium .java_client .MobileCommand .LAUNCH_APP ;
33- import static io .appium .java_client .MobileCommand .LOCK ;
34- import static io .appium .java_client .MobileCommand .OPEN_NOTIFICATIONS ;
35- import static io .appium .java_client .MobileCommand .PERFORM_MULTI_TOUCH ;
36- import static io .appium .java_client .MobileCommand .PERFORM_TOUCH_ACTION ;
37- import static io .appium .java_client .MobileCommand .PULL_FILE ;
38- import static io .appium .java_client .MobileCommand .PULL_FOLDER ;
39- import static io .appium .java_client .MobileCommand .PUSH_FILE ;
40- import static io .appium .java_client .MobileCommand .REMOVE_APP ;
41- import static io .appium .java_client .MobileCommand .RESET ;
42- import static io .appium .java_client .MobileCommand .RUN_APP_IN_BACKGROUND ;
43- import static io .appium .java_client .MobileCommand .SET_NETWORK_CONNECTION ;
44- import static io .appium .java_client .MobileCommand .SET_SETTINGS ;
45- import static io .appium .java_client .MobileCommand .SET_VALUE ;
46- import static io .appium .java_client .MobileCommand .SHAKE ;
47- import static io .appium .java_client .MobileCommand .START_ACTIVITY ;
20+ import com .google .common .collect .ImmutableList ;
21+ import com .google .common .collect .ImmutableMap ;
22+ import com .google .gson .JsonObject ;
23+ import com .google .gson .JsonParser ;
4824import io .appium .java_client .internal .JsonToMobileElementConverter ;
4925import io .appium .java_client .remote .MobileCapabilityType ;
26+ import org .openqa .selenium .*;
27+ import org .openqa .selenium .html5 .Location ;
28+ import org .openqa .selenium .html5 .LocationContext ;
29+ import org .openqa .selenium .remote .*;
30+ import org .openqa .selenium .remote .html5 .RemoteLocationContext ;
31+ import org .openqa .selenium .remote .http .HttpMethod ;
5032
33+ import javax .xml .bind .DatatypeConverter ;
5134import java .net .URL ;
5235import java .util .LinkedHashSet ;
5336import java .util .List ;
5437import java .util .Map ;
5538import java .util .Set ;
5639
57- import javax .xml .bind .DatatypeConverter ;
58-
59- import org .openqa .selenium .Capabilities ;
60- import org .openqa .selenium .ContextAware ;
61- import org .openqa .selenium .Dimension ;
62- import org .openqa .selenium .Point ;
63- import org .openqa .selenium .Rotatable ;
64- import org .openqa .selenium .ScreenOrientation ;
65- import org .openqa .selenium .WebDriver ;
66- import org .openqa .selenium .WebDriverException ;
67- import org .openqa .selenium .WebElement ;
68- import org .openqa .selenium .html5 .Location ;
69- import org .openqa .selenium .html5 .LocationContext ;
70- import org .openqa .selenium .remote .CommandInfo ;
71- import org .openqa .selenium .remote .DesiredCapabilities ;
72- import org .openqa .selenium .remote .DriverCommand ;
73- import org .openqa .selenium .remote .ErrorHandler ;
74- import org .openqa .selenium .remote .ExecuteMethod ;
75- import org .openqa .selenium .remote .HttpCommandExecutor ;
76- import org .openqa .selenium .remote .RemoteWebDriver ;
77- import org .openqa .selenium .remote .Response ;
78- import org .openqa .selenium .remote .html5 .RemoteLocationContext ;
79- import org .openqa .selenium .remote .http .HttpMethod ;
80-
81- import com .google .common .collect .ImmutableList ;
82- import com .google .common .collect .ImmutableMap ;
83- import com .google .gson .JsonObject ;
84- import com .google .gson .JsonParser ;
40+ import static io .appium .java_client .MobileCommand .*;
8541
8642public class AppiumDriver extends RemoteWebDriver implements MobileDriver ,
8743 ContextAware , Rotatable , FindsByIosUIAutomation ,
@@ -138,7 +94,7 @@ protected static ImmutableMap<String, Object> getCommandImmutableMap(
13894 String [] params , Object [] values ) {
13995 ImmutableMap .Builder <String , Object > builder = ImmutableMap .builder ();
14096 for (int i =0 ; i < params .length ; i ++ ){
141- if (_isNotNullOrEmpty (params [i ])){
97+ if (_isNotNullOrEmpty (params [i ]) && _isNotNullOrEmpty ( values [ i ]) ){
14298 builder .put (params [i ], values [i ]);
14399 }
144100 }
@@ -238,6 +194,58 @@ public void resetApp() {
238194 execute (MobileCommand .RESET );
239195 }
240196
197+
198+ /**
199+ * @see InteractsWithApps#isAppInstalled(String)
200+ */
201+ @ Override
202+ public boolean isAppInstalled (String bundleId ) {
203+ Response response = execute (IS_APP_INSTALLED ,
204+ ImmutableMap .of ("bundleId" , bundleId ));
205+
206+ return Boolean .parseBoolean (response .getValue ().toString ());
207+ }
208+
209+ /**
210+ * @see InteractsWithApps#installApp(String)
211+ */
212+ @ Override
213+ public void installApp (String appPath ) {
214+ execute (INSTALL_APP , ImmutableMap .of ("appPath" , appPath ));
215+ }
216+
217+ /**
218+ * @see InteractsWithApps#removeApp(String)
219+ */
220+ @ Override
221+ public void removeApp (String bundleId ) {
222+ execute (REMOVE_APP , ImmutableMap .of ("bundleId" , bundleId ));
223+ }
224+
225+ /**
226+ * @see InteractsWithApps#launchApp()
227+ */
228+ @ Override
229+ public void launchApp () {
230+ execute (LAUNCH_APP );
231+ }
232+
233+ /**
234+ * @see InteractsWithApps#closeApp()
235+ */
236+ @ Override
237+ public void closeApp () {
238+ execute (CLOSE_APP );
239+ }
240+
241+ /**
242+ * @see InteractsWithApps#runAppInBackground(int)
243+ */
244+ @ Override
245+ public void runAppInBackground (int seconds ) {
246+ execute (RUN_APP_IN_BACKGROUND , ImmutableMap .of ("seconds" , seconds ));
247+ }
248+
241249 /**
242250 * Send a key event to the device
243251 *
@@ -249,17 +257,13 @@ public void sendKeyEvent(int key) {
249257 execute (KEY_EVENT , getCommandImmutableMap (KEY_CODE , key ));
250258 }
251259
252- /**
253- * Checks if a string is null, empty, or whitespace.
254- *
255- * @param str
256- * String to check.
257- *
258- * @return True if str is not null or empty.
259- */
260- protected static boolean _isNotNullOrEmpty (String str ) {
261- return str != null && !str .isEmpty () && str .trim ().length () > 0 ;
262- }
260+ /**
261+ * @see DeviceActionShortcuts#hideKeyboard()
262+ */
263+ @ Override
264+ public void hideKeyboard () {
265+ execute (HIDE_KEYBOARD );
266+ }
263267
264268 /**
265269 * @see InteractsWithFiles#pullFile(String)
@@ -285,22 +289,6 @@ public byte[] pullFolder(String remotePath) {
285289 return DatatypeConverter .parseBase64Binary (base64String );
286290 }
287291
288- /**
289- * @see DeviceActionShortcuts#hideKeyboard()
290- */
291- @ Override
292- public void hideKeyboard () {
293- execute (HIDE_KEYBOARD );
294- }
295-
296- /**
297- * @see InteractsWithApps#runAppInBackground(int)
298- */
299- @ Override
300- public void runAppInBackground (int seconds ) {
301- execute (RUN_APP_IN_BACKGROUND , ImmutableMap .of ("seconds" , seconds ));
302- }
303-
304292 /**
305293 * @see PerformsTouchActions#performTouchAction(TouchAction)
306294 */
@@ -448,49 +436,6 @@ public void zoom(int x, int y) {
448436 multiTouch .perform ();
449437 }
450438
451- /**
452- * @see InteractsWithApps#isAppInstalled(String)
453- */
454- @ Override
455- public boolean isAppInstalled (String bundleId ) {
456- Response response = execute (IS_APP_INSTALLED ,
457- ImmutableMap .of ("bundleId" , bundleId ));
458-
459- return Boolean .parseBoolean (response .getValue ().toString ());
460- }
461-
462- /**
463- * @see InteractsWithApps#installApp(String)
464- */
465- @ Override
466- public void installApp (String appPath ) {
467- execute (INSTALL_APP , ImmutableMap .of ("appPath" , appPath ));
468- }
469-
470- /**
471- * @see InteractsWithApps#removeApp(String)
472- */
473- @ Override
474- public void removeApp (String bundleId ) {
475- execute (REMOVE_APP , ImmutableMap .of ("bundleId" , bundleId ));
476- }
477-
478- /**
479- * @see InteractsWithApps#launchApp()
480- */
481- @ Override
482- public void launchApp () {
483- execute (LAUNCH_APP );
484- }
485-
486- /**
487- * @see InteractsWithApps#closeApp()
488- */
489- @ Override
490- public void closeApp () {
491- execute (CLOSE_APP );
492- }
493-
494439 /**
495440 * Get settings stored for this test session It's probably better to use a
496441 * convenience function, rather than use this function directly. Try finding
@@ -534,7 +479,18 @@ protected void setSetting(AppiumSetting setting, Object value) {
534479 setSettings (getCommandImmutableMap (setting .toString (), value ));
535480 }
536481
537- @ Override
482+ /**
483+ * Lock the device (bring it to the lock screen) for a given number of
484+ * seconds
485+ *
486+ * @param seconds
487+ * number of seconds to lock the screen for
488+ */
489+ public void lockScreen (int seconds ) {
490+ execute (LOCK , ImmutableMap .of ("seconds" , seconds ));
491+ }
492+
493+ @ Override
538494 public WebDriver context (String name ) {
539495 if (!_isNotNullOrEmpty (name )) {
540496 throw new IllegalArgumentException ("Must supply a context name" );
@@ -655,4 +611,20 @@ private static CommandInfo deleteC(String url) {
655611 public URL getRemoteAddress () {
656612 return remoteAddress ;
657613 }
614+
615+ /**
616+ * Checks if a string is null, empty, or whitespace.
617+ *
618+ * @param str
619+ * String to check.
620+ *
621+ * @return True if str is not null or empty.
622+ */
623+ protected static boolean _isNotNullOrEmpty (String str ) {
624+ return str != null && !str .isEmpty () && str .trim ().length () > 0 ;
625+ }
626+
627+ protected static boolean _isNotNullOrEmpty (Object ob ) {
628+ return ob != null ;
629+ }
658630}
0 commit comments