From 32c07bb6fa5cbc0d8e87400cd124a6df6c81cf92 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Tue, 18 Apr 2023 19:34:07 +0200 Subject: [PATCH 1/5] refactor: Use mobile extensions for app strings getters and keyboard commands --- .../java_client/CommandExecutionHelper.java | 5 ++ .../io/appium/java_client/HasAppStrings.java | 46 +++++++++++++++---- .../java_client/HasOnScreenKeyboard.java | 13 +++++- .../io/appium/java_client/HidesKeyboard.java | 13 +++++- .../java_client/HidesKeyboardWithKeyName.java | 20 +++++++- .../appium/java_client/InteractsWithApps.java | 17 +++++-- .../io/appium/java_client/PullsFiles.java | 14 ++++-- .../java_client/android/AndroidDriver.java | 8 +--- .../connection/HasNetworkConnection.java | 40 ++++++++++++++-- .../io/appium/java_client/ios/IOSDriver.java | 9 +--- 10 files changed, 146 insertions(+), 39 deletions(-) diff --git a/src/main/java/io/appium/java_client/CommandExecutionHelper.java b/src/main/java/io/appium/java_client/CommandExecutionHelper.java index e0a379e17..8bd053f36 100644 --- a/src/main/java/io/appium/java_client/CommandExecutionHelper.java +++ b/src/main/java/io/appium/java_client/CommandExecutionHelper.java @@ -28,20 +28,24 @@ public final class CommandExecutionHelper { + @Nullable public static T execute(ExecutesMethod executesMethod, Map.Entry> keyValuePair) { return handleResponse(executesMethod.execute(keyValuePair.getKey(), keyValuePair.getValue())); } + @Nullable public static T execute(ExecutesMethod executesMethod, String command) { return handleResponse(executesMethod.execute(command)); } + @Nullable private static T handleResponse(Response response) { //noinspection unchecked return response == null ? null : (T) response.getValue(); } + @Nullable public static T executeScript(ExecutesMethod executesMethod, String scriptName) { return executeScript(executesMethod, scriptName, null); } @@ -54,6 +58,7 @@ public static T executeScript(ExecutesMethod executesMethod, String scriptNa * @param args Extension script arguments (if present). * @return Script execution result. */ + @Nullable public static T executeScript( ExecutesMethod executesMethod, String scriptName, @Nullable Map args ) { diff --git a/src/main/java/io/appium/java_client/HasAppStrings.java b/src/main/java/io/appium/java_client/HasAppStrings.java index 1983d4667..d093e777f 100644 --- a/src/main/java/io/appium/java_client/HasAppStrings.java +++ b/src/main/java/io/appium/java_client/HasAppStrings.java @@ -16,6 +16,9 @@ package io.appium.java_client; +import com.google.common.collect.ImmutableMap; +import org.openqa.selenium.UnsupportedCommandException; + import java.util.AbstractMap; import java.util.Map; @@ -25,37 +28,62 @@ public interface HasAppStrings extends ExecutesMethod { /** * Get all defined Strings from an app for the default language. + * See the documentation for 'mobile: getAppStrings' extension for more details. * * @return a map with localized strings defined in the app */ default Map getAppStringMap() { - return CommandExecutionHelper.execute(this, GET_STRINGS); + try { + return CommandExecutionHelper.executeScript(this, "mobile: getAppStrings"); + } catch (UnsupportedCommandException e) { + // TODO: Remove the fallback + return CommandExecutionHelper.execute(this, GET_STRINGS); + } } /** * Get all defined Strings from an app for the specified language. + * See the documentation for 'mobile: getAppStrings' extension for more details. * * @param language strings language code * @return a map with localized strings defined in the app */ default Map getAppStringMap(String language) { - return CommandExecutionHelper.execute(this, new AbstractMap.SimpleEntry<>(GET_STRINGS, - prepareArguments("language", language))); + try { + return CommandExecutionHelper.executeScript(this, "mobile: getAppStrings", ImmutableMap.of( + "language", language + )); + } catch (UnsupportedCommandException e) { + // TODO: Remove the fallback + return CommandExecutionHelper.execute( + this, new AbstractMap.SimpleEntry<>(GET_STRINGS, prepareArguments("language", language)) + ); + } } /** * Get all defined Strings from an app for the specified language and - * strings filename. + * strings filename. See the documentation for 'mobile: getAppStrings' + * extension for more details. * * @param language strings language code - * @param stringFile strings filename + * @param stringFile strings filename. Ignored on Android * @return a map with localized strings defined in the app */ default Map getAppStringMap(String language, String stringFile) { - String[] parameters = new String[] {"language", "stringFile"}; - Object[] values = new Object[] {language, stringFile}; - return CommandExecutionHelper.execute(this, - new AbstractMap.SimpleEntry<>(GET_STRINGS, prepareArguments(parameters, values))); + try { + return CommandExecutionHelper.executeScript(this, "mobile: getAppStrings", ImmutableMap.of( + "language", language, + "stringFile", stringFile + )); + } catch (UnsupportedCommandException e) { + // TODO: Remove the fallback + String[] parameters = new String[]{"language", "stringFile"}; + Object[] values = new Object[]{language, stringFile}; + return CommandExecutionHelper.execute( + this, new AbstractMap.SimpleEntry<>(GET_STRINGS, prepareArguments(parameters, values)) + ); + } } } diff --git a/src/main/java/io/appium/java_client/HasOnScreenKeyboard.java b/src/main/java/io/appium/java_client/HasOnScreenKeyboard.java index 7a9d6febb..322c849db 100644 --- a/src/main/java/io/appium/java_client/HasOnScreenKeyboard.java +++ b/src/main/java/io/appium/java_client/HasOnScreenKeyboard.java @@ -1,15 +1,24 @@ package io.appium.java_client; +import org.openqa.selenium.UnsupportedCommandException; + +import static com.google.common.base.Preconditions.checkNotNull; import static io.appium.java_client.MobileCommand.isKeyboardShownCommand; public interface HasOnScreenKeyboard extends ExecutesMethod { /** - * Check if the keyboard is displayed. + * Check if the on-screen keyboard is displayed. + * See the documentation for 'mobile: isKeyboardShown' extension for more details. * * @return true if keyboard is displayed. False otherwise */ default boolean isKeyboardShown() { - return CommandExecutionHelper.execute(this, isKeyboardShownCommand()); + try { + return checkNotNull(CommandExecutionHelper.executeScript(this, "mobile: isKeyboardShown")); + } catch (UnsupportedCommandException e) { + // TODO: Remove the fallback + return checkNotNull(CommandExecutionHelper.execute(this, isKeyboardShownCommand())); + } } } diff --git a/src/main/java/io/appium/java_client/HidesKeyboard.java b/src/main/java/io/appium/java_client/HidesKeyboard.java index 5f292b0ce..6de1e0516 100644 --- a/src/main/java/io/appium/java_client/HidesKeyboard.java +++ b/src/main/java/io/appium/java_client/HidesKeyboard.java @@ -16,14 +16,25 @@ package io.appium.java_client; +import org.openqa.selenium.UnsupportedCommandException; + import static io.appium.java_client.MobileCommand.HIDE_KEYBOARD; public interface HidesKeyboard extends ExecutesMethod { /** * Hides the keyboard if it is showing. + * If the on-screen keyboard does not have any dedicated button that + * hides it then an error is going to be thrown. In such case you must emulate + * same actions an app user would do to hide the keyboard. + * See the documentation for 'mobile: hideKeyboard' extension for more details. */ default void hideKeyboard() { - execute(HIDE_KEYBOARD); + try { + CommandExecutionHelper.executeScript(this, "mobile: hideKeyboard"); + } catch (UnsupportedCommandException e) { + // TODO: Remove the fallback + CommandExecutionHelper.execute(this, HIDE_KEYBOARD); + } } } diff --git a/src/main/java/io/appium/java_client/HidesKeyboardWithKeyName.java b/src/main/java/io/appium/java_client/HidesKeyboardWithKeyName.java index 2549ad018..50a6c0aa5 100644 --- a/src/main/java/io/appium/java_client/HidesKeyboardWithKeyName.java +++ b/src/main/java/io/appium/java_client/HidesKeyboardWithKeyName.java @@ -16,19 +16,33 @@ package io.appium.java_client; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import org.openqa.selenium.UnsupportedCommandException; + import static io.appium.java_client.MobileCommand.hideKeyboardCommand; public interface HidesKeyboardWithKeyName extends HidesKeyboard { /** * Hides the keyboard by pressing the button specified by keyName if it is - * showing. + * showing. If the on-screen keyboard does not have any dedicated button that + * hides it then an error is going to be thrown. In such case you must emulate + * same actions an app user would do to hide the keyboard. + * See the documentation for 'mobile: hideKeyboard' extension for more details. * * @param keyName The button pressed by the mobile driver to attempt hiding the * keyboard. */ default void hideKeyboard(String keyName) { - CommandExecutionHelper.execute(this, hideKeyboardCommand(keyName)); + try { + CommandExecutionHelper.executeScript(this, "mobile: hideKeyboard", ImmutableMap.of( + "keys", ImmutableList.of(keyName) + )); + } catch (UnsupportedCommandException e) { + // TODO: Remove the fallback + CommandExecutionHelper.execute(this, hideKeyboardCommand(keyName)); + } } /** @@ -36,10 +50,12 @@ default void hideKeyboard(String keyName) { * depends on the way an app is implemented, no single strategy always * works. * + * @deprecated This API is deprecated and will be removed in the future. * @param strategy HideKeyboardStrategy. * @param keyName a String, representing the text displayed on the button of the * keyboard you want to press. For example: "Done". */ + @Deprecated default void hideKeyboard(String strategy, String keyName) { CommandExecutionHelper.execute(this, hideKeyboardCommand(strategy, keyName)); } diff --git a/src/main/java/io/appium/java_client/InteractsWithApps.java b/src/main/java/io/appium/java_client/InteractsWithApps.java index fc5e17ada..fedf957c8 100644 --- a/src/main/java/io/appium/java_client/InteractsWithApps.java +++ b/src/main/java/io/appium/java_client/InteractsWithApps.java @@ -30,6 +30,7 @@ import java.util.Map; import java.util.Optional; +import static com.google.common.base.Preconditions.checkNotNull; import static io.appium.java_client.MobileCommand.RUN_APP_IN_BACKGROUND; @SuppressWarnings("rawtypes") @@ -66,10 +67,12 @@ default void installApp(String appPath, @Nullable BaseInstallApplicationOptions * @return True if app is installed, false otherwise. */ default boolean isAppInstalled(String bundleId) { - return CommandExecutionHelper.executeScript(this, "mobile: isAppInstalled", ImmutableMap.of( + return checkNotNull( + CommandExecutionHelper.executeScript(this, "mobile: isAppInstalled", ImmutableMap.of( "bundleId", bundleId, "appId", bundleId - )); + )) + ); } /** @@ -107,7 +110,9 @@ default boolean removeApp(String bundleId, @Nullable BaseRemoveApplicationOption args.put("bundleId", bundleId); args.put("appId", bundleId); Optional.ofNullable(options).map(BaseOptions::build).ifPresent(args::putAll); - return CommandExecutionHelper.executeScript(this, "mobile: removeApp", args); + return checkNotNull( + CommandExecutionHelper.executeScript(this, "mobile: removeApp", args) + ); } /** @@ -144,10 +149,12 @@ default void activateApp(String bundleId, @Nullable BaseActivateApplicationOptio */ default ApplicationState queryAppState(String bundleId) { return ApplicationState.ofCode( + checkNotNull( CommandExecutionHelper.executeScript(this, "mobile: queryAppState", ImmutableMap.of( "bundleId", bundleId, "appId", bundleId )) + ) ); } @@ -174,6 +181,8 @@ default boolean terminateApp(String bundleId, @Nullable BaseTerminateApplication args.put("bundleId", bundleId); args.put("appId", bundleId); Optional.ofNullable(options).map(BaseOptions::build).ifPresent(args::putAll); - return CommandExecutionHelper.executeScript(this, "mobile: terminateApp", args); + return checkNotNull( + CommandExecutionHelper.executeScript(this, "mobile: terminateApp", args) + ); } } diff --git a/src/main/java/io/appium/java_client/PullsFiles.java b/src/main/java/io/appium/java_client/PullsFiles.java index 2dd403771..0042b0997 100644 --- a/src/main/java/io/appium/java_client/PullsFiles.java +++ b/src/main/java/io/appium/java_client/PullsFiles.java @@ -21,6 +21,8 @@ import java.nio.charset.StandardCharsets; import java.util.Base64; +import static com.google.common.base.Preconditions.checkNotNull; + public interface PullsFiles extends ExecutesMethod { /** @@ -36,9 +38,11 @@ public interface PullsFiles extends ExecutesMethod { * @return A byte array of Base64 encoded data. */ default byte[] pullFile(String remotePath) { - String base64String = CommandExecutionHelper.executeScript(this, "mobile: pullFile", ImmutableMap.of( + String base64String = checkNotNull( + CommandExecutionHelper.executeScript(this, "mobile: pullFile", ImmutableMap.of( "remotePath", remotePath - )); + )) + ); return Base64.getDecoder().decode(base64String.getBytes(StandardCharsets.UTF_8)); } @@ -55,9 +59,11 @@ default byte[] pullFile(String remotePath) { * @return A byte array of Base64 encoded zip archive data. */ default byte[] pullFolder(String remotePath) { - String base64String = CommandExecutionHelper.executeScript(this, "mobile: pullFolder", ImmutableMap.of( + String base64String = checkNotNull( + CommandExecutionHelper.executeScript(this, "mobile: pullFolder", ImmutableMap.of( "remotePath", remotePath - )); + )) + ); return Base64.getDecoder().decode(base64String.getBytes(StandardCharsets.UTF_8)); } diff --git a/src/main/java/io/appium/java_client/android/AndroidDriver.java b/src/main/java/io/appium/java_client/android/AndroidDriver.java index 0fc1c5917..c7290882b 100644 --- a/src/main/java/io/appium/java_client/android/AndroidDriver.java +++ b/src/main/java/io/appium/java_client/android/AndroidDriver.java @@ -16,7 +16,6 @@ package io.appium.java_client.android; -import com.google.common.collect.ImmutableMap; import io.appium.java_client.AppiumClientConfig; import io.appium.java_client.AppiumDriver; import io.appium.java_client.CommandExecutionHelper; @@ -50,13 +49,10 @@ import org.openqa.selenium.remote.http.HttpClient; import java.net.URL; -import java.util.Collections; -import java.util.Map; import static io.appium.java_client.android.AndroidMobileCommandHelper.endTestCoverageCommand; import static io.appium.java_client.android.AndroidMobileCommandHelper.openNotificationsCommand; import static io.appium.java_client.android.AndroidMobileCommandHelper.toggleLocationServicesCommand; -import static org.openqa.selenium.remote.DriverCommand.EXECUTE_SCRIPT; /** * Android driver implementation. @@ -275,11 +271,9 @@ public void toggleLocationServices() { CommandExecutionHelper.execute(this, toggleLocationServicesCommand()); } - @SuppressWarnings("unchecked") @Override public AndroidBatteryInfo getBatteryInfo() { - return new AndroidBatteryInfo((Map) execute(EXECUTE_SCRIPT, ImmutableMap.of( - "script", "mobile: batteryInfo", "args", Collections.emptyList())).getValue()); + return new AndroidBatteryInfo(CommandExecutionHelper.executeScript(this, "mobile: batteryInfo")); } @Override diff --git a/src/main/java/io/appium/java_client/android/connection/HasNetworkConnection.java b/src/main/java/io/appium/java_client/android/connection/HasNetworkConnection.java index 7f76340a3..f77bf7d3a 100644 --- a/src/main/java/io/appium/java_client/android/connection/HasNetworkConnection.java +++ b/src/main/java/io/appium/java_client/android/connection/HasNetworkConnection.java @@ -16,9 +16,14 @@ package io.appium.java_client.android.connection; +import com.google.common.collect.ImmutableMap; import io.appium.java_client.CommandExecutionHelper; import io.appium.java_client.ExecutesMethod; +import org.openqa.selenium.UnsupportedCommandException; +import java.util.Map; + +import static com.google.common.base.Preconditions.checkNotNull; import static io.appium.java_client.android.AndroidMobileCommandHelper.getNetworkConnectionCommand; import static io.appium.java_client.android.AndroidMobileCommandHelper.setConnectionCommand; @@ -31,8 +36,21 @@ public interface HasNetworkConnection extends ExecutesMethod { * @return Connection object, which represents the resulting state */ default ConnectionState setConnection(ConnectionState connection) { - return new ConnectionState(CommandExecutionHelper.execute(this, - setConnectionCommand(connection.getBitMask()))); + try { + CommandExecutionHelper.executeScript(this, "mobile: setConnectivity", ImmutableMap.of( + "wifi", connection.isWiFiEnabled(), + "data", connection.isDataEnabled(), + "airplaneMode", connection.isAirplaneModeEnabled() + )); + return getConnection(); + } catch (UnsupportedCommandException e) { + // TODO: Remove the fallback + return new ConnectionState( + checkNotNull( + CommandExecutionHelper.execute(this, setConnectionCommand(connection.getBitMask())) + ) + ); + } } /** @@ -41,6 +59,22 @@ default ConnectionState setConnection(ConnectionState connection) { * @return Connection object, which lets you to inspect the current status */ default ConnectionState getConnection() { - return new ConnectionState(CommandExecutionHelper.execute(this, getNetworkConnectionCommand())); + try { + Map result = checkNotNull( + CommandExecutionHelper.executeScript(this, "mobile: getConnectivity") + ); + return new ConnectionState( + ((boolean) result.get("wifi") ? ConnectionState.WIFI_MASK : 0) + | ((boolean) result.get("data") ? ConnectionState.DATA_MASK : 0) + | ((boolean) result.get("airplaneMode") ? ConnectionState.AIRPLANE_MODE_MASK : 0) + ); + } catch (UnsupportedCommandException e) { + // TODO: Remove the fallback + return new ConnectionState( + checkNotNull( + CommandExecutionHelper.execute(this, getNetworkConnectionCommand()) + ) + ); + } } } diff --git a/src/main/java/io/appium/java_client/ios/IOSDriver.java b/src/main/java/io/appium/java_client/ios/IOSDriver.java index ad00a5f90..2b735d649 100644 --- a/src/main/java/io/appium/java_client/ios/IOSDriver.java +++ b/src/main/java/io/appium/java_client/ios/IOSDriver.java @@ -16,9 +16,9 @@ package io.appium.java_client.ios; -import com.google.common.collect.ImmutableMap; import io.appium.java_client.AppiumClientConfig; import io.appium.java_client.AppiumDriver; +import io.appium.java_client.CommandExecutionHelper; import io.appium.java_client.HasAppStrings; import io.appium.java_client.HasDeviceTime; import io.appium.java_client.HasOnScreenKeyboard; @@ -50,11 +50,8 @@ import org.openqa.selenium.remote.http.HttpClient; import java.net.URL; -import java.util.Collections; -import java.util.Map; import static io.appium.java_client.MobileCommand.prepareArguments; -import static org.openqa.selenium.remote.DriverCommand.EXECUTE_SCRIPT; /** * iOS driver implementation. @@ -247,11 +244,9 @@ public IOSDriver(Capabilities capabilities) { return new InnerTargetLocator(); } - @SuppressWarnings("unchecked") @Override public IOSBatteryInfo getBatteryInfo() { - return new IOSBatteryInfo((Map) execute(EXECUTE_SCRIPT, ImmutableMap.of( - "script", "mobile: batteryInfo", "args", Collections.emptyList())).getValue()); + return new IOSBatteryInfo(CommandExecutionHelper.executeScript(this, "mobile: batteryInfo")); } private class InnerTargetLocator extends RemoteTargetLocator { From 4bb053073fa7235b9b1198d9bf680666b92963d6 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Tue, 18 Apr 2023 19:38:08 +0200 Subject: [PATCH 2/5] Checkstyle --- .../java/io/appium/java_client/HidesKeyboardWithKeyName.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/appium/java_client/HidesKeyboardWithKeyName.java b/src/main/java/io/appium/java_client/HidesKeyboardWithKeyName.java index 50a6c0aa5..c7e809911 100644 --- a/src/main/java/io/appium/java_client/HidesKeyboardWithKeyName.java +++ b/src/main/java/io/appium/java_client/HidesKeyboardWithKeyName.java @@ -37,7 +37,7 @@ public interface HidesKeyboardWithKeyName extends HidesKeyboard { default void hideKeyboard(String keyName) { try { CommandExecutionHelper.executeScript(this, "mobile: hideKeyboard", ImmutableMap.of( - "keys", ImmutableList.of(keyName) + "keys", ImmutableList.of(keyName) )); } catch (UnsupportedCommandException e) { // TODO: Remove the fallback @@ -50,10 +50,10 @@ default void hideKeyboard(String keyName) { * depends on the way an app is implemented, no single strategy always * works. * - * @deprecated This API is deprecated and will be removed in the future. * @param strategy HideKeyboardStrategy. * @param keyName a String, representing the text displayed on the button of the * keyboard you want to press. For example: "Done". + * @deprecated This API is deprecated and will be removed in the future. */ @Deprecated default void hideKeyboard(String strategy, String keyName) { From b0192ba4c7fd159d0960cbfe6c8ddb4340768b32 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Wed, 19 Apr 2023 09:32:52 +0200 Subject: [PATCH 3/5] Always send args --- .../appium/java_client/CommandExecutionHelper.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/appium/java_client/CommandExecutionHelper.java b/src/main/java/io/appium/java_client/CommandExecutionHelper.java index 8bd053f36..5c08a28a6 100644 --- a/src/main/java/io/appium/java_client/CommandExecutionHelper.java +++ b/src/main/java/io/appium/java_client/CommandExecutionHelper.java @@ -30,7 +30,7 @@ public final class CommandExecutionHelper { @Nullable public static T execute(ExecutesMethod executesMethod, - Map.Entry> keyValuePair) { + Map.Entry> keyValuePair) { return handleResponse(executesMethod.execute(keyValuePair.getKey(), keyValuePair.getValue())); } @@ -54,8 +54,8 @@ public static T executeScript(ExecutesMethod executesMethod, String scriptNa * Simplifies arguments preparation for the script execution command. * * @param executesMethod Method executor instance. - * @param scriptName Extension script name. - * @param args Extension script arguments (if present). + * @param scriptName Extension script name. + * @param args Extension script arguments (if present). * @return Script execution result. */ @Nullable @@ -64,9 +64,10 @@ public static T executeScript( ) { Map payload = new HashMap<>(); payload.put("script", scriptName); - if (args != null) { - payload.put("args", args.isEmpty() ? Collections.emptyList() : Collections.singletonList(args)); - } + payload.put( + "args", + (args == null || args.isEmpty()) ? Collections.emptyList() : Collections.singletonList(args) + ); return execute(executesMethod, new AbstractMap.SimpleEntry<>(EXECUTE_SCRIPT, payload)); } } From 98d75b5c8130d3556e5916f1cb0494c89f892508 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Wed, 19 Apr 2023 09:35:34 +0200 Subject: [PATCH 4/5] tune map --- .../java_client/CommandExecutionHelper.java | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/main/java/io/appium/java_client/CommandExecutionHelper.java b/src/main/java/io/appium/java_client/CommandExecutionHelper.java index 5c08a28a6..0a41f17b6 100644 --- a/src/main/java/io/appium/java_client/CommandExecutionHelper.java +++ b/src/main/java/io/appium/java_client/CommandExecutionHelper.java @@ -16,12 +16,12 @@ package io.appium.java_client; +import com.google.common.collect.ImmutableMap; import org.openqa.selenium.remote.Response; import javax.annotation.Nullable; import java.util.AbstractMap; import java.util.Collections; -import java.util.HashMap; import java.util.Map; import static org.openqa.selenium.remote.DriverCommand.EXECUTE_SCRIPT; @@ -29,8 +29,9 @@ public final class CommandExecutionHelper { @Nullable - public static T execute(ExecutesMethod executesMethod, - Map.Entry> keyValuePair) { + public static T execute( + ExecutesMethod executesMethod, Map.Entry> keyValuePair + ) { return handleResponse(executesMethod.execute(keyValuePair.getKey(), keyValuePair.getValue())); } @@ -62,12 +63,9 @@ public static T executeScript(ExecutesMethod executesMethod, String scriptNa public static T executeScript( ExecutesMethod executesMethod, String scriptName, @Nullable Map args ) { - Map payload = new HashMap<>(); - payload.put("script", scriptName); - payload.put( - "args", - (args == null || args.isEmpty()) ? Collections.emptyList() : Collections.singletonList(args) - ); - return execute(executesMethod, new AbstractMap.SimpleEntry<>(EXECUTE_SCRIPT, payload)); + return execute(executesMethod, new AbstractMap.SimpleEntry<>(EXECUTE_SCRIPT, ImmutableMap.of( + "script", scriptName, + "args", (args == null || args.isEmpty()) ? Collections.emptyList() : Collections.singletonList(args) + ))); } } From a7cdb079f7082b4a855ba61a13076cdb99ff3428 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Wed, 19 Apr 2023 09:41:05 +0200 Subject: [PATCH 5/5] simplify more --- .../java/io/appium/java_client/HasBrowserCheck.java | 12 ++++-------- .../java_client/android/ListensToLogcatMessages.java | 10 +++------- .../java_client/ios/ListensToSyslogMessages.java | 10 +++------- 3 files changed, 10 insertions(+), 22 deletions(-) diff --git a/src/main/java/io/appium/java_client/HasBrowserCheck.java b/src/main/java/io/appium/java_client/HasBrowserCheck.java index efa5c6c62..6b00a8d5d 100644 --- a/src/main/java/io/appium/java_client/HasBrowserCheck.java +++ b/src/main/java/io/appium/java_client/HasBrowserCheck.java @@ -1,17 +1,14 @@ package io.appium.java_client; -import com.google.common.collect.ImmutableMap; import io.appium.java_client.internal.CapabilityHelpers; import org.openqa.selenium.ContextAware; import org.openqa.selenium.HasCapabilities; import org.openqa.selenium.WebDriverException; import org.openqa.selenium.remote.CapabilityType; -import java.util.Collections; - +import static com.google.common.base.Preconditions.checkNotNull; import static org.apache.commons.lang3.StringUtils.containsIgnoreCase; import static org.apache.commons.lang3.StringUtils.isBlank; -import static org.openqa.selenium.remote.DriverCommand.EXECUTE_SCRIPT; public interface HasBrowserCheck extends ExecutesMethod, HasCapabilities { /** @@ -24,10 +21,9 @@ default boolean isBrowser() { CapabilityType.BROWSER_NAME, String.class); if (!isBlank(browserName)) { try { - return (boolean) execute(EXECUTE_SCRIPT, ImmutableMap.of( - "script", "return !!window.navigator;", - "args", Collections.emptyList() - )).getValue(); + return checkNotNull( + CommandExecutionHelper.executeScript(this, "return !!window.navigator;") + ); } catch (WebDriverException ign) { // ignore } diff --git a/src/main/java/io/appium/java_client/android/ListensToLogcatMessages.java b/src/main/java/io/appium/java_client/android/ListensToLogcatMessages.java index 8f9cf1b38..8d16bf7de 100644 --- a/src/main/java/io/appium/java_client/android/ListensToLogcatMessages.java +++ b/src/main/java/io/appium/java_client/android/ListensToLogcatMessages.java @@ -16,18 +16,16 @@ package io.appium.java_client.android; -import com.google.common.collect.ImmutableMap; +import io.appium.java_client.CommandExecutionHelper; import io.appium.java_client.ExecutesMethod; import io.appium.java_client.ws.StringWebSocketClient; import org.openqa.selenium.remote.RemoteWebDriver; import java.net.URI; import java.net.URISyntaxException; -import java.util.Collections; import java.util.function.Consumer; import static io.appium.java_client.service.local.AppiumServiceBuilder.DEFAULT_APPIUM_PORT; -import static org.openqa.selenium.remote.DriverCommand.EXECUTE_SCRIPT; public interface ListensToLogcatMessages extends ExecutesMethod { StringWebSocketClient getLogcatClient(); @@ -58,8 +56,7 @@ default void startLogcatBroadcast(String host) { * @param port the port of the host where Appium server is running */ default void startLogcatBroadcast(String host, int port) { - execute(EXECUTE_SCRIPT, ImmutableMap.of("script", "mobile: startLogsBroadcast", - "args", Collections.emptyList())); + CommandExecutionHelper.executeScript(this, "mobile: startLogsBroadcast"); final URI endpointUri; try { endpointUri = new URI(String.format("ws://%s:%s/ws/session/%s/appium/device/logcat", @@ -132,7 +129,6 @@ default void removeAllLogcatListeners() { */ default void stopLogcatBroadcast() { removeAllLogcatListeners(); - execute(EXECUTE_SCRIPT, ImmutableMap.of("script", "mobile: stopLogsBroadcast", - "args", Collections.emptyList())); + CommandExecutionHelper.executeScript(this, "mobile: stopLogsBroadcast"); } } diff --git a/src/main/java/io/appium/java_client/ios/ListensToSyslogMessages.java b/src/main/java/io/appium/java_client/ios/ListensToSyslogMessages.java index 3341f753d..ef0bd3f9d 100644 --- a/src/main/java/io/appium/java_client/ios/ListensToSyslogMessages.java +++ b/src/main/java/io/appium/java_client/ios/ListensToSyslogMessages.java @@ -16,18 +16,16 @@ package io.appium.java_client.ios; -import com.google.common.collect.ImmutableMap; +import io.appium.java_client.CommandExecutionHelper; import io.appium.java_client.ExecutesMethod; import io.appium.java_client.ws.StringWebSocketClient; import org.openqa.selenium.remote.RemoteWebDriver; import java.net.URI; import java.net.URISyntaxException; -import java.util.Collections; import java.util.function.Consumer; import static io.appium.java_client.service.local.AppiumServiceBuilder.DEFAULT_APPIUM_PORT; -import static org.openqa.selenium.remote.DriverCommand.EXECUTE_SCRIPT; public interface ListensToSyslogMessages extends ExecutesMethod { @@ -59,8 +57,7 @@ default void startSyslogBroadcast(String host) { * @param port the port of the host where Appium server is running */ default void startSyslogBroadcast(String host, int port) { - execute(EXECUTE_SCRIPT, ImmutableMap.of("script", "mobile: startLogsBroadcast", - "args", Collections.emptyList())); + CommandExecutionHelper.executeScript(this, "mobile: startLogsBroadcast"); final URI endpointUri; try { endpointUri = new URI(String.format("ws://%s:%s/ws/session/%s/appium/device/syslog", @@ -132,7 +129,6 @@ default void removeAllSyslogListeners() { * Stops syslog messages broadcast via web socket. */ default void stopSyslogBroadcast() { - execute(EXECUTE_SCRIPT, ImmutableMap.of("script", "mobile: stopLogsBroadcast", - "args", Collections.emptyList())); + CommandExecutionHelper.executeScript(this, "mobile: stopLogsBroadcast"); } }