From e33e3a076a4da35cec02e6650fa5e1cfe1f56675 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Sun, 24 Oct 2021 16:40:21 +0200 Subject: [PATCH 1/6] Start adding UiAutomator2 options --- .../options/SupportsSystemPortOption.java | 52 ++ .../android/options/UiAutomator2Options.java | 75 ++- .../app/SupportsAllowTestPackagesOption.java | 61 +++ .../SupportsAndroidInstallTimeoutOption.java | 52 ++ .../app/SupportsAppActivityOption.java | 48 ++ .../app/SupportsAppDurationOption.java | 52 ++ .../options/app/SupportsAppPackageOption.java | 48 ++ .../app/SupportsAppWaitActivityOption.java | 49 ++ .../app/SupportsAppWaitForLaunchOption.java | 52 ++ .../app/SupportsAppWaitPackageOption.java | 49 ++ .../SupportsAutoGrantPermissionsOption.java | 62 +++ .../app/SupportsEnforceAppInstallOption.java | 61 +++ .../app/SupportsIntentActionOption.java | 49 ++ .../app/SupportsIntentCategoryOption.java | 49 ++ .../app/SupportsIntentFlagsOption.java | 49 ++ ...SupportsOptionalIntentArgumentsOption.java | 49 ++ .../SupportsRemoteAppsCacheLimitOption.java | 54 ++ .../SupportsUninstallOtherPackagesOption.java | 48 ++ .../SupportsDisableWindowAnimationOption.java | 60 +++ ...upportsSkipDeviceInitializationOption.java | 60 +++ .../SupportsSkipServerInstallationOption.java | 69 +++ ...iautomator2ServerInstallTimeoutOption.java | 52 ++ ...Uiautomator2ServerLaunchTimeoutOption.java | 52 ++ ...tsUiautomator2ServerReadTimeoutOption.java | 54 ++ .../internal/CapabilityHelpers.java | 52 ++ .../java_client/remote/MobileOptions.java | 486 +++++++++++++++++- .../remote/options/BaseOptions.java | 221 +------- .../remote/options/SupportsAppOption.java | 19 +- .../options/SupportsAutoWebViewOption.java | 20 +- .../options/SupportsAutomationNameOption.java | 45 ++ .../SupportsClearSystemFilesOption.java | 15 +- .../options/SupportsDeviceNameOption.java | 13 +- ...upportsEnablePerformanceLoggingOption.java | 19 +- .../options/SupportsEventTimingsOption.java | 56 ++ .../options/SupportsFullResetOption.java | 56 ++ .../options/SupportsLanguageOption.java | 12 +- .../remote/options/SupportsLocaleOption.java | 14 +- .../SupportsNewCommandTimeoutOption.java | 49 ++ .../remote/options/SupportsNoResetOption.java | 56 ++ .../options/SupportsOrientationOption.java | 20 +- .../options/SupportsOtherAppsOption.java | 15 +- .../SupportsPlatformVersionOption.java | 45 ++ ...rtsPrintPageSourceOnFindFailureOption.java | 58 +++ .../remote/options/SupportsUdidOption.java | 15 +- 44 files changed, 2298 insertions(+), 294 deletions(-) create mode 100644 src/main/java/io/appium/java_client/android/options/SupportsSystemPortOption.java create mode 100644 src/main/java/io/appium/java_client/android/options/app/SupportsAllowTestPackagesOption.java create mode 100644 src/main/java/io/appium/java_client/android/options/app/SupportsAndroidInstallTimeoutOption.java create mode 100644 src/main/java/io/appium/java_client/android/options/app/SupportsAppActivityOption.java create mode 100644 src/main/java/io/appium/java_client/android/options/app/SupportsAppDurationOption.java create mode 100644 src/main/java/io/appium/java_client/android/options/app/SupportsAppPackageOption.java create mode 100644 src/main/java/io/appium/java_client/android/options/app/SupportsAppWaitActivityOption.java create mode 100644 src/main/java/io/appium/java_client/android/options/app/SupportsAppWaitForLaunchOption.java create mode 100644 src/main/java/io/appium/java_client/android/options/app/SupportsAppWaitPackageOption.java create mode 100644 src/main/java/io/appium/java_client/android/options/app/SupportsAutoGrantPermissionsOption.java create mode 100644 src/main/java/io/appium/java_client/android/options/app/SupportsEnforceAppInstallOption.java create mode 100644 src/main/java/io/appium/java_client/android/options/app/SupportsIntentActionOption.java create mode 100644 src/main/java/io/appium/java_client/android/options/app/SupportsIntentCategoryOption.java create mode 100644 src/main/java/io/appium/java_client/android/options/app/SupportsIntentFlagsOption.java create mode 100644 src/main/java/io/appium/java_client/android/options/app/SupportsOptionalIntentArgumentsOption.java create mode 100644 src/main/java/io/appium/java_client/android/options/app/SupportsRemoteAppsCacheLimitOption.java create mode 100644 src/main/java/io/appium/java_client/android/options/app/SupportsUninstallOtherPackagesOption.java create mode 100644 src/main/java/io/appium/java_client/android/options/server/SupportsDisableWindowAnimationOption.java create mode 100644 src/main/java/io/appium/java_client/android/options/server/SupportsSkipDeviceInitializationOption.java create mode 100644 src/main/java/io/appium/java_client/android/options/server/SupportsSkipServerInstallationOption.java create mode 100644 src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerInstallTimeoutOption.java create mode 100644 src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerLaunchTimeoutOption.java create mode 100644 src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerReadTimeoutOption.java create mode 100644 src/main/java/io/appium/java_client/remote/options/SupportsAutomationNameOption.java create mode 100644 src/main/java/io/appium/java_client/remote/options/SupportsEventTimingsOption.java create mode 100644 src/main/java/io/appium/java_client/remote/options/SupportsFullResetOption.java create mode 100644 src/main/java/io/appium/java_client/remote/options/SupportsNewCommandTimeoutOption.java create mode 100644 src/main/java/io/appium/java_client/remote/options/SupportsNoResetOption.java create mode 100644 src/main/java/io/appium/java_client/remote/options/SupportsPlatformVersionOption.java create mode 100644 src/main/java/io/appium/java_client/remote/options/SupportsPrintPageSourceOnFindFailureOption.java diff --git a/src/main/java/io/appium/java_client/android/options/SupportsSystemPortOption.java b/src/main/java/io/appium/java_client/android/options/SupportsSystemPortOption.java new file mode 100644 index 000000000..0f1128b07 --- /dev/null +++ b/src/main/java/io/appium/java_client/android/options/SupportsSystemPortOption.java @@ -0,0 +1,52 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.java_client.android.options; + +import io.appium.java_client.remote.options.BaseOptions; +import io.appium.java_client.remote.options.CanSetCapability; +import org.openqa.selenium.Capabilities; + +import java.util.Optional; + +import static io.appium.java_client.internal.CapabilityHelpers.toSafeInteger; + +public interface SupportsSystemPortOption> extends + Capabilities, CanSetCapability { + String SYSTEM_PORT_OPTION = "systemPort"; + + /** + * The number of the port the UiAutomator2 server is listening on. + * By default, the first free port from 8200..8299 range is selected. + * It is recommended to set this value if you are running parallel + * tests on the same machine. + * + * @param port port number in range 0..65535 + * @return self instance for chaining. + */ + default T setSystemPort(int port) { + return amend(SYSTEM_PORT_OPTION, port); + } + + /** + * Get the system port value. + * + * @return System port value + */ + default Optional getSystemPort() { + return Optional.ofNullable(toSafeInteger(getCapability(SYSTEM_PORT_OPTION))); + } +} diff --git a/src/main/java/io/appium/java_client/android/options/UiAutomator2Options.java b/src/main/java/io/appium/java_client/android/options/UiAutomator2Options.java index 3514db845..cb6ac52f4 100644 --- a/src/main/java/io/appium/java_client/android/options/UiAutomator2Options.java +++ b/src/main/java/io/appium/java_client/android/options/UiAutomator2Options.java @@ -16,6 +16,28 @@ package io.appium.java_client.android.options; +import io.appium.java_client.android.options.app.SupportsAllowTestPackagesOption; +import io.appium.java_client.android.options.app.SupportsAndroidInstallTimeoutOption; +import io.appium.java_client.android.options.app.SupportsAppActivityOption; +import io.appium.java_client.android.options.app.SupportsAppDurationOption; +import io.appium.java_client.android.options.app.SupportsAppPackageOption; +import io.appium.java_client.android.options.app.SupportsAppWaitActivityOption; +import io.appium.java_client.android.options.app.SupportsAppWaitForLaunchOption; +import io.appium.java_client.android.options.app.SupportsAppWaitPackageOption; +import io.appium.java_client.android.options.app.SupportsAutoGrantPermissionsOption; +import io.appium.java_client.android.options.app.SupportsEnforceAppInstallOption; +import io.appium.java_client.android.options.app.SupportsIntentActionOption; +import io.appium.java_client.android.options.app.SupportsIntentCategoryOption; +import io.appium.java_client.android.options.app.SupportsIntentFlagsOption; +import io.appium.java_client.android.options.app.SupportsOptionalIntentArgumentsOption; +import io.appium.java_client.android.options.app.SupportsRemoteAppsCacheLimitOption; +import io.appium.java_client.android.options.app.SupportsUninstallOtherPackagesOption; +import io.appium.java_client.android.options.server.SupportsDisableWindowAnimationOption; +import io.appium.java_client.android.options.server.SupportsSkipDeviceInitializationOption; +import io.appium.java_client.android.options.server.SupportsSkipServerInstallationOption; +import io.appium.java_client.android.options.server.SupportsUiautomator2ServerInstallTimeoutOption; +import io.appium.java_client.android.options.server.SupportsUiautomator2ServerLaunchTimeoutOption; +import io.appium.java_client.android.options.server.SupportsUiautomator2ServerReadTimeoutOption; import io.appium.java_client.remote.AutomationName; import io.appium.java_client.remote.MobilePlatform; import io.appium.java_client.remote.options.BaseOptions; @@ -31,12 +53,55 @@ import io.appium.java_client.remote.options.SupportsUdidOption; import org.openqa.selenium.Capabilities; +/** + * https://github.com/appium/appium-uiautomator2-driver#capabilities + */ public class UiAutomator2Options extends BaseOptions implements - SupportsAppOption, SupportsAutoWebViewOption, - SupportsClearSystemFilesOption, SupportsDeviceNameOption, - SupportsEnablePerformanceLoggingOption, SupportsLanguageOption, - SupportsLocaleOption, SupportsOrientationOption, - SupportsOtherAppsOption, SupportsUdidOption { + // TODO: ADB options: https://github.com/appium/appium-uiautomator2-driver#adb + // TODO: AVD options: https://github.com/appium/appium-uiautomator2-driver#emulator-android-virtual-device + // TODO: App signing options: https://github.com/appium/appium-uiautomator2-driver#app-signing + // TODO: Device locking options: https://github.com/appium/appium-uiautomator2-driver#device-locking + // TODO: MJPEG options: https://github.com/appium/appium-uiautomator2-driver#mjpeg + // TODO: Web Context options: https://github.com/appium/appium-uiautomator2-driver#web-context + // TODO: Other options: https://github.com/appium/appium-uiautomator2-driver#other + // TODO: Shared options + SupportsAutoWebViewOption, + // General options: https://github.com/appium/appium-uiautomator2-driver#general + SupportsDeviceNameOption, + SupportsUdidOption, + // Driver/Server options: https://github.com/appium/appium-uiautomator2-driver#driverserver + SupportsSystemPortOption, + SupportsSkipServerInstallationOption, + SupportsUiautomator2ServerLaunchTimeoutOption, + SupportsUiautomator2ServerInstallTimeoutOption, + SupportsUiautomator2ServerReadTimeoutOption, + SupportsDisableWindowAnimationOption, + SupportsSkipDeviceInitializationOption, + SupportsOrientationOption, + SupportsClearSystemFilesOption, + SupportsEnablePerformanceLoggingOption, + // App options: https://github.com/appium/appium-uiautomator2-driver#app + SupportsAppOption, + SupportsAppPackageOption, + SupportsAppActivityOption, + SupportsAppWaitActivityOption, + SupportsAppWaitPackageOption, + SupportsAppDurationOption, + SupportsAndroidInstallTimeoutOption, + SupportsAppWaitForLaunchOption, + SupportsIntentCategoryOption, + SupportsIntentActionOption, + SupportsIntentFlagsOption, + SupportsOptionalIntentArgumentsOption, + SupportsAutoGrantPermissionsOption, + SupportsOtherAppsOption, + SupportsUninstallOtherPackagesOption, + SupportsAllowTestPackagesOption, + SupportsRemoteAppsCacheLimitOption, + SupportsEnforceAppInstallOption, + // TODO: App localization options: https://github.com/appium/appium-uiautomator2-driver#app-localization + SupportsLanguageOption, + SupportsLocaleOption { public UiAutomator2Options() { setCommonOptions(); } diff --git a/src/main/java/io/appium/java_client/android/options/app/SupportsAllowTestPackagesOption.java b/src/main/java/io/appium/java_client/android/options/app/SupportsAllowTestPackagesOption.java new file mode 100644 index 000000000..7bf73aaa2 --- /dev/null +++ b/src/main/java/io/appium/java_client/android/options/app/SupportsAllowTestPackagesOption.java @@ -0,0 +1,61 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.java_client.android.options.app; + +import io.appium.java_client.remote.options.BaseOptions; +import io.appium.java_client.remote.options.CanSetCapability; +import org.openqa.selenium.Capabilities; + +import java.util.Optional; + +import static io.appium.java_client.internal.CapabilityHelpers.toSafeBoolean; + +public interface SupportsAllowTestPackagesOption> extends + Capabilities, CanSetCapability { + String ALLOW_TEST_PACKAGES_OPTION = "allowTestPackages"; + + /** + * Enables usage of packages built with the test flag for + * the automated testing (literally adds -t flag to the adb install command). + * + * @return self instance for chaining. + */ + default T setAllowTestPackages() { + return amend(ALLOW_TEST_PACKAGES_OPTION, true); + } + + /** + * If set to true then it would be possible to use packages built with the test flag for + * the automated testing (literally adds -t flag to the adb install command). false by default. + * + * @param value True to allow test packages installation. + * @return self instance for chaining. + */ + default T setAllowTestPackages(boolean value) { + return amend(ALLOW_TEST_PACKAGES_OPTION, value); + } + + /** + * Get whether it is possible to use packages built with the test flag for + * the automated testing (literally adds -t flag to the adb install command). + * + * @return True or false. + */ + default Optional doesAllowTestPackages() { + return Optional.ofNullable(toSafeBoolean(getCapability(ALLOW_TEST_PACKAGES_OPTION))); + } +} diff --git a/src/main/java/io/appium/java_client/android/options/app/SupportsAndroidInstallTimeoutOption.java b/src/main/java/io/appium/java_client/android/options/app/SupportsAndroidInstallTimeoutOption.java new file mode 100644 index 000000000..b24d48b11 --- /dev/null +++ b/src/main/java/io/appium/java_client/android/options/app/SupportsAndroidInstallTimeoutOption.java @@ -0,0 +1,52 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.java_client.android.options.app; + +import io.appium.java_client.remote.options.BaseOptions; +import io.appium.java_client.remote.options.CanSetCapability; +import org.openqa.selenium.Capabilities; + +import java.time.Duration; +import java.util.Optional; + +import static io.appium.java_client.internal.CapabilityHelpers.toSafeLong; + +public interface SupportsAndroidInstallTimeoutOption> extends + Capabilities, CanSetCapability { + String ANDROID_INSTALL_TIMEOUT_OPTION = "androidInstallTimeout"; + + /** + * Maximum amount of time to wait until the application under test is installed. + * 90000 ms by default + * + * @param installTimeout App install timeout. + * @return self instance for chaining. + */ + default T setAndroidInstallTimeout(Duration installTimeout) { + return amend(ANDROID_INSTALL_TIMEOUT_OPTION, installTimeout.toMillis()); + } + + /** + * Get maximum amount of time to wait until the application under test is installed. + * + * @return Timeout value. + */ + default Optional getAndroidInstallTimeout() { + Long value = toSafeLong(getCapability(ANDROID_INSTALL_TIMEOUT_OPTION)); + return Optional.ofNullable(value == null ? null : Duration.ofMillis(value)); + } +} diff --git a/src/main/java/io/appium/java_client/android/options/app/SupportsAppActivityOption.java b/src/main/java/io/appium/java_client/android/options/app/SupportsAppActivityOption.java new file mode 100644 index 000000000..6e1e544bd --- /dev/null +++ b/src/main/java/io/appium/java_client/android/options/app/SupportsAppActivityOption.java @@ -0,0 +1,48 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.java_client.android.options.app; + +import io.appium.java_client.remote.options.BaseOptions; +import io.appium.java_client.remote.options.CanSetCapability; +import org.openqa.selenium.Capabilities; + +import java.util.Optional; + +public interface SupportsAppActivityOption> extends + Capabilities, CanSetCapability { + String APP_ACTIVITY_OPTION = "appActivity"; + + /** + * Main application activity identifier. If not provided then UiAutomator2 + * will try to detect it automatically from the package provided by the app capability. + * + * @param appActivity Fully qualified app activity class name. + * @return self instance for chaining. + */ + default T setAppActivity(String appActivity) { + return amend(APP_ACTIVITY_OPTION, appActivity); + } + + /** + * Get the name of the main app activity. + * + * @return Activity class name. + */ + default Optional getAppActivity() { + return Optional.ofNullable((String) getCapability(APP_ACTIVITY_OPTION)); + } +} diff --git a/src/main/java/io/appium/java_client/android/options/app/SupportsAppDurationOption.java b/src/main/java/io/appium/java_client/android/options/app/SupportsAppDurationOption.java new file mode 100644 index 000000000..c85688efd --- /dev/null +++ b/src/main/java/io/appium/java_client/android/options/app/SupportsAppDurationOption.java @@ -0,0 +1,52 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.java_client.android.options.app; + +import io.appium.java_client.remote.options.BaseOptions; +import io.appium.java_client.remote.options.CanSetCapability; +import org.openqa.selenium.Capabilities; + +import java.time.Duration; +import java.util.Optional; + +import static io.appium.java_client.internal.CapabilityHelpers.toSafeLong; + +public interface SupportsAppDurationOption> extends + Capabilities, CanSetCapability { + String APP_WAIT_DURATION_OPTION = "appWaitDuration"; + + /** + * Maximum amount of time to wait until the application under test is started + * (e.g. an activity returns the control to the caller). 20000 ms by default. + * + * @param appWaitDuration Package identifier to wait for. + * @return self instance for chaining. + */ + default T setAppWaitDuration(Duration appWaitDuration) { + return amend(APP_WAIT_DURATION_OPTION, appWaitDuration.toMillis()); + } + + /** + * Get the identifier of the app package to wait for. + * + * @return Package identifier. + */ + default Optional getAppWaitDuration() { + Long value = toSafeLong(getCapability(APP_WAIT_DURATION_OPTION)); + return Optional.ofNullable(value == null ? null : Duration.ofMillis(value)); + } +} diff --git a/src/main/java/io/appium/java_client/android/options/app/SupportsAppPackageOption.java b/src/main/java/io/appium/java_client/android/options/app/SupportsAppPackageOption.java new file mode 100644 index 000000000..a82cc5dfe --- /dev/null +++ b/src/main/java/io/appium/java_client/android/options/app/SupportsAppPackageOption.java @@ -0,0 +1,48 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.java_client.android.options.app; + +import io.appium.java_client.remote.options.BaseOptions; +import io.appium.java_client.remote.options.CanSetCapability; +import org.openqa.selenium.Capabilities; + +import java.util.Optional; + +public interface SupportsAppPackageOption> extends + Capabilities, CanSetCapability { + String APP_PACKAGE_OPTION = "appPackage"; + + /** + * Application package identifier to be started. If not provided then UiAutomator2 will + * try to detect it automatically from the package provided by the app capability. + * + * @param appPackage App package identifier. + * @return self instance for chaining. + */ + default T setAppPackage(String appPackage) { + return amend(APP_PACKAGE_OPTION, appPackage); + } + + /** + * Get the app package identifier. + * + * @return Identifier value. + */ + default Optional getAppPackage() { + return Optional.ofNullable((String) getCapability(APP_PACKAGE_OPTION)); + } +} diff --git a/src/main/java/io/appium/java_client/android/options/app/SupportsAppWaitActivityOption.java b/src/main/java/io/appium/java_client/android/options/app/SupportsAppWaitActivityOption.java new file mode 100644 index 000000000..5567fbb1e --- /dev/null +++ b/src/main/java/io/appium/java_client/android/options/app/SupportsAppWaitActivityOption.java @@ -0,0 +1,49 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.java_client.android.options.app; + +import io.appium.java_client.remote.options.BaseOptions; +import io.appium.java_client.remote.options.CanSetCapability; +import org.openqa.selenium.Capabilities; + +import java.util.Optional; + +public interface SupportsAppWaitActivityOption> extends + Capabilities, CanSetCapability { + String APP_WAIT_ACTIVITY_OPTION = "appWaitActivity"; + + /** + * Identifier of the activity that the driver should wait for + * (not necessarily the main one). + * If not provided then defaults to appium:appActivity. + * + * @param appWaitActivity Fully qualified app activity class name. + * @return self instance for chaining. + */ + default T setAppWaitActivity(String appWaitActivity) { + return amend(APP_WAIT_ACTIVITY_OPTION, appWaitActivity); + } + + /** + * Get the name of the app activity to wait for. + * + * @return Activity class name. + */ + default Optional getAppWaitActivity() { + return Optional.ofNullable((String) getCapability(APP_WAIT_ACTIVITY_OPTION)); + } +} diff --git a/src/main/java/io/appium/java_client/android/options/app/SupportsAppWaitForLaunchOption.java b/src/main/java/io/appium/java_client/android/options/app/SupportsAppWaitForLaunchOption.java new file mode 100644 index 000000000..70e440b54 --- /dev/null +++ b/src/main/java/io/appium/java_client/android/options/app/SupportsAppWaitForLaunchOption.java @@ -0,0 +1,52 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.java_client.android.options.app; + +import io.appium.java_client.remote.options.BaseOptions; +import io.appium.java_client.remote.options.CanSetCapability; +import org.openqa.selenium.Capabilities; + +import java.util.Optional; + +import static io.appium.java_client.internal.CapabilityHelpers.toSafeBoolean; + +public interface SupportsAppWaitForLaunchOption> extends + Capabilities, CanSetCapability { + String APP_WAIT_FOR_LAUNCH_OPTION = "appWaitForLaunch"; + + /** + * Whether to block until the app under test returns the control to the + * caller after its activity has been started by Activity Manager + * (true, the default value) or to continue the test without waiting for that (false). + * + * @param value Set to false if you don't want to wait for the app to finish its launch. + * @return self instance for chaining. + */ + default T setAppWaitForLaunch(boolean value) { + return amend(APP_WAIT_FOR_LAUNCH_OPTION, value); + } + + /** + * Get whether to block until the app under test returns the control to the + * caller after its activity has been started by Activity Manager. + * + * @return True if the driver should block or false otherwise. + */ + default Optional doesAppWaitForLaunch() { + return Optional.ofNullable(toSafeBoolean(getCapability(APP_WAIT_FOR_LAUNCH_OPTION))); + } +} diff --git a/src/main/java/io/appium/java_client/android/options/app/SupportsAppWaitPackageOption.java b/src/main/java/io/appium/java_client/android/options/app/SupportsAppWaitPackageOption.java new file mode 100644 index 000000000..6089d9bcf --- /dev/null +++ b/src/main/java/io/appium/java_client/android/options/app/SupportsAppWaitPackageOption.java @@ -0,0 +1,49 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.java_client.android.options.app; + +import io.appium.java_client.remote.options.BaseOptions; +import io.appium.java_client.remote.options.CanSetCapability; +import org.openqa.selenium.Capabilities; + +import java.util.Optional; + +public interface SupportsAppWaitPackageOption> extends + Capabilities, CanSetCapability { + String APP_WAIT_PACKAGE_OPTION = "appWaitPackage"; + + /** + * Identifier of the package that the driver should wait for + * (not necessarily the main one). + * If not provided then defaults to appium:appPackage. + * + * @param appWaitPackage Package identifier to wait for. + * @return self instance for chaining. + */ + default T setAppWaitPackage(String appWaitPackage) { + return amend(APP_WAIT_PACKAGE_OPTION, appWaitPackage); + } + + /** + * Get the identifier of the app package to wait for. + * + * @return Package identifier. + */ + default Optional getAppWaitPackage() { + return Optional.ofNullable((String) getCapability(APP_WAIT_PACKAGE_OPTION)); + } +} diff --git a/src/main/java/io/appium/java_client/android/options/app/SupportsAutoGrantPermissionsOption.java b/src/main/java/io/appium/java_client/android/options/app/SupportsAutoGrantPermissionsOption.java new file mode 100644 index 000000000..97402c867 --- /dev/null +++ b/src/main/java/io/appium/java_client/android/options/app/SupportsAutoGrantPermissionsOption.java @@ -0,0 +1,62 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.java_client.android.options.app; + +import io.appium.java_client.remote.options.BaseOptions; +import io.appium.java_client.remote.options.CanSetCapability; +import org.openqa.selenium.Capabilities; + +import java.util.Optional; + +import static io.appium.java_client.internal.CapabilityHelpers.toSafeBoolean; + +public interface SupportsAutoGrantPermissionsOption> extends + Capabilities, CanSetCapability { + String AUTO_GRANT_PERMISSIONS_OPTION = "autoGrantPermissions"; + + /** + * Enables granting of all the requested application permissions + * automatically when a test starts. + * + * @return self instance for chaining. + */ + default T setAutoGrantPermissions() { + return amend(AUTO_GRANT_PERMISSIONS_OPTION, true); + } + + + /** + * Whether to grant all the requested application permissions + * automatically when a test starts(true). false by default. + * + * @param value Whether to enable or disable automatic permissions granting. + * @return self instance for chaining. + */ + default T setAutoGrantPermissions(boolean value) { + return amend(AUTO_GRANT_PERMISSIONS_OPTION, value); + } + + /** + * Get whether to grant all the requested application permissions + * automatically when a test starts. + * + * @return True if the permissions should be granted. + */ + default Optional doesAutoGrantPermissions() { + return Optional.ofNullable(toSafeBoolean(getCapability(AUTO_GRANT_PERMISSIONS_OPTION))); + } +} diff --git a/src/main/java/io/appium/java_client/android/options/app/SupportsEnforceAppInstallOption.java b/src/main/java/io/appium/java_client/android/options/app/SupportsEnforceAppInstallOption.java new file mode 100644 index 000000000..fde56bf83 --- /dev/null +++ b/src/main/java/io/appium/java_client/android/options/app/SupportsEnforceAppInstallOption.java @@ -0,0 +1,61 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.java_client.android.options.app; + +import io.appium.java_client.remote.options.BaseOptions; +import io.appium.java_client.remote.options.CanSetCapability; +import org.openqa.selenium.Capabilities; + +import java.util.Optional; + +import static io.appium.java_client.internal.CapabilityHelpers.toSafeBoolean; + +public interface SupportsEnforceAppInstallOption> extends + Capabilities, CanSetCapability { + String ENFORCE_APP_INSTALL_OPTION = "enforceAppInstall"; + + /** + * Sets the application under test is always reinstalled even if a newer version + * of it already exists on the device under test. + * + * @return self instance for chaining. + */ + default T setEnforceAppInstall() { + return amend(ENFORCE_APP_INSTALL_OPTION, true); + } + + /** + * Allows setting whether the application under test is always reinstalled even + * if a newer version of it already exists on the device under test. false by default. + * + * @param value True to allow test packages installation. + * @return self instance for chaining. + */ + default T setEnforceAppInstall(boolean value) { + return amend(ENFORCE_APP_INSTALL_OPTION, value); + } + + /** + * Get whether the application under test is always reinstalled even + * if a newer version of it already exists on the device under test. + * + * @return True or false. + */ + default Optional doesEnforceAppInstall() { + return Optional.ofNullable(toSafeBoolean(getCapability(ENFORCE_APP_INSTALL_OPTION))); + } +} diff --git a/src/main/java/io/appium/java_client/android/options/app/SupportsIntentActionOption.java b/src/main/java/io/appium/java_client/android/options/app/SupportsIntentActionOption.java new file mode 100644 index 000000000..7fc2afe89 --- /dev/null +++ b/src/main/java/io/appium/java_client/android/options/app/SupportsIntentActionOption.java @@ -0,0 +1,49 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.java_client.android.options.app; + +import io.appium.java_client.remote.options.BaseOptions; +import io.appium.java_client.remote.options.CanSetCapability; +import org.openqa.selenium.Capabilities; + +import java.util.Optional; + +public interface SupportsIntentActionOption> extends + Capabilities, CanSetCapability { + String INTENT_ACTION_OPTION = "intentAction"; + + /** + * Set an optional intent action to be applied when + * starting the given appActivity by Activity Manager. + * + * @param intentAction Intent action class name. + * @return self instance for chaining. + */ + default T setIntentAction(String intentAction) { + return amend(INTENT_ACTION_OPTION, intentAction); + } + + /** + * Get intent action to be applied when + * starting the given appActivity by Activity Manager. + * + * @return Intent action class name. + */ + default Optional getIntentAction() { + return Optional.ofNullable((String) getCapability(INTENT_ACTION_OPTION)); + } +} diff --git a/src/main/java/io/appium/java_client/android/options/app/SupportsIntentCategoryOption.java b/src/main/java/io/appium/java_client/android/options/app/SupportsIntentCategoryOption.java new file mode 100644 index 000000000..287ac09d4 --- /dev/null +++ b/src/main/java/io/appium/java_client/android/options/app/SupportsIntentCategoryOption.java @@ -0,0 +1,49 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.java_client.android.options.app; + +import io.appium.java_client.remote.options.BaseOptions; +import io.appium.java_client.remote.options.CanSetCapability; +import org.openqa.selenium.Capabilities; + +import java.util.Optional; + +public interface SupportsIntentCategoryOption> extends + Capabilities, CanSetCapability { + String INTENT_CATEGORY_OPTION = "intentCategory"; + + /** + * Set an optional intent category to be applied when + * starting the given appActivity by Activity Manager. + * + * @param intentCategory Intent category class name. + * @return self instance for chaining. + */ + default T setIntentCategory(String intentCategory) { + return amend(INTENT_CATEGORY_OPTION, intentCategory); + } + + /** + * Get intent category to be applied when + * starting the given appActivity by Activity Manager. + * + * @return Intent category class name. + */ + default Optional getIntentCategory() { + return Optional.ofNullable((String) getCapability(INTENT_CATEGORY_OPTION)); + } +} diff --git a/src/main/java/io/appium/java_client/android/options/app/SupportsIntentFlagsOption.java b/src/main/java/io/appium/java_client/android/options/app/SupportsIntentFlagsOption.java new file mode 100644 index 000000000..6c9ed08a8 --- /dev/null +++ b/src/main/java/io/appium/java_client/android/options/app/SupportsIntentFlagsOption.java @@ -0,0 +1,49 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.java_client.android.options.app; + +import io.appium.java_client.remote.options.BaseOptions; +import io.appium.java_client.remote.options.CanSetCapability; +import org.openqa.selenium.Capabilities; + +import java.util.Optional; + +public interface SupportsIntentFlagsOption> extends + Capabilities, CanSetCapability { + String INTENT_FLAGS_OPTION = "intentFlags"; + + /** + * Set an optional intent flags to be applied when + * starting the given appActivity by Activity Manager. + * + * @param intentFlags Intent flags hexadecimal string. + * @return self instance for chaining. + */ + default T setIntentFlags(String intentFlags) { + return amend(INTENT_FLAGS_OPTION, intentFlags); + } + + /** + * Get intent flags to be applied when + * starting the given appActivity by Activity Manager. + * + * @return Intent flags string. + */ + default Optional getIntentFlags() { + return Optional.ofNullable((String) getCapability(INTENT_FLAGS_OPTION)); + } +} diff --git a/src/main/java/io/appium/java_client/android/options/app/SupportsOptionalIntentArgumentsOption.java b/src/main/java/io/appium/java_client/android/options/app/SupportsOptionalIntentArgumentsOption.java new file mode 100644 index 000000000..da5d5a3c7 --- /dev/null +++ b/src/main/java/io/appium/java_client/android/options/app/SupportsOptionalIntentArgumentsOption.java @@ -0,0 +1,49 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.java_client.android.options.app; + +import io.appium.java_client.remote.options.BaseOptions; +import io.appium.java_client.remote.options.CanSetCapability; +import org.openqa.selenium.Capabilities; + +import java.util.Optional; + +public interface SupportsOptionalIntentArgumentsOption> extends + Capabilities, CanSetCapability { + String OPTIONAL_INTENT_ARGUMENTS_OPTION = "optionalIntentArguments"; + + /** + * Set an optional intent arguments to be applied when + * starting the given appActivity by Activity Manager. + * + * @param arguments Intent arguments string. + * @return self instance for chaining. + */ + default T setOptionalIntentArguments(String arguments) { + return amend(OPTIONAL_INTENT_ARGUMENTS_OPTION, arguments); + } + + /** + * Get intent arguments to be applied when + * starting the given appActivity by Activity Manager. + * + * @return Intent arguments string. + */ + default Optional getOptionalIntentArguments() { + return Optional.ofNullable((String) getCapability(OPTIONAL_INTENT_ARGUMENTS_OPTION)); + } +} diff --git a/src/main/java/io/appium/java_client/android/options/app/SupportsRemoteAppsCacheLimitOption.java b/src/main/java/io/appium/java_client/android/options/app/SupportsRemoteAppsCacheLimitOption.java new file mode 100644 index 000000000..137c0f279 --- /dev/null +++ b/src/main/java/io/appium/java_client/android/options/app/SupportsRemoteAppsCacheLimitOption.java @@ -0,0 +1,54 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.java_client.android.options.app; + +import io.appium.java_client.remote.options.BaseOptions; +import io.appium.java_client.remote.options.CanSetCapability; +import org.openqa.selenium.Capabilities; + +import java.util.Optional; + +import static io.appium.java_client.internal.CapabilityHelpers.toSafeInteger; +import static io.appium.java_client.internal.CapabilityHelpers.toSafeLong; + +public interface SupportsRemoteAppsCacheLimitOption> extends + Capabilities, CanSetCapability { + String REMOTE_APPS_CACHE_LIMIT_OPTION = "remoteAppsCacheLimit"; + + /** + * Sets the maximum amount of application packages to be cached on the device under test. + * This is needed for devices that don't support streamed installs (Android 7 and below), + * because ADB must push app packages to the device first in order to install them, + * which takes some time. Setting this capability to zero disables apps caching. + * 10 by default. + * + * @param limit The maximum amount of cached apps. + * @return self instance for chaining. + */ + default T setRemoteAppsCacheLimit(int limit) { + return amend(REMOTE_APPS_CACHE_LIMIT_OPTION, limit); + } + + /** + * Get the maximum amount of apps that could be cached on the remote device. + * + * @return The maximum amount of cached apps. + */ + default Optional getRemoteAppsCacheLimit() { + return Optional.ofNullable(toSafeInteger(getCapability(REMOTE_APPS_CACHE_LIMIT_OPTION))); + } +} diff --git a/src/main/java/io/appium/java_client/android/options/app/SupportsUninstallOtherPackagesOption.java b/src/main/java/io/appium/java_client/android/options/app/SupportsUninstallOtherPackagesOption.java new file mode 100644 index 000000000..fb1402d79 --- /dev/null +++ b/src/main/java/io/appium/java_client/android/options/app/SupportsUninstallOtherPackagesOption.java @@ -0,0 +1,48 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.java_client.android.options.app; + +import io.appium.java_client.remote.options.BaseOptions; +import io.appium.java_client.remote.options.CanSetCapability; +import org.openqa.selenium.Capabilities; + +import java.util.Optional; + +public interface SupportsUninstallOtherPackagesOption> extends + Capabilities, CanSetCapability { + String UNINSTALL_OTHER_PACKAGES_OPTION = "uninstallOtherPackages"; + + /** + * Allows to set one or more comma-separated package + * identifiers to be uninstalled from the device before a test starts. + * + * @param packages one or more comma-separated package identifiers to uninstall. + * @return self instance for chaining. + */ + default T setUninstallOtherPackages(String packages) { + return amend(UNINSTALL_OTHER_PACKAGES_OPTION, packages); + } + + /** + * Get identifiers of packages to be uninstalled from the device before a test starts. + * + * @return Comma-separated package identifiers. + */ + default Optional getUninstallOtherPackages() { + return Optional.ofNullable((String) getCapability(UNINSTALL_OTHER_PACKAGES_OPTION)); + } +} diff --git a/src/main/java/io/appium/java_client/android/options/server/SupportsDisableWindowAnimationOption.java b/src/main/java/io/appium/java_client/android/options/server/SupportsDisableWindowAnimationOption.java new file mode 100644 index 000000000..71f3f9626 --- /dev/null +++ b/src/main/java/io/appium/java_client/android/options/server/SupportsDisableWindowAnimationOption.java @@ -0,0 +1,60 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.java_client.android.options.server; + +import io.appium.java_client.remote.options.BaseOptions; +import io.appium.java_client.remote.options.CanSetCapability; +import org.openqa.selenium.Capabilities; + +import java.util.Optional; + +import static io.appium.java_client.internal.CapabilityHelpers.toSafeBoolean; + +public interface SupportsDisableWindowAnimationOption> extends + Capabilities, CanSetCapability { + String DISABLE_WINDOWS_ANIMATION_OPTION = "disableWindowAnimation"; + + /** + * Disables window animations when starting the instrumentation process. + * + * @return self instance for chaining. + */ + default T setDisableWindowAnimation() { + return amend(DISABLE_WINDOWS_ANIMATION_OPTION, true); + } + + /** + * Set whether to disable window animations when starting the instrumentation process. + * false by default + * + * @param value True to disable window animations. + * @return self instance for chaining. + */ + default T setDisableWindowAnimation(boolean value) { + return amend(DISABLE_WINDOWS_ANIMATION_OPTION, value); + } + + /** + * Get whether window animations when starting the instrumentation process + * are disabled. + * + * @return True or false. + */ + default Optional doesDisableWindowAnimation() { + return Optional.ofNullable(toSafeBoolean(getCapability(DISABLE_WINDOWS_ANIMATION_OPTION))); + } +} diff --git a/src/main/java/io/appium/java_client/android/options/server/SupportsSkipDeviceInitializationOption.java b/src/main/java/io/appium/java_client/android/options/server/SupportsSkipDeviceInitializationOption.java new file mode 100644 index 000000000..bdfbd04c3 --- /dev/null +++ b/src/main/java/io/appium/java_client/android/options/server/SupportsSkipDeviceInitializationOption.java @@ -0,0 +1,60 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.java_client.android.options.server; + +import io.appium.java_client.remote.options.BaseOptions; +import io.appium.java_client.remote.options.CanSetCapability; +import org.openqa.selenium.Capabilities; + +import java.util.Optional; + +import static io.appium.java_client.internal.CapabilityHelpers.toSafeBoolean; + +public interface SupportsSkipDeviceInitializationOption> extends + Capabilities, CanSetCapability { + String SKIP_DEVICE_INITIALIZATION_OPTION = "skipDeviceInitialization"; + + /** + * Disables initial device startup checks by the server. + * + * @return self instance for chaining. + */ + default T setSkipDeviceInitialization() { + return amend(SKIP_DEVICE_INITIALIZATION_OPTION, true); + } + + /** + * If set to true then device startup checks (whether it is ready and whether + * Settings app is installed) will be canceled on session creation. + * Could speed up the session creation if you know what you are doing. false by default + * + * @param value True to skip device initialization. + * @return self instance for chaining. + */ + default T setSkipDeviceInitialization(boolean value) { + return amend(SKIP_DEVICE_INITIALIZATION_OPTION, value); + } + + /** + * Get whether initial device startup checks by the server are disabled. + * + * @return True or false. + */ + default Optional doesSkipDeviceInitialization() { + return Optional.ofNullable(toSafeBoolean(getCapability(SKIP_DEVICE_INITIALIZATION_OPTION))); + } +} diff --git a/src/main/java/io/appium/java_client/android/options/server/SupportsSkipServerInstallationOption.java b/src/main/java/io/appium/java_client/android/options/server/SupportsSkipServerInstallationOption.java new file mode 100644 index 000000000..4c69c240a --- /dev/null +++ b/src/main/java/io/appium/java_client/android/options/server/SupportsSkipServerInstallationOption.java @@ -0,0 +1,69 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.java_client.android.options.server; + +import io.appium.java_client.remote.options.BaseOptions; +import io.appium.java_client.remote.options.CanSetCapability; +import org.openqa.selenium.Capabilities; + +import java.util.Optional; + +import static io.appium.java_client.internal.CapabilityHelpers.toSafeBoolean; + +public interface SupportsSkipServerInstallationOption> extends + Capabilities, CanSetCapability { + String SKIP_SERVER_INSTALLATION_OPTION = "skipServerInstallation"; + + /** + * Enables skipping of the UiAutomator2 Server component installation + * on the device under test and all the related checks. + * This could help to speed up the session startup if you know for sure the + * correct server version is installed on the device. + * In case the server is not installed or an incorrect version of it is installed + * then you may get an unexpected error later. + * + * @return self instance for chaining. + */ + default T setSkipServerInstallation() { + return amend(SKIP_SERVER_INSTALLATION_OPTION, true); + } + + /** + * Set whether to skip the UiAutomator2 Server component installation + * on the device under test and all the related checks. + * This could help to speed up the session startup if you know for sure the + * correct server version is installed on the device. + * In case the server is not installed or an incorrect version of it is installed + * then you may get an unexpected error later. + * + * @param value True to skip the server installation. + * @return self instance for chaining. + */ + default T setSkipServerInstallation(boolean value) { + return amend(SKIP_SERVER_INSTALLATION_OPTION, value); + } + + /** + * Get whether to skip the UiAutomator2 Server component installation + * on the device under test and all the related checks. + * + * @return True or false. + */ + default Optional doesSkipServerInstallation() { + return Optional.ofNullable(toSafeBoolean(getCapability(SKIP_SERVER_INSTALLATION_OPTION))); + } +} diff --git a/src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerInstallTimeoutOption.java b/src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerInstallTimeoutOption.java new file mode 100644 index 000000000..947565a11 --- /dev/null +++ b/src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerInstallTimeoutOption.java @@ -0,0 +1,52 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.java_client.android.options.server; + +import io.appium.java_client.remote.options.BaseOptions; +import io.appium.java_client.remote.options.CanSetCapability; +import org.openqa.selenium.Capabilities; + +import java.time.Duration; +import java.util.Optional; + +import static io.appium.java_client.internal.CapabilityHelpers.toSafeLong; + +public interface SupportsUiautomator2ServerInstallTimeoutOption> extends + Capabilities, CanSetCapability { + String UIAUTOMATOR_2_SERVER_INSTALL_TIMEOUT_OPTION = "uiautomator2ServerInstallTimeout"; + + /** + * Set the maximum timeout to wait util UiAutomator2Server is installed on the device. + * 20000 ms by default + * + * @param timeout Timeout value. + * @return self instance for chaining. + */ + default T setUiautomator2ServerInstallTimeout(Duration timeout) { + return amend(UIAUTOMATOR_2_SERVER_INSTALL_TIMEOUT_OPTION, timeout.toMillis()); + } + + /** + * Get the maximum timeout to wait until UiAutomator2Server is installed on the device. + * + * @return The timeout value. + */ + default Optional getUiautomator2ServerInstallTimeout() { + Long value = toSafeLong(getCapability(UIAUTOMATOR_2_SERVER_INSTALL_TIMEOUT_OPTION)); + return Optional.ofNullable(value == null ? null : Duration.ofMillis(value)); + } +} diff --git a/src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerLaunchTimeoutOption.java b/src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerLaunchTimeoutOption.java new file mode 100644 index 000000000..5c0e9dc8b --- /dev/null +++ b/src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerLaunchTimeoutOption.java @@ -0,0 +1,52 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.java_client.android.options.server; + +import io.appium.java_client.remote.options.BaseOptions; +import io.appium.java_client.remote.options.CanSetCapability; +import org.openqa.selenium.Capabilities; + +import java.time.Duration; +import java.util.Optional; + +import static io.appium.java_client.internal.CapabilityHelpers.toSafeLong; + +public interface SupportsUiautomator2ServerLaunchTimeoutOption> extends + Capabilities, CanSetCapability { + String UIAUTOMATOR_2_SERVER_LAUNCH_TIMEOUT_OPTION = "uiautomator2ServerLaunchTimeout"; + + /** + * Set the maximum timeout to wait util UiAutomator2Server is listening on + * the device. 30000 ms by default + * + * @param timeout Timeout value. + * @return self instance for chaining. + */ + default T setUiautomator2ServerLaunchTimeout(Duration timeout) { + return amend(UIAUTOMATOR_2_SERVER_LAUNCH_TIMEOUT_OPTION, timeout.toMillis()); + } + + /** + * Get the maximum timeout to wait until UiAutomator2Server is listening on the device. + * + * @return The timeout value. + */ + default Optional getUiautomator2ServerLaunchTimeout() { + Long value = toSafeLong(getCapability(UIAUTOMATOR_2_SERVER_LAUNCH_TIMEOUT_OPTION)); + return Optional.ofNullable(value == null ? null : Duration.ofMillis(value)); + } +} diff --git a/src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerReadTimeoutOption.java b/src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerReadTimeoutOption.java new file mode 100644 index 000000000..3e41ed6e9 --- /dev/null +++ b/src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerReadTimeoutOption.java @@ -0,0 +1,54 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.java_client.android.options.server; + +import io.appium.java_client.remote.options.BaseOptions; +import io.appium.java_client.remote.options.CanSetCapability; +import org.openqa.selenium.Capabilities; + +import java.time.Duration; +import java.util.Optional; + +import static io.appium.java_client.internal.CapabilityHelpers.toSafeLong; + +public interface SupportsUiautomator2ServerReadTimeoutOption> extends + Capabilities, CanSetCapability { + String UIAUTOMATOR_2_SERVER_READ_TIMEOUT_OPTION = "uiautomator2ServerReadTimeout"; + + /** + * Set the maximum timeout to wait for a HTTP response from UiAutomator2Server. + * Only values greater than zero are accepted. If the given value is too low + * then expect driver commands to fail with timeout of Xms exceeded error. + * 240000 ms by default + * + * @param timeout Timeout value. + * @return self instance for chaining. + */ + default T setUiautomator2ServerReadTimeout(Duration timeout) { + return amend(UIAUTOMATOR_2_SERVER_READ_TIMEOUT_OPTION, timeout.toMillis()); + } + + /** + * Get the maximum timeout to wait for a HTTP response from UiAutomator2Server. + * + * @return The timeout value. + */ + default Optional getUiautomator2ServerReadTimeout() { + Long value = toSafeLong(getCapability(UIAUTOMATOR_2_SERVER_READ_TIMEOUT_OPTION)); + return Optional.ofNullable(value == null ? null : Duration.ofMillis(value)); + } +} diff --git a/src/main/java/io/appium/java_client/internal/CapabilityHelpers.java b/src/main/java/io/appium/java_client/internal/CapabilityHelpers.java index b97da189c..37ba1edc3 100644 --- a/src/main/java/io/appium/java_client/internal/CapabilityHelpers.java +++ b/src/main/java/io/appium/java_client/internal/CapabilityHelpers.java @@ -21,6 +21,7 @@ import javax.annotation.Nullable; import java.util.ArrayList; import java.util.List; +import java.util.Objects; public class CapabilityHelpers { public static final String APPIUM_PREFIX = "appium:"; @@ -56,4 +57,55 @@ public static T getCapability(Capabilities caps, String name, Class expec } return null; } + + /** + * Converts generic capability value to boolean without + * throwing exceptions. + * + * @param value The capability value. + * @return null is the passed value is null otherwise the converted value. + */ + @Nullable + public static Boolean toSafeBoolean(Object value) { + if (value == null) { + return null; + } + return (value instanceof String) + ? ((String) value).equalsIgnoreCase("true") + : Objects.equals(value, true); + } + + /** + * Converts generic capability value to integer without + * throwing exceptions. + * + * @param value The capability value. + * @return null is the passed value is null otherwise the converted value. + */ + @Nullable + public static Integer toSafeInteger(Object value) { + if (value instanceof String) { + return Integer.parseInt((String) value); + } else if (value instanceof Number) { + return ((Number) value).intValue(); + } + return null; + } + + /** + * Converts generic capability value to long without + * throwing exceptions. + * + * @param value The capability value. + * @return null is the passed value is null otherwise the converted value. + */ + @Nullable + public static Long toSafeLong(Object value) { + if (value instanceof String) { + return Long.parseLong((String) value); + } else if (value instanceof Number) { + return ((Number) value).longValue(); + } + return null; + } } diff --git a/src/main/java/io/appium/java_client/remote/MobileOptions.java b/src/main/java/io/appium/java_client/remote/MobileOptions.java index fad8d5ad1..98bd098b7 100644 --- a/src/main/java/io/appium/java_client/remote/MobileOptions.java +++ b/src/main/java/io/appium/java_client/remote/MobileOptions.java @@ -16,18 +16,13 @@ package io.appium.java_client.remote; -import io.appium.java_client.remote.options.BaseOptions; -import io.appium.java_client.remote.options.SupportsAppOption; -import io.appium.java_client.remote.options.SupportsAutoWebViewOption; -import io.appium.java_client.remote.options.SupportsClearSystemFilesOption; -import io.appium.java_client.remote.options.SupportsDeviceNameOption; -import io.appium.java_client.remote.options.SupportsEnablePerformanceLoggingOption; -import io.appium.java_client.remote.options.SupportsLanguageOption; -import io.appium.java_client.remote.options.SupportsLocaleOption; -import io.appium.java_client.remote.options.SupportsOrientationOption; -import io.appium.java_client.remote.options.SupportsOtherAppsOption; -import io.appium.java_client.remote.options.SupportsUdidOption; import org.openqa.selenium.Capabilities; +import org.openqa.selenium.MutableCapabilities; +import org.openqa.selenium.ScreenOrientation; +import org.openqa.selenium.remote.CapabilityType; + +import java.net.URL; +import java.time.Duration; /** * Use the specific options class for your driver, @@ -36,12 +31,7 @@ * @param The child class for a proper chaining. */ @Deprecated -public class MobileOptions> extends BaseOptions - implements SupportsAppOption, SupportsAutoWebViewOption, - SupportsClearSystemFilesOption, SupportsDeviceNameOption, - SupportsEnablePerformanceLoggingOption, SupportsLanguageOption, - SupportsLocaleOption, SupportsOrientationOption, SupportsOtherAppsOption, - SupportsUdidOption { +public class MobileOptions> extends MutableCapabilities { /** * Creates new instance with no preset capabilities. @@ -57,4 +47,466 @@ public MobileOptions() { public MobileOptions(Capabilities source) { super(source); } + + /** + * Set the kind of mobile device or emulator to use. + * + * @param platform the kind of mobile device or emulator to use. + * @return this MobileOptions, for chaining. + * @see org.openqa.selenium.remote.CapabilityType#PLATFORM_NAME + */ + public T setPlatformName(String platform) { + return amend(CapabilityType.PLATFORM_NAME, platform); + } + + /** + * Set the absolute local path for the location of the App. + * The or remote http URL to a {@code .ipa} file (IOS), + * + * @param path is a String representing the location of the App + * @return this MobileOptions, for chaining. + * @see MobileCapabilityType#APP + */ + public T setApp(String path) { + return amend(MobileCapabilityType.APP, path); + } + + /** + * Set the remote http URL for the location of the App. + * + * @param url is the URL representing the location of the App + * @return this MobileOptions, for chaining. + * @see MobileCapabilityType#APP + */ + public T setApp(URL url) { + return setApp(url.toString()); + } + + /** + * Get the app location. + * + * @return String representing app location + * @see MobileCapabilityType#APP + */ + public String getApp() { + return (String) getCapability(MobileCapabilityType.APP); + } + + /** + * Set the automation engine to use. + * + * @param name is the name of the automation engine + * @return this MobileOptions, for chaining. + * @see MobileCapabilityType#AUTOMATION_NAME + */ + public T setAutomationName(String name) { + return amend(MobileCapabilityType.AUTOMATION_NAME, name); + } + + /** + * Get the automation engine to use. + * + * @return String representing the name of the automation engine + * @see MobileCapabilityType#AUTOMATION_NAME + */ + public String getAutomationName() { + return (String) getCapability(MobileCapabilityType.AUTOMATION_NAME); + } + + /** + * Set the app to move directly into Webview context. + * + * @return this MobileOptions, for chaining. + * @see MobileCapabilityType#AUTO_WEBVIEW + */ + public T setAutoWebview() { + return setAutoWebview(true); + } + + /** + * Set whether the app moves directly into Webview context. + * + * @param bool is whether the app moves directly into Webview context. + * @return this MobileOptions, for chaining. + * @see MobileCapabilityType#AUTO_WEBVIEW + */ + public T setAutoWebview(boolean bool) { + return amend(MobileCapabilityType.AUTO_WEBVIEW, bool); + } + + /** + * Get whether the app moves directly into Webview context. + * + * @return true if app moves directly into Webview context. + * @see MobileCapabilityType#AUTO_WEBVIEW + */ + public boolean doesAutoWebview() { + return (boolean) getCapability(MobileCapabilityType.AUTO_WEBVIEW); + } + + /** + * Set the app to delete any generated files at the end of a session. + * + * @return this MobileOptions, for chaining. + * @see MobileCapabilityType#CLEAR_SYSTEM_FILES + */ + public T setClearSystemFiles() { + return setClearSystemFiles(true); + } + + /** + * Set whether the app deletes generated files at the end of a session. + * + * @param bool is whether the app deletes generated files at the end of a session. + * @return this MobileOptions, for chaining. + * @see MobileCapabilityType#CLEAR_SYSTEM_FILES + */ + public T setClearSystemFiles(boolean bool) { + return amend(MobileCapabilityType.CLEAR_SYSTEM_FILES, bool); + } + + /** + * Get whether the app deletes generated files at the end of a session. + * + * @return true if the app deletes generated files at the end of a session. + * @see MobileCapabilityType#CLEAR_SYSTEM_FILES + */ + public boolean doesClearSystemFiles() { + return (boolean) getCapability(MobileCapabilityType.CLEAR_SYSTEM_FILES); + } + + /** + * Set the name of the device. + * + * @param deviceName is the name of the device. + * @return this MobileOptions, for chaining. + * @see MobileCapabilityType#DEVICE_NAME + */ + public T setDeviceName(String deviceName) { + return amend(MobileCapabilityType.DEVICE_NAME, deviceName); + } + + /** + * Get the name of the device. + * + * @return String representing the name of the device. + * @see MobileCapabilityType#DEVICE_NAME + */ + public String getDeviceName() { + return (String) getCapability(MobileCapabilityType.DEVICE_NAME); + } + + /** + * Set the app to enable performance logging. + * + * @return this MobileOptions, for chaining. + * @see MobileCapabilityType#ENABLE_PERFORMANCE_LOGGING + */ + public T setEnablePerformanceLogging() { + return setEnablePerformanceLogging(true); + } + + /** + * Set whether the app logs performance. + * + * @param bool is whether the app logs performance. + * @return this MobileOptions, for chaining. + * @see MobileCapabilityType#ENABLE_PERFORMANCE_LOGGING + */ + public T setEnablePerformanceLogging(boolean bool) { + return amend(MobileCapabilityType.ENABLE_PERFORMANCE_LOGGING, bool); + } + + /** + * Get the app logs performance. + * + * @return true if the app logs performance. + * @see MobileCapabilityType#ENABLE_PERFORMANCE_LOGGING + */ + public boolean isEnablePerformanceLogging() { + return (boolean) getCapability(MobileCapabilityType.ENABLE_PERFORMANCE_LOGGING); + } + + /** + * Set the app to report the timings for various Appium-internal events. + * + * @return this MobileOptions, for chaining. + * @see MobileCapabilityType#EVENT_TIMINGS + */ + public T setEventTimings() { + return setEventTimings(true); + } + + /** + * Set whether the app reports the timings for various Appium-internal events. + * + * @param bool is whether the app enables event timings. + * @return this MobileOptions, for chaining. + * @see MobileCapabilityType#EVENT_TIMINGS + */ + public T setEventTimings(boolean bool) { + return amend(MobileCapabilityType.EVENT_TIMINGS, bool); + } + + /** + * Get whether the app reports the timings for various Appium-internal events. + * + * @return true if the app reports event timings. + * @see MobileCapabilityType#EVENT_TIMINGS + */ + public boolean doesEventTimings() { + return (boolean) getCapability(MobileCapabilityType.EVENT_TIMINGS); + } + + /** + * Set the app to do a full reset. + * + * @return this MobileOptions, for chaining. + * @see MobileCapabilityType#FULL_RESET + */ + public T setFullReset() { + return setFullReset(true); + } + + /** + * Set whether the app does a full reset. + * + * @param bool is whether the app does a full reset. + * @return this MobileOptions, for chaining. + * @see MobileCapabilityType#FULL_RESET + */ + public T setFullReset(boolean bool) { + return amend(MobileCapabilityType.FULL_RESET, bool); + } + + /** + * Get whether the app does a full reset. + * + * @return true if the app does a full reset. + * @see MobileCapabilityType#FULL_RESET + */ + public boolean doesFullReset() { + return (boolean) getCapability(MobileCapabilityType.FULL_RESET); + } + + /** + * Set language abbreviation for use in session. + * + * @param language is the language abbreviation. + * @return this MobileOptions, for chaining. + * @see MobileCapabilityType#LANGUAGE + */ + public T setLanguage(String language) { + return amend(MobileCapabilityType.LANGUAGE, language); + } + + /** + * Get language abbreviation for use in session. + * + * @return String representing the language abbreviation. + * @see MobileCapabilityType#LANGUAGE + */ + public String getLanguage() { + return (String) getCapability(MobileCapabilityType.LANGUAGE); + } + + /** + * Set locale abbreviation for use in session. + * + * @param locale is the locale abbreviation. + * @return this MobileOptions, for chaining. + * @see MobileCapabilityType#LOCALE + */ + public T setLocale(String locale) { + return amend(MobileCapabilityType.LOCALE, locale); + } + + /** + * Get locale abbreviation for use in session. + * + * @return String representing the locale abbreviation. + * @see MobileCapabilityType#LOCALE + */ + public String getLocale() { + return (String) getCapability(MobileCapabilityType.LOCALE); + } + + /** + * Set the timeout for new commands. + * + * @param duration is the allowed time before seeing a new command. + * @return this MobileOptions, for chaining. + * @see MobileCapabilityType#NEW_COMMAND_TIMEOUT + */ + public T setNewCommandTimeout(Duration duration) { + return amend(MobileCapabilityType.NEW_COMMAND_TIMEOUT, duration.getSeconds()); + } + + /** + * Get the timeout for new commands. + * + * @return allowed time before seeing a new command. + * @see MobileCapabilityType#NEW_COMMAND_TIMEOUT + */ + public Duration getNewCommandTimeout() { + Object duration = getCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT); + return Duration.ofSeconds(Long.parseLong("" + duration)); + } + + /** + * Set the app not to do a reset. + * + * @return this MobileOptions, for chaining. + * @see MobileCapabilityType#NO_RESET + */ + public T setNoReset() { + return setNoReset(true); + } + + /** + * Set whether the app does not do a reset. + * + * @param bool is whether the app does not do a reset. + * @return this MobileOptions, for chaining. + * @see MobileCapabilityType#NO_RESET + */ + public T setNoReset(boolean bool) { + return amend(MobileCapabilityType.NO_RESET, bool); + } + + /** + * Get whether the app does not do a reset. + * + * @return true if the app does not do a reset. + * @see MobileCapabilityType#NO_RESET + */ + public boolean doesNoReset() { + return (boolean) getCapability(MobileCapabilityType.NO_RESET); + } + + /** + * Set the orientation of the screen. + * + * @param orientation is the screen orientation. + * @return this MobileOptions, for chaining. + * @see MobileCapabilityType#ORIENTATION + */ + public T setOrientation(ScreenOrientation orientation) { + return amend(MobileCapabilityType.ORIENTATION, orientation); + } + + /** + * Get the orientation of the screen. + * + * @return ScreenOrientation of the app. + * @see MobileCapabilityType#ORIENTATION + */ + public ScreenOrientation getOrientation() { + return (ScreenOrientation) getCapability(MobileCapabilityType.ORIENTATION); + } + + /** + * Set the location of the app(s) to install before running a test. + * + * @param apps is the apps to install. + * @return this MobileOptions, for chaining. + * @see MobileCapabilityType#OTHER_APPS + */ + public T setOtherApps(String apps) { + return amend(MobileCapabilityType.OTHER_APPS, apps); + } + + /** + * Get the list of apps to install before running a test. + * + * @return String of apps to install. + * @see MobileCapabilityType#OTHER_APPS + */ + public String getOtherApps() { + return (String) getCapability(MobileCapabilityType.OTHER_APPS); + } + + /** + * Set the version of the platform. + * + * @param version is the platform version. + * @return this MobileOptions, for chaining. + * @see MobileCapabilityType#PLATFORM_VERSION + */ + public T setPlatformVersion(String version) { + return amend(MobileCapabilityType.PLATFORM_VERSION, version); + } + + /** + * Get the version of the platform. + * + * @return String representing the platform version. + * @see MobileCapabilityType#PLATFORM_VERSION + */ + public String getPlatformVersion() { + return (String) getCapability(MobileCapabilityType.PLATFORM_VERSION); + } + + /** + * Set the app to print page source when a find operation fails. + * + * @return this MobileOptions, for chaining. + * @see MobileCapabilityType#PRINT_PAGE_SOURCE_ON_FIND_FAILURE + */ + public T setPrintPageSourceOnFindFailure() { + return setPrintPageSourceOnFindFailure(true); + } + + /** + * Set whether the app to print page source when a find operation fails. + * + * @param bool is whether to print page source. + * @return this MobileOptions, for chaining. + * @see MobileCapabilityType#PRINT_PAGE_SOURCE_ON_FIND_FAILURE + */ + public T setPrintPageSourceOnFindFailure(boolean bool) { + return amend(MobileCapabilityType.PRINT_PAGE_SOURCE_ON_FIND_FAILURE, bool); + } + + /** + * Get whether the app to print page source when a find operation fails. + * + * @return true if app prints page source. + * @see MobileCapabilityType#PRINT_PAGE_SOURCE_ON_FIND_FAILURE + */ + public boolean doesPrintPageSourceOnFindFailure() { + return (boolean) getCapability(MobileCapabilityType.PRINT_PAGE_SOURCE_ON_FIND_FAILURE); + } + + /** + * Set the id of the device. + * + * @param id is the unique device identifier. + * @return this MobileOptions, for chaining. + * @see MobileCapabilityType#UDID + */ + public T setUdid(String id) { + return amend(MobileCapabilityType.UDID, id); + } + + /** + * Get the id of the device. + * + * @return String representing the unique device identifier. + * @see MobileCapabilityType#UDID + */ + public String getUdid() { + return (String) getCapability(MobileCapabilityType.UDID); + } + + @Override + public T merge(Capabilities extraCapabilities) { + super.merge(extraCapabilities); + return (T) this; + } + + protected T amend(String optionName, Object value) { + setCapability(optionName, value); + return (T) this; + } } diff --git a/src/main/java/io/appium/java_client/remote/options/BaseOptions.java b/src/main/java/io/appium/java_client/remote/options/BaseOptions.java index cdfb8f168..ff575989f 100644 --- a/src/main/java/io/appium/java_client/remote/options/BaseOptions.java +++ b/src/main/java/io/appium/java_client/remote/options/BaseOptions.java @@ -16,13 +16,13 @@ package io.appium.java_client.remote.options; -import io.appium.java_client.remote.MobileCapabilityType; import org.openqa.selenium.Capabilities; import org.openqa.selenium.MutableCapabilities; +import org.openqa.selenium.internal.Require; import org.openqa.selenium.remote.AcceptedW3CCapabilityKeys; import org.openqa.selenium.remote.CapabilityType; -import java.time.Duration; +import javax.annotation.Nullable; import java.util.Map; import java.util.stream.Collectors; @@ -35,12 +35,13 @@ * * @param The child class for a proper chaining. */ -@SuppressWarnings("unused") +@SuppressWarnings({"unused", "UnusedReturnValue"}) public class BaseOptions> extends MutableCapabilities - implements CanSetCapability { - private static final AcceptedW3CCapabilityKeys W3C_KEY_PATTERNS = - new AcceptedW3CCapabilityKeys(); - + implements CanSetCapability, SupportsAutomationNameOption, + SupportsEventTimingsOption, SupportsPrintPageSourceOnFindFailureOption, + SupportsNoResetOption, SupportsFullResetOption, SupportsNewCommandTimeoutOption, + SupportsPlatformVersionOption { + private static final AcceptedW3CCapabilityKeys W3C_KEY_PATTERNS = new AcceptedW3CCapabilityKeys(); /** * Creates new instance with no preset capabilities. @@ -68,194 +69,6 @@ public T setPlatformName(String platform) { return amend(CapabilityType.PLATFORM_NAME, platform); } - /** - * Set the automation engine to use. - * - * @param name is the name of the automation engine - * @return this MobileOptions, for chaining. - * @see MobileCapabilityType#AUTOMATION_NAME - */ - public T setAutomationName(String name) { - return amend(MobileCapabilityType.AUTOMATION_NAME, name); - } - - /** - * Get the automation engine to use. - * - * @return String representing the name of the automation engine - * @see MobileCapabilityType#AUTOMATION_NAME - */ - public String getAutomationName() { - return (String) getCapability(MobileCapabilityType.AUTOMATION_NAME); - } - - /** - * Set the app to report the timings for various Appium-internal events. - * - * @return this MobileOptions, for chaining. - * @see MobileCapabilityType#EVENT_TIMINGS - */ - public T setEventTimings() { - return setEventTimings(true); - } - - /** - * Set whether the app reports the timings for various Appium-internal events. - * - * @param bool is whether the app enables event timings. - * @return this MobileOptions, for chaining. - * @see MobileCapabilityType#EVENT_TIMINGS - */ - public T setEventTimings(boolean bool) { - return amend(MobileCapabilityType.EVENT_TIMINGS, bool); - } - - /** - * Get whether the app reports the timings for various Appium-internal events. - * - * @return true if the app reports event timings. - * @see MobileCapabilityType#EVENT_TIMINGS - */ - public boolean doesEventTimings() { - return (boolean) getCapability(MobileCapabilityType.EVENT_TIMINGS); - } - - /** - * Set the app to do a full reset. - * - * @return this MobileOptions, for chaining. - * @see MobileCapabilityType#FULL_RESET - */ - public T setFullReset() { - return setFullReset(true); - } - - /** - * Set whether the app does a full reset. - * - * @param bool is whether the app does a full reset. - * @return this MobileOptions, for chaining. - * @see MobileCapabilityType#FULL_RESET - */ - public T setFullReset(boolean bool) { - return amend(MobileCapabilityType.FULL_RESET, bool); - } - - /** - * Get whether the app does a full reset. - * - * @return true if the app does a full reset. - * @see MobileCapabilityType#FULL_RESET - */ - public boolean doesFullReset() { - return (boolean) getCapability(MobileCapabilityType.FULL_RESET); - } - - /** - * Set the timeout for new commands. - * - * @param duration is the allowed time before seeing a new command. - * @return this MobileOptions, for chaining. - * @see MobileCapabilityType#NEW_COMMAND_TIMEOUT - */ - public T setNewCommandTimeout(Duration duration) { - return amend(MobileCapabilityType.NEW_COMMAND_TIMEOUT, duration.getSeconds()); - } - - /** - * Get the timeout for new commands. - * - * @return allowed time before seeing a new command. - * @see MobileCapabilityType#NEW_COMMAND_TIMEOUT - */ - public Duration getNewCommandTimeout() { - Object duration = getCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT); - return Duration.ofSeconds(Long.parseLong("" + duration)); - } - - /** - * Set the app not to do a reset. - * - * @return this MobileOptions, for chaining. - * @see MobileCapabilityType#NO_RESET - */ - public T setNoReset() { - return setNoReset(true); - } - - /** - * Set whether the app does not do a reset. - * - * @param bool is whether the app does not do a reset. - * @return this MobileOptions, for chaining. - * @see MobileCapabilityType#NO_RESET - */ - public T setNoReset(boolean bool) { - return amend(MobileCapabilityType.NO_RESET, bool); - } - - /** - * Get whether the app does not do a reset. - * - * @return true if the app does not do a reset. - * @see MobileCapabilityType#NO_RESET - */ - public boolean doesNoReset() { - return (boolean) getCapability(MobileCapabilityType.NO_RESET); - } - - /** - * Set the version of the platform. - * - * @param version is the platform version. - * @return this MobileOptions, for chaining. - * @see MobileCapabilityType#PLATFORM_VERSION - */ - public T setPlatformVersion(String version) { - return amend(MobileCapabilityType.PLATFORM_VERSION, version); - } - - /** - * Get the version of the platform. - * - * @return String representing the platform version. - * @see MobileCapabilityType#PLATFORM_VERSION - */ - public String getPlatformVersion() { - return (String) getCapability(MobileCapabilityType.PLATFORM_VERSION); - } - - /** - * Set the app to print page source when a find operation fails. - * - * @return this MobileOptions, for chaining. - * @see MobileCapabilityType#PRINT_PAGE_SOURCE_ON_FIND_FAILURE - */ - public T setPrintPageSourceOnFindFailure() { - return setPrintPageSourceOnFindFailure(true); - } - - /** - * Set whether the app to print page source when a find operation fails. - * - * @param bool is whether to print page source. - * @return this MobileOptions, for chaining. - * @see MobileCapabilityType#PRINT_PAGE_SOURCE_ON_FIND_FAILURE - */ - public T setPrintPageSourceOnFindFailure(boolean bool) { - return amend(MobileCapabilityType.PRINT_PAGE_SOURCE_ON_FIND_FAILURE, bool); - } - - /** - * Get whether the app to print page source when a find operation fails. - * - * @return true if app prints page source. - * @see MobileCapabilityType#PRINT_PAGE_SOURCE_ON_FIND_FAILURE - */ - public boolean doesPrintPageSourceOnFindFailure() { - return (boolean) getCapability(MobileCapabilityType.PRINT_PAGE_SOURCE_ON_FIND_FAILURE); - } - @Override public Map asMap() { return unmodifiableMap(super.asMap().entrySet().stream() @@ -270,4 +83,20 @@ public T merge(Capabilities extraCapabilities) { //noinspection unchecked return (T) this; } -} + + @Override + public void setCapability(String key, @Nullable Object value) { + Require.nonNull("Capability name", key); + super.setCapability(W3C_KEY_PATTERNS.test(key) ? key : APPIUM_PREFIX + key, value); + } + + @Override + @Nullable + public Object getCapability(String capabilityName) { + Object value = super.getCapability(capabilityName); + if (value == null) { + value = super.getCapability(APPIUM_PREFIX + capabilityName); + } + return value; + } +} \ No newline at end of file diff --git a/src/main/java/io/appium/java_client/remote/options/SupportsAppOption.java b/src/main/java/io/appium/java_client/remote/options/SupportsAppOption.java index 8e112a32f..043904fde 100644 --- a/src/main/java/io/appium/java_client/remote/options/SupportsAppOption.java +++ b/src/main/java/io/appium/java_client/remote/options/SupportsAppOption.java @@ -16,32 +16,32 @@ package io.appium.java_client.remote.options; -import io.appium.java_client.remote.MobileCapabilityType; import org.openqa.selenium.Capabilities; import java.net.URL; +import java.util.Optional; public interface SupportsAppOption> extends Capabilities, CanSetCapability { + String APP_OPTION = "app"; /** * Set the absolute local path for the location of the App. - * The or remote http URL to a {@code .ipa} file (IOS), + * The app must be located on the same machine where Appium + * server is running. * * @param path is a String representing the location of the App - * @return this MobileOptions, for chaining. - * @see MobileCapabilityType#APP + * @return self instance for chaining. */ default T setApp(String path) { - return amend(MobileCapabilityType.APP, path); + return amend(APP_OPTION, path); } /** * Set the remote http URL for the location of the App. * * @param url is the URL representing the location of the App - * @return this MobileOptions, for chaining. - * @see MobileCapabilityType#APP + * @return self instance for chaining. */ default T setApp(URL url) { return setApp(url.toString()); @@ -51,9 +51,8 @@ default T setApp(URL url) { * Get the app location. * * @return String representing app location - * @see MobileCapabilityType#APP */ - default String getApp() { - return (String) getCapability(MobileCapabilityType.APP); + default Optional getApp() { + return Optional.ofNullable((String) getCapability(APP_OPTION)); } } diff --git a/src/main/java/io/appium/java_client/remote/options/SupportsAutoWebViewOption.java b/src/main/java/io/appium/java_client/remote/options/SupportsAutoWebViewOption.java index 8c68c20bf..e51dbbecb 100644 --- a/src/main/java/io/appium/java_client/remote/options/SupportsAutoWebViewOption.java +++ b/src/main/java/io/appium/java_client/remote/options/SupportsAutoWebViewOption.java @@ -16,16 +16,20 @@ package io.appium.java_client.remote.options; -import io.appium.java_client.remote.MobileCapabilityType; import org.openqa.selenium.Capabilities; +import java.util.Optional; + +import static io.appium.java_client.internal.CapabilityHelpers.toSafeBoolean; + public interface SupportsAutoWebViewOption> extends Capabilities, CanSetCapability { + String AUTO_WEB_VIEW_OPTION = "autoWebView"; + /** * Set the app to move directly into Webview context. * - * @return this MobileOptions, for chaining. - * @see MobileCapabilityType#AUTO_WEBVIEW + * @return self instance for chaining. */ default T setAutoWebview() { return setAutoWebview(true); @@ -35,20 +39,18 @@ default T setAutoWebview() { * Set whether the app moves directly into Webview context. * * @param bool is whether the app moves directly into Webview context. - * @return this MobileOptions, for chaining. - * @see MobileCapabilityType#AUTO_WEBVIEW + * @return self instance for chaining. */ default T setAutoWebview(boolean bool) { - return amend(MobileCapabilityType.AUTO_WEBVIEW, bool); + return amend(AUTO_WEB_VIEW_OPTION, bool); } /** * Get whether the app moves directly into Webview context. * * @return true if app moves directly into Webview context. - * @see MobileCapabilityType#AUTO_WEBVIEW */ - default boolean doesAutoWebview() { - return (boolean) getCapability(MobileCapabilityType.AUTO_WEBVIEW); + default Optional doesAutoWebview() { + return Optional.ofNullable(toSafeBoolean(getCapability(AUTO_WEB_VIEW_OPTION))); } } diff --git a/src/main/java/io/appium/java_client/remote/options/SupportsAutomationNameOption.java b/src/main/java/io/appium/java_client/remote/options/SupportsAutomationNameOption.java new file mode 100644 index 000000000..2fdb5870d --- /dev/null +++ b/src/main/java/io/appium/java_client/remote/options/SupportsAutomationNameOption.java @@ -0,0 +1,45 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.java_client.remote.options; + +import org.openqa.selenium.Capabilities; + +import java.util.Optional; + +public interface SupportsAutomationNameOption> extends + Capabilities, CanSetCapability { + String AUTOMATION_NAME_OPTION = "automationName"; + + /** + * Set the automation driver to use. + * + * @param automationName One of supported automation names. + * @return self instance for chaining. + */ + default T setAutomationName(String automationName) { + return amend(AUTOMATION_NAME_OPTION, automationName); + } + + /** + * Get the automation driver to use. + * + * @return String representing the name of the automation engine + */ + default Optional getAutomationName() { + return Optional.ofNullable((String) getCapability(AUTOMATION_NAME_OPTION)); + } +} diff --git a/src/main/java/io/appium/java_client/remote/options/SupportsClearSystemFilesOption.java b/src/main/java/io/appium/java_client/remote/options/SupportsClearSystemFilesOption.java index 162956fc1..cbb63f5a4 100644 --- a/src/main/java/io/appium/java_client/remote/options/SupportsClearSystemFilesOption.java +++ b/src/main/java/io/appium/java_client/remote/options/SupportsClearSystemFilesOption.java @@ -16,17 +16,20 @@ package io.appium.java_client.remote.options; -import io.appium.java_client.remote.MobileCapabilityType; import org.openqa.selenium.Capabilities; +import java.util.Optional; + +import static io.appium.java_client.internal.CapabilityHelpers.toSafeBoolean; + public interface SupportsClearSystemFilesOption> extends Capabilities, CanSetCapability { + String CLEAR_SYSTEM_FILES_OPTION = "clearSystemFiles"; /** * Set the app to delete any generated files at the end of a session. * * @return this MobileOptions, for chaining. - * @see MobileCapabilityType#CLEAR_SYSTEM_FILES */ default T setClearSystemFiles() { return setClearSystemFiles(true); @@ -37,19 +40,17 @@ default T setClearSystemFiles() { * * @param bool is whether the app deletes generated files at the end of a session. * @return this MobileOptions, for chaining. - * @see MobileCapabilityType#CLEAR_SYSTEM_FILES */ default T setClearSystemFiles(boolean bool) { - return amend(MobileCapabilityType.CLEAR_SYSTEM_FILES, bool); + return amend(CLEAR_SYSTEM_FILES_OPTION, bool); } /** * Get whether the app deletes generated files at the end of a session. * * @return true if the app deletes generated files at the end of a session. - * @see MobileCapabilityType#CLEAR_SYSTEM_FILES */ - default boolean doesClearSystemFiles() { - return (boolean) getCapability(MobileCapabilityType.CLEAR_SYSTEM_FILES); + default Optional doesClearSystemFiles() { + return Optional.ofNullable(toSafeBoolean(getCapability(CLEAR_SYSTEM_FILES_OPTION))); } } diff --git a/src/main/java/io/appium/java_client/remote/options/SupportsDeviceNameOption.java b/src/main/java/io/appium/java_client/remote/options/SupportsDeviceNameOption.java index 91f1a8c75..729741bbc 100644 --- a/src/main/java/io/appium/java_client/remote/options/SupportsDeviceNameOption.java +++ b/src/main/java/io/appium/java_client/remote/options/SupportsDeviceNameOption.java @@ -16,29 +16,30 @@ package io.appium.java_client.remote.options; -import io.appium.java_client.remote.MobileCapabilityType; import org.openqa.selenium.Capabilities; +import java.util.Optional; + public interface SupportsDeviceNameOption> extends Capabilities, CanSetCapability { + String DEVICE_NAME_OPTION = "deviceName"; + /** * Set the name of the device. * * @param deviceName is the name of the device. * @return this MobileOptions, for chaining. - * @see MobileCapabilityType#DEVICE_NAME */ default T setDeviceName(String deviceName) { - return amend(MobileCapabilityType.DEVICE_NAME, deviceName); + return amend(DEVICE_NAME_OPTION, deviceName); } /** * Get the name of the device. * * @return String representing the name of the device. - * @see MobileCapabilityType#DEVICE_NAME */ - default String getDeviceName() { - return (String) getCapability(MobileCapabilityType.DEVICE_NAME); + default Optional getDeviceName() { + return Optional.ofNullable((String) getCapability(DEVICE_NAME_OPTION)); } } diff --git a/src/main/java/io/appium/java_client/remote/options/SupportsEnablePerformanceLoggingOption.java b/src/main/java/io/appium/java_client/remote/options/SupportsEnablePerformanceLoggingOption.java index 82ff44573..34f8c4dd7 100644 --- a/src/main/java/io/appium/java_client/remote/options/SupportsEnablePerformanceLoggingOption.java +++ b/src/main/java/io/appium/java_client/remote/options/SupportsEnablePerformanceLoggingOption.java @@ -16,17 +16,20 @@ package io.appium.java_client.remote.options; -import io.appium.java_client.remote.MobileCapabilityType; import org.openqa.selenium.Capabilities; +import java.util.Optional; + +import static io.appium.java_client.internal.CapabilityHelpers.toSafeBoolean; + public interface SupportsEnablePerformanceLoggingOption> extends Capabilities, CanSetCapability { + String ENABLE_PERFORMANCE_LOGGING_OPTION = "enablePerformanceLogging"; /** * Set the app to enable performance logging. * - * @return this MobileOptions, for chaining. - * @see MobileCapabilityType#ENABLE_PERFORMANCE_LOGGING + * @return self instance for chaining. */ default T setEnablePerformanceLogging() { return setEnablePerformanceLogging(true); @@ -36,20 +39,18 @@ default T setEnablePerformanceLogging() { * Set whether the app logs performance. * * @param bool is whether the app logs performance. - * @return this MobileOptions, for chaining. - * @see MobileCapabilityType#ENABLE_PERFORMANCE_LOGGING + * @return self instance for chaining. */ default T setEnablePerformanceLogging(boolean bool) { - return amend(MobileCapabilityType.ENABLE_PERFORMANCE_LOGGING, bool); + return amend(ENABLE_PERFORMANCE_LOGGING_OPTION, bool); } /** * Get the app logs performance. * * @return true if the app logs performance. - * @see MobileCapabilityType#ENABLE_PERFORMANCE_LOGGING */ - default boolean isEnablePerformanceLogging() { - return (boolean) getCapability(MobileCapabilityType.ENABLE_PERFORMANCE_LOGGING); + default Optional isEnablePerformanceLogging() { + return Optional.ofNullable(toSafeBoolean(getCapability(ENABLE_PERFORMANCE_LOGGING_OPTION))); } } diff --git a/src/main/java/io/appium/java_client/remote/options/SupportsEventTimingsOption.java b/src/main/java/io/appium/java_client/remote/options/SupportsEventTimingsOption.java new file mode 100644 index 000000000..3d6bd39e6 --- /dev/null +++ b/src/main/java/io/appium/java_client/remote/options/SupportsEventTimingsOption.java @@ -0,0 +1,56 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.java_client.remote.options; + +import org.openqa.selenium.Capabilities; + +import java.util.Optional; + +import static io.appium.java_client.internal.CapabilityHelpers.toSafeBoolean; + +public interface SupportsEventTimingsOption> extends + Capabilities, CanSetCapability { + String EVENT_TIMINGS_OPTION = "eventTimings"; + + /** + * Set the app to report the timings for various Appium-internal events. + * + * @return self instance for chaining. + */ + default T setEventTimings() { + return setEventTimings(true); + } + + /** + * Set whether the app reports the timings for various Appium-internal events. + * + * @param bool is whether the app enables event timings. + * @return self instance for chaining. + */ + default T setEventTimings(boolean bool) { + return amend(EVENT_TIMINGS_OPTION, bool); + } + + /** + * Get whether the app reports the timings for various Appium-internal events. + * + * @return true if the app reports event timings. + */ + default Optional doesEventTimings() { + return Optional.ofNullable(toSafeBoolean(getCapability(EVENT_TIMINGS_OPTION))); + } +} diff --git a/src/main/java/io/appium/java_client/remote/options/SupportsFullResetOption.java b/src/main/java/io/appium/java_client/remote/options/SupportsFullResetOption.java new file mode 100644 index 000000000..7dffc301b --- /dev/null +++ b/src/main/java/io/appium/java_client/remote/options/SupportsFullResetOption.java @@ -0,0 +1,56 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.java_client.remote.options; + +import org.openqa.selenium.Capabilities; + +import java.util.Optional; + +import static io.appium.java_client.internal.CapabilityHelpers.toSafeBoolean; + +public interface SupportsFullResetOption> extends + Capabilities, CanSetCapability { + String FULL_RESET_OPTION = "fullReset"; + + /** + * Set the app to do a full reset. + * + * @return self instance for chaining. + */ + default T setFullReset() { + return setFullReset(true); + } + + /** + * Set whether the app does a full reset. + * + * @param bool is whether the app does a full reset. + * @return self instance for chaining. + */ + default T setFullReset(boolean bool) { + return amend(FULL_RESET_OPTION, bool); + } + + /** + * Get whether the app does a full reset. + * + * @return true if the app does a full reset. + */ + default Optional doesFullReset() { + return Optional.ofNullable(toSafeBoolean(getCapability(FULL_RESET_OPTION))); + } +} diff --git a/src/main/java/io/appium/java_client/remote/options/SupportsLanguageOption.java b/src/main/java/io/appium/java_client/remote/options/SupportsLanguageOption.java index 12e46838e..8428d6f45 100644 --- a/src/main/java/io/appium/java_client/remote/options/SupportsLanguageOption.java +++ b/src/main/java/io/appium/java_client/remote/options/SupportsLanguageOption.java @@ -16,30 +16,30 @@ package io.appium.java_client.remote.options; -import io.appium.java_client.remote.MobileCapabilityType; import org.openqa.selenium.Capabilities; +import java.util.Optional; + public interface SupportsLanguageOption> extends Capabilities, CanSetCapability { + String LANGUAGE_OPTION = "language"; /** * Set language abbreviation for use in session. * * @param language is the language abbreviation. * @return this MobileOptions, for chaining. - * @see MobileCapabilityType#LANGUAGE */ default T setLanguage(String language) { - return amend(MobileCapabilityType.LANGUAGE, language); + return amend(LANGUAGE_OPTION, language); } /** * Get language abbreviation for use in session. * * @return String representing the language abbreviation. - * @see MobileCapabilityType#LANGUAGE */ - default String getLanguage() { - return (String) getCapability(MobileCapabilityType.LANGUAGE); + default Optional getLanguage() { + return Optional.ofNullable((String) getCapability(LANGUAGE_OPTION)); } } diff --git a/src/main/java/io/appium/java_client/remote/options/SupportsLocaleOption.java b/src/main/java/io/appium/java_client/remote/options/SupportsLocaleOption.java index f2f0d26e5..37689be59 100644 --- a/src/main/java/io/appium/java_client/remote/options/SupportsLocaleOption.java +++ b/src/main/java/io/appium/java_client/remote/options/SupportsLocaleOption.java @@ -16,30 +16,30 @@ package io.appium.java_client.remote.options; -import io.appium.java_client.remote.MobileCapabilityType; import org.openqa.selenium.Capabilities; +import java.util.Optional; + public interface SupportsLocaleOption> extends Capabilities, CanSetCapability { + String LOCALE_OPTION = "locale"; /** * Set locale abbreviation for use in session. * * @param locale is the locale abbreviation. - * @return this MobileOptions, for chaining. - * @see MobileCapabilityType#LOCALE + * @return self instance for chaining. */ default T setLocale(String locale) { - return amend(MobileCapabilityType.LOCALE, locale); + return amend(LOCALE_OPTION, locale); } /** * Get locale abbreviation for use in session. * * @return String representing the locale abbreviation. - * @see MobileCapabilityType#LOCALE */ - default String getLocale() { - return (String) getCapability(MobileCapabilityType.LOCALE); + default Optional getLocale() { + return Optional.ofNullable((String) getCapability(LOCALE_OPTION)); } } diff --git a/src/main/java/io/appium/java_client/remote/options/SupportsNewCommandTimeoutOption.java b/src/main/java/io/appium/java_client/remote/options/SupportsNewCommandTimeoutOption.java new file mode 100644 index 000000000..f452341d9 --- /dev/null +++ b/src/main/java/io/appium/java_client/remote/options/SupportsNewCommandTimeoutOption.java @@ -0,0 +1,49 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.java_client.remote.options; + +import org.openqa.selenium.Capabilities; + +import java.time.Duration; +import java.util.Optional; + +public interface SupportsNewCommandTimeoutOption> extends + Capabilities, CanSetCapability { + String NEW_COMMAND_TIMEOUT = "newCommandTimeout"; + + /** + * Set the timeout for new commands. + * + * @param duration is the allowed time before seeing a new command. + * @return self instance for chaining. + */ + default T setNewCommandTimeout(Duration duration) { + return amend(NEW_COMMAND_TIMEOUT, duration.getSeconds()); + } + + /** + * Get the timeout for new commands. + * + * @return allowed time before seeing a new command. + */ + default Optional getNewCommandTimeout() { + Object duration = getCapability(NEW_COMMAND_TIMEOUT); + return duration == null + ? Optional.empty() + : Optional.of(Duration.ofSeconds(Long.parseLong("" + duration))); + } +} diff --git a/src/main/java/io/appium/java_client/remote/options/SupportsNoResetOption.java b/src/main/java/io/appium/java_client/remote/options/SupportsNoResetOption.java new file mode 100644 index 000000000..d386690c5 --- /dev/null +++ b/src/main/java/io/appium/java_client/remote/options/SupportsNoResetOption.java @@ -0,0 +1,56 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.java_client.remote.options; + +import org.openqa.selenium.Capabilities; + +import java.util.Optional; + +import static io.appium.java_client.internal.CapabilityHelpers.toSafeBoolean; + +public interface SupportsNoResetOption> extends + Capabilities, CanSetCapability { + String NO_RESET_OPTION = "noReset"; + + /** + * Set the app not to do a reset. + * + * @return self instance for chaining. + */ + default T setNoReset() { + return setNoReset(true); + } + + /** + * Set whether the app does not do a reset. + * + * @param bool is whether the app does not do a reset. + * @return self instance for chaining. + */ + default T setNoReset(boolean bool) { + return amend(NO_RESET_OPTION, bool); + } + + /** + * Get whether the app does not do a reset. + * + * @return true if the app does not do a reset. + */ + default Optional doesNoReset() { + return Optional.ofNullable(toSafeBoolean(getCapability(NO_RESET_OPTION))); + } +} diff --git a/src/main/java/io/appium/java_client/remote/options/SupportsOrientationOption.java b/src/main/java/io/appium/java_client/remote/options/SupportsOrientationOption.java index 4bc34bb59..b587bf9fa 100644 --- a/src/main/java/io/appium/java_client/remote/options/SupportsOrientationOption.java +++ b/src/main/java/io/appium/java_client/remote/options/SupportsOrientationOption.java @@ -16,31 +16,37 @@ package io.appium.java_client.remote.options; -import io.appium.java_client.remote.MobileCapabilityType; import org.openqa.selenium.Capabilities; import org.openqa.selenium.ScreenOrientation; +import java.util.Optional; + public interface SupportsOrientationOption> extends Capabilities, CanSetCapability { + String ORIENTATION_OPTION = "orientation"; /** * Set the orientation of the screen. * * @param orientation is the screen orientation. - * @return this MobileOptions, for chaining. - * @see MobileCapabilityType#ORIENTATION + * @return self instance for chaining. */ default T setOrientation(ScreenOrientation orientation) { - return amend(MobileCapabilityType.ORIENTATION, orientation); + return amend(ORIENTATION_OPTION, orientation.value()); } /** * Get the orientation of the screen. * * @return ScreenOrientation of the app. - * @see MobileCapabilityType#ORIENTATION */ - default ScreenOrientation getOrientation() { - return (ScreenOrientation) getCapability(MobileCapabilityType.ORIENTATION); + default Optional getOrientation() { + Object value = getCapability(ORIENTATION_OPTION); + if (value == null) { + return Optional.empty(); + } + return Optional.of(value instanceof ScreenOrientation + ? (ScreenOrientation) value + : ScreenOrientation.valueOf(((String) value).toUpperCase())); } } diff --git a/src/main/java/io/appium/java_client/remote/options/SupportsOtherAppsOption.java b/src/main/java/io/appium/java_client/remote/options/SupportsOtherAppsOption.java index 2de4c224f..fa08176bc 100644 --- a/src/main/java/io/appium/java_client/remote/options/SupportsOtherAppsOption.java +++ b/src/main/java/io/appium/java_client/remote/options/SupportsOtherAppsOption.java @@ -16,31 +16,30 @@ package io.appium.java_client.remote.options; -import io.appium.java_client.remote.MobileCapabilityType; import org.openqa.selenium.Capabilities; +import java.util.Optional; + public interface SupportsOtherAppsOption> extends Capabilities, CanSetCapability { - + String OTHER_APPS_OPTION = "otherApps"; /** * Set the location of the app(s) to install before running a test. * * @param apps is the apps to install. - * @return this MobileOptions, for chaining. - * @see MobileCapabilityType#OTHER_APPS + * @return self instance for chaining. */ default T setOtherApps(String apps) { - return amend(MobileCapabilityType.OTHER_APPS, apps); + return amend(OTHER_APPS_OPTION, apps); } /** * Get the list of apps to install before running a test. * * @return String of apps to install. - * @see MobileCapabilityType#OTHER_APPS */ - default String getOtherApps() { - return (String) getCapability(MobileCapabilityType.OTHER_APPS); + default Optional getOtherApps() { + return Optional.ofNullable((String) getCapability(OTHER_APPS_OPTION)); } } diff --git a/src/main/java/io/appium/java_client/remote/options/SupportsPlatformVersionOption.java b/src/main/java/io/appium/java_client/remote/options/SupportsPlatformVersionOption.java new file mode 100644 index 000000000..fbb319f32 --- /dev/null +++ b/src/main/java/io/appium/java_client/remote/options/SupportsPlatformVersionOption.java @@ -0,0 +1,45 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.java_client.remote.options; + +import org.openqa.selenium.Capabilities; + +import java.util.Optional; + +public interface SupportsPlatformVersionOption> extends + Capabilities, CanSetCapability { + String PLATFORM_VERSION_OPTION = "platformVersion"; + + /** + * Set the version of the platform. + * + * @param version is the platform version. + * @return self instance for chaining. + */ + default T setPlatformVersion(String version) { + return amend(PLATFORM_VERSION_OPTION, version); + } + + /** + * Get the version of the platform. + * + * @return String representing the platform version. + */ + default Optional getPlatformVersion() { + return Optional.ofNullable((String) getCapability(PLATFORM_VERSION_OPTION)); + } +} diff --git a/src/main/java/io/appium/java_client/remote/options/SupportsPrintPageSourceOnFindFailureOption.java b/src/main/java/io/appium/java_client/remote/options/SupportsPrintPageSourceOnFindFailureOption.java new file mode 100644 index 000000000..b9401302a --- /dev/null +++ b/src/main/java/io/appium/java_client/remote/options/SupportsPrintPageSourceOnFindFailureOption.java @@ -0,0 +1,58 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * See the NOTICE file distributed with this work for additional + * information regarding copyright ownership. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.appium.java_client.remote.options; + +import org.openqa.selenium.Capabilities; + +import java.util.Optional; + +import static io.appium.java_client.internal.CapabilityHelpers.toSafeBoolean; + +public interface SupportsPrintPageSourceOnFindFailureOption> extends + Capabilities, CanSetCapability { + String PRINT_PAGE_SOURCE_ON_FIND_FAILURE_OPTION = "printPageSourceOnFindFailure"; + + /** + * Set the app to print page source when a find operation fails. + * + * @return self instance for chaining. + */ + default T setPrintPageSourceOnFindFailure() { + return setPrintPageSourceOnFindFailure(true); + } + + /** + * Set whether the app to print page source when a find operation fails. + * + * @param bool is whether to print page source. + * @return self instance for chaining. + */ + default T setPrintPageSourceOnFindFailure(boolean bool) { + return amend(PRINT_PAGE_SOURCE_ON_FIND_FAILURE_OPTION, bool); + } + + /** + * Get whether the app to print page source when a find operation fails. + * + * @return true if app prints page source. + */ + default Optional doesPrintPageSourceOnFindFailure() { + return Optional.ofNullable( + toSafeBoolean(getCapability(PRINT_PAGE_SOURCE_ON_FIND_FAILURE_OPTION)) + ); + } +} diff --git a/src/main/java/io/appium/java_client/remote/options/SupportsUdidOption.java b/src/main/java/io/appium/java_client/remote/options/SupportsUdidOption.java index bc6e59b37..fc1364b3d 100644 --- a/src/main/java/io/appium/java_client/remote/options/SupportsUdidOption.java +++ b/src/main/java/io/appium/java_client/remote/options/SupportsUdidOption.java @@ -16,29 +16,30 @@ package io.appium.java_client.remote.options; -import io.appium.java_client.remote.MobileCapabilityType; import org.openqa.selenium.Capabilities; +import java.util.Optional; + public interface SupportsUdidOption> extends Capabilities, CanSetCapability { + String UDID_OPTION = "udid"; + /** * Set the id of the device. * * @param id is the unique device identifier. - * @return this MobileOptions, for chaining. - * @see MobileCapabilityType#UDID + * @return self instance, for chaining. */ default T setUdid(String id) { - return amend(MobileCapabilityType.UDID, id); + return amend(UDID_OPTION, id); } /** * Get the id of the device. * * @return String representing the unique device identifier. - * @see MobileCapabilityType#UDID */ - default String getUdid() { - return (String) getCapability(MobileCapabilityType.UDID); + default Optional getUdid() { + return Optional.ofNullable((String) getCapability(UDID_OPTION)); } } From 4ae0812cba698cc783d70329b8b9f07715ff49c8 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Mon, 25 Oct 2021 06:09:08 +0200 Subject: [PATCH 2/6] Address comments --- .../android/options/UiAutomator2Options.java | 1 + .../app/SupportsAllowTestPackagesOption.java | 2 +- .../SupportsAndroidInstallTimeoutOption.java | 7 ++-- .../app/SupportsAppDurationOption.java | 5 +-- .../app/SupportsEnforceAppInstallOption.java | 2 +- .../SupportsDisableWindowAnimationOption.java | 2 +- ...upportsSkipDeviceInitializationOption.java | 2 +- .../SupportsSkipServerInstallationOption.java | 6 ++- .../SupportsSystemPortOption.java | 2 +- ...iautomator2ServerInstallTimeoutOption.java | 11 ++--- ...Uiautomator2ServerLaunchTimeoutOption.java | 11 ++--- ...tsUiautomator2ServerReadTimeoutOption.java | 11 ++--- .../internal/CapabilityHelpers.java | 40 ++++++++++++++++--- .../options/SupportsAutoWebViewOption.java | 2 +- .../SupportsClearSystemFilesOption.java | 2 +- ...upportsEnablePerformanceLoggingOption.java | 2 +- .../options/SupportsFullResetOption.java | 2 +- .../SupportsNewCommandTimeoutOption.java | 9 +++-- .../remote/options/SupportsNoResetOption.java | 2 +- .../options/SupportsOrientationOption.java | 12 +++--- ...rtsPrintPageSourceOnFindFailureOption.java | 2 +- 21 files changed, 85 insertions(+), 50 deletions(-) rename src/main/java/io/appium/java_client/android/options/{ => server}/SupportsSystemPortOption.java (97%) diff --git a/src/main/java/io/appium/java_client/android/options/UiAutomator2Options.java b/src/main/java/io/appium/java_client/android/options/UiAutomator2Options.java index cb6ac52f4..c228e917f 100644 --- a/src/main/java/io/appium/java_client/android/options/UiAutomator2Options.java +++ b/src/main/java/io/appium/java_client/android/options/UiAutomator2Options.java @@ -35,6 +35,7 @@ import io.appium.java_client.android.options.server.SupportsDisableWindowAnimationOption; import io.appium.java_client.android.options.server.SupportsSkipDeviceInitializationOption; import io.appium.java_client.android.options.server.SupportsSkipServerInstallationOption; +import io.appium.java_client.android.options.server.SupportsSystemPortOption; import io.appium.java_client.android.options.server.SupportsUiautomator2ServerInstallTimeoutOption; import io.appium.java_client.android.options.server.SupportsUiautomator2ServerLaunchTimeoutOption; import io.appium.java_client.android.options.server.SupportsUiautomator2ServerReadTimeoutOption; diff --git a/src/main/java/io/appium/java_client/android/options/app/SupportsAllowTestPackagesOption.java b/src/main/java/io/appium/java_client/android/options/app/SupportsAllowTestPackagesOption.java index 7bf73aaa2..2926aa932 100644 --- a/src/main/java/io/appium/java_client/android/options/app/SupportsAllowTestPackagesOption.java +++ b/src/main/java/io/appium/java_client/android/options/app/SupportsAllowTestPackagesOption.java @@ -34,7 +34,7 @@ public interface SupportsAllowTestPackagesOption> exten * * @return self instance for chaining. */ - default T setAllowTestPackages() { + default T allowTestPackages() { return amend(ALLOW_TEST_PACKAGES_OPTION, true); } diff --git a/src/main/java/io/appium/java_client/android/options/app/SupportsAndroidInstallTimeoutOption.java b/src/main/java/io/appium/java_client/android/options/app/SupportsAndroidInstallTimeoutOption.java index b24d48b11..ed1455685 100644 --- a/src/main/java/io/appium/java_client/android/options/app/SupportsAndroidInstallTimeoutOption.java +++ b/src/main/java/io/appium/java_client/android/options/app/SupportsAndroidInstallTimeoutOption.java @@ -23,7 +23,7 @@ import java.time.Duration; import java.util.Optional; -import static io.appium.java_client.internal.CapabilityHelpers.toSafeLong; +import static io.appium.java_client.internal.CapabilityHelpers.toSafeDuration; public interface SupportsAndroidInstallTimeoutOption> extends Capabilities, CanSetCapability { @@ -46,7 +46,8 @@ default T setAndroidInstallTimeout(Duration installTimeout) { * @return Timeout value. */ default Optional getAndroidInstallTimeout() { - Long value = toSafeLong(getCapability(ANDROID_INSTALL_TIMEOUT_OPTION)); - return Optional.ofNullable(value == null ? null : Duration.ofMillis(value)); + return Optional.ofNullable( + toSafeDuration(getCapability(ANDROID_INSTALL_TIMEOUT_OPTION)) + ); } } diff --git a/src/main/java/io/appium/java_client/android/options/app/SupportsAppDurationOption.java b/src/main/java/io/appium/java_client/android/options/app/SupportsAppDurationOption.java index c85688efd..eb0cb078c 100644 --- a/src/main/java/io/appium/java_client/android/options/app/SupportsAppDurationOption.java +++ b/src/main/java/io/appium/java_client/android/options/app/SupportsAppDurationOption.java @@ -23,7 +23,7 @@ import java.time.Duration; import java.util.Optional; -import static io.appium.java_client.internal.CapabilityHelpers.toSafeLong; +import static io.appium.java_client.internal.CapabilityHelpers.toSafeDuration; public interface SupportsAppDurationOption> extends Capabilities, CanSetCapability { @@ -46,7 +46,6 @@ default T setAppWaitDuration(Duration appWaitDuration) { * @return Package identifier. */ default Optional getAppWaitDuration() { - Long value = toSafeLong(getCapability(APP_WAIT_DURATION_OPTION)); - return Optional.ofNullable(value == null ? null : Duration.ofMillis(value)); + return Optional.ofNullable(toSafeDuration(getCapability(APP_WAIT_DURATION_OPTION))); } } diff --git a/src/main/java/io/appium/java_client/android/options/app/SupportsEnforceAppInstallOption.java b/src/main/java/io/appium/java_client/android/options/app/SupportsEnforceAppInstallOption.java index fde56bf83..3acc25875 100644 --- a/src/main/java/io/appium/java_client/android/options/app/SupportsEnforceAppInstallOption.java +++ b/src/main/java/io/appium/java_client/android/options/app/SupportsEnforceAppInstallOption.java @@ -34,7 +34,7 @@ public interface SupportsEnforceAppInstallOption> exten * * @return self instance for chaining. */ - default T setEnforceAppInstall() { + default T enforceAppInstall() { return amend(ENFORCE_APP_INSTALL_OPTION, true); } diff --git a/src/main/java/io/appium/java_client/android/options/server/SupportsDisableWindowAnimationOption.java b/src/main/java/io/appium/java_client/android/options/server/SupportsDisableWindowAnimationOption.java index 71f3f9626..c8acbaac9 100644 --- a/src/main/java/io/appium/java_client/android/options/server/SupportsDisableWindowAnimationOption.java +++ b/src/main/java/io/appium/java_client/android/options/server/SupportsDisableWindowAnimationOption.java @@ -33,7 +33,7 @@ public interface SupportsDisableWindowAnimationOption> * * @return self instance for chaining. */ - default T setDisableWindowAnimation() { + default T disableWindowAnimation() { return amend(DISABLE_WINDOWS_ANIMATION_OPTION, true); } diff --git a/src/main/java/io/appium/java_client/android/options/server/SupportsSkipDeviceInitializationOption.java b/src/main/java/io/appium/java_client/android/options/server/SupportsSkipDeviceInitializationOption.java index bdfbd04c3..2b42327bb 100644 --- a/src/main/java/io/appium/java_client/android/options/server/SupportsSkipDeviceInitializationOption.java +++ b/src/main/java/io/appium/java_client/android/options/server/SupportsSkipDeviceInitializationOption.java @@ -33,7 +33,7 @@ public interface SupportsSkipDeviceInitializationOption * * @return self instance for chaining. */ - default T setSkipDeviceInitialization() { + default T skipDeviceInitialization() { return amend(SKIP_DEVICE_INITIALIZATION_OPTION, true); } diff --git a/src/main/java/io/appium/java_client/android/options/server/SupportsSkipServerInstallationOption.java b/src/main/java/io/appium/java_client/android/options/server/SupportsSkipServerInstallationOption.java index 4c69c240a..088f040fc 100644 --- a/src/main/java/io/appium/java_client/android/options/server/SupportsSkipServerInstallationOption.java +++ b/src/main/java/io/appium/java_client/android/options/server/SupportsSkipServerInstallationOption.java @@ -38,7 +38,7 @@ public interface SupportsSkipServerInstallationOption> * * @return self instance for chaining. */ - default T setSkipServerInstallation() { + default T skipServerInstallation() { return amend(SKIP_SERVER_INSTALLATION_OPTION, true); } @@ -64,6 +64,8 @@ default T setSkipServerInstallation(boolean value) { * @return True or false. */ default Optional doesSkipServerInstallation() { - return Optional.ofNullable(toSafeBoolean(getCapability(SKIP_SERVER_INSTALLATION_OPTION))); + return Optional.ofNullable( + toSafeBoolean(getCapability(SKIP_SERVER_INSTALLATION_OPTION)) + ); } } diff --git a/src/main/java/io/appium/java_client/android/options/SupportsSystemPortOption.java b/src/main/java/io/appium/java_client/android/options/server/SupportsSystemPortOption.java similarity index 97% rename from src/main/java/io/appium/java_client/android/options/SupportsSystemPortOption.java rename to src/main/java/io/appium/java_client/android/options/server/SupportsSystemPortOption.java index 0f1128b07..b3377d018 100644 --- a/src/main/java/io/appium/java_client/android/options/SupportsSystemPortOption.java +++ b/src/main/java/io/appium/java_client/android/options/server/SupportsSystemPortOption.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package io.appium.java_client.android.options; +package io.appium.java_client.android.options.server; import io.appium.java_client.remote.options.BaseOptions; import io.appium.java_client.remote.options.CanSetCapability; diff --git a/src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerInstallTimeoutOption.java b/src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerInstallTimeoutOption.java index 947565a11..5c8cc7138 100644 --- a/src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerInstallTimeoutOption.java +++ b/src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerInstallTimeoutOption.java @@ -23,11 +23,11 @@ import java.time.Duration; import java.util.Optional; -import static io.appium.java_client.internal.CapabilityHelpers.toSafeLong; +import static io.appium.java_client.internal.CapabilityHelpers.toSafeDuration; public interface SupportsUiautomator2ServerInstallTimeoutOption> extends Capabilities, CanSetCapability { - String UIAUTOMATOR_2_SERVER_INSTALL_TIMEOUT_OPTION = "uiautomator2ServerInstallTimeout"; + String UIAUTOMATOR2_SERVER_INSTALL_TIMEOUT_OPTION = "uiautomator2ServerInstallTimeout"; /** * Set the maximum timeout to wait util UiAutomator2Server is installed on the device. @@ -37,7 +37,7 @@ public interface SupportsUiautomator2ServerInstallTimeoutOption getUiautomator2ServerInstallTimeout() { - Long value = toSafeLong(getCapability(UIAUTOMATOR_2_SERVER_INSTALL_TIMEOUT_OPTION)); - return Optional.ofNullable(value == null ? null : Duration.ofMillis(value)); + return Optional.ofNullable( + toSafeDuration(getCapability(UIAUTOMATOR2_SERVER_INSTALL_TIMEOUT_OPTION)) + ); } } diff --git a/src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerLaunchTimeoutOption.java b/src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerLaunchTimeoutOption.java index 5c0e9dc8b..4e44138a7 100644 --- a/src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerLaunchTimeoutOption.java +++ b/src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerLaunchTimeoutOption.java @@ -23,11 +23,11 @@ import java.time.Duration; import java.util.Optional; -import static io.appium.java_client.internal.CapabilityHelpers.toSafeLong; +import static io.appium.java_client.internal.CapabilityHelpers.toSafeDuration; public interface SupportsUiautomator2ServerLaunchTimeoutOption> extends Capabilities, CanSetCapability { - String UIAUTOMATOR_2_SERVER_LAUNCH_TIMEOUT_OPTION = "uiautomator2ServerLaunchTimeout"; + String UIAUTOMATOR2_SERVER_LAUNCH_TIMEOUT_OPTION = "uiautomator2ServerLaunchTimeout"; /** * Set the maximum timeout to wait util UiAutomator2Server is listening on @@ -37,7 +37,7 @@ public interface SupportsUiautomator2ServerLaunchTimeoutOption getUiautomator2ServerLaunchTimeout() { - Long value = toSafeLong(getCapability(UIAUTOMATOR_2_SERVER_LAUNCH_TIMEOUT_OPTION)); - return Optional.ofNullable(value == null ? null : Duration.ofMillis(value)); + return Optional.ofNullable( + toSafeDuration(getCapability(UIAUTOMATOR2_SERVER_LAUNCH_TIMEOUT_OPTION)) + ); } } diff --git a/src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerReadTimeoutOption.java b/src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerReadTimeoutOption.java index 3e41ed6e9..07ce85ef1 100644 --- a/src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerReadTimeoutOption.java +++ b/src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerReadTimeoutOption.java @@ -23,11 +23,11 @@ import java.time.Duration; import java.util.Optional; -import static io.appium.java_client.internal.CapabilityHelpers.toSafeLong; +import static io.appium.java_client.internal.CapabilityHelpers.toSafeDuration; public interface SupportsUiautomator2ServerReadTimeoutOption> extends Capabilities, CanSetCapability { - String UIAUTOMATOR_2_SERVER_READ_TIMEOUT_OPTION = "uiautomator2ServerReadTimeout"; + String UIAUTOMATOR2_SERVER_READ_TIMEOUT_OPTION = "uiautomator2ServerReadTimeout"; /** * Set the maximum timeout to wait for a HTTP response from UiAutomator2Server. @@ -39,7 +39,7 @@ public interface SupportsUiautomator2ServerReadTimeoutOption getUiautomator2ServerReadTimeout() { - Long value = toSafeLong(getCapability(UIAUTOMATOR_2_SERVER_READ_TIMEOUT_OPTION)); - return Optional.ofNullable(value == null ? null : Duration.ofMillis(value)); + return Optional.ofNullable( + toSafeDuration(getCapability(UIAUTOMATOR2_SERVER_READ_TIMEOUT_OPTION)) + ); } } diff --git a/src/main/java/io/appium/java_client/internal/CapabilityHelpers.java b/src/main/java/io/appium/java_client/internal/CapabilityHelpers.java index 37ba1edc3..4a994d2cf 100644 --- a/src/main/java/io/appium/java_client/internal/CapabilityHelpers.java +++ b/src/main/java/io/appium/java_client/internal/CapabilityHelpers.java @@ -19,9 +19,11 @@ import org.openqa.selenium.Capabilities; import javax.annotation.Nullable; +import java.time.Duration; import java.util.ArrayList; import java.util.List; import java.util.Objects; +import java.util.function.Function; public class CapabilityHelpers { public static final String APPIUM_PREFIX = "appium:"; @@ -47,7 +49,7 @@ public static T getCapability(Capabilities caps, String name, Class expec if (caps.getCapability(capName) == null) { continue; } - + if (expectedType == String.class) { return expectedType.cast(String.valueOf(caps.getCapability(capName))); } @@ -71,8 +73,8 @@ public static Boolean toSafeBoolean(Object value) { return null; } return (value instanceof String) - ? ((String) value).equalsIgnoreCase("true") - : Objects.equals(value, true); + ? Boolean.parseBoolean((String) value) + : Objects.equals(value, true); } /** @@ -86,7 +88,7 @@ public static Boolean toSafeBoolean(Object value) { public static Integer toSafeInteger(Object value) { if (value instanceof String) { return Integer.parseInt((String) value); - } else if (value instanceof Number) { + } else if (value instanceof Number) { return ((Number) value).intValue(); } return null; @@ -103,9 +105,37 @@ public static Integer toSafeInteger(Object value) { public static Long toSafeLong(Object value) { if (value instanceof String) { return Long.parseLong((String) value); - } else if (value instanceof Number) { + } else if (value instanceof Number) { return ((Number) value).longValue(); } return null; } + + /** + * Converts generic capability value to duration without + * throwing exceptions. The value is assumed to be + * measured in milliseconds. + * + * @param value The capability value. + * @return null is the passed value is null otherwise the converted value. + */ + @Nullable + public static Duration toSafeDuration(Object value) { + return toSafeDuration(value, Duration::ofMillis); + } + + /** + * Converts generic capability value to duration without + * throwing exceptions. + * + * @param value The capability value. + * @param converter Converts the numeric value to a Duration instance. + * @return null is the passed value is null otherwise the converted value. + */ + @Nullable + public static Duration toSafeDuration(Object value, + Function converter) { + Long v = toSafeLong(value); + return v == null ? null : converter.apply(v); + } } diff --git a/src/main/java/io/appium/java_client/remote/options/SupportsAutoWebViewOption.java b/src/main/java/io/appium/java_client/remote/options/SupportsAutoWebViewOption.java index e51dbbecb..8a5d7e9fd 100644 --- a/src/main/java/io/appium/java_client/remote/options/SupportsAutoWebViewOption.java +++ b/src/main/java/io/appium/java_client/remote/options/SupportsAutoWebViewOption.java @@ -31,7 +31,7 @@ public interface SupportsAutoWebViewOption> extends * * @return self instance for chaining. */ - default T setAutoWebview() { + default T autoWebview() { return setAutoWebview(true); } diff --git a/src/main/java/io/appium/java_client/remote/options/SupportsClearSystemFilesOption.java b/src/main/java/io/appium/java_client/remote/options/SupportsClearSystemFilesOption.java index cbb63f5a4..0d652a1da 100644 --- a/src/main/java/io/appium/java_client/remote/options/SupportsClearSystemFilesOption.java +++ b/src/main/java/io/appium/java_client/remote/options/SupportsClearSystemFilesOption.java @@ -31,7 +31,7 @@ public interface SupportsClearSystemFilesOption> extend * * @return this MobileOptions, for chaining. */ - default T setClearSystemFiles() { + default T clearSystemFiles() { return setClearSystemFiles(true); } diff --git a/src/main/java/io/appium/java_client/remote/options/SupportsEnablePerformanceLoggingOption.java b/src/main/java/io/appium/java_client/remote/options/SupportsEnablePerformanceLoggingOption.java index 34f8c4dd7..cf3601925 100644 --- a/src/main/java/io/appium/java_client/remote/options/SupportsEnablePerformanceLoggingOption.java +++ b/src/main/java/io/appium/java_client/remote/options/SupportsEnablePerformanceLoggingOption.java @@ -31,7 +31,7 @@ public interface SupportsEnablePerformanceLoggingOption * * @return self instance for chaining. */ - default T setEnablePerformanceLogging() { + default T enablePerformanceLogging() { return setEnablePerformanceLogging(true); } diff --git a/src/main/java/io/appium/java_client/remote/options/SupportsFullResetOption.java b/src/main/java/io/appium/java_client/remote/options/SupportsFullResetOption.java index 7dffc301b..667bb1f3d 100644 --- a/src/main/java/io/appium/java_client/remote/options/SupportsFullResetOption.java +++ b/src/main/java/io/appium/java_client/remote/options/SupportsFullResetOption.java @@ -31,7 +31,7 @@ public interface SupportsFullResetOption> extends * * @return self instance for chaining. */ - default T setFullReset() { + default T fullReset() { return setFullReset(true); } diff --git a/src/main/java/io/appium/java_client/remote/options/SupportsNewCommandTimeoutOption.java b/src/main/java/io/appium/java_client/remote/options/SupportsNewCommandTimeoutOption.java index f452341d9..5cb8a56c5 100644 --- a/src/main/java/io/appium/java_client/remote/options/SupportsNewCommandTimeoutOption.java +++ b/src/main/java/io/appium/java_client/remote/options/SupportsNewCommandTimeoutOption.java @@ -21,6 +21,8 @@ import java.time.Duration; import java.util.Optional; +import static io.appium.java_client.internal.CapabilityHelpers.toSafeDuration; + public interface SupportsNewCommandTimeoutOption> extends Capabilities, CanSetCapability { String NEW_COMMAND_TIMEOUT = "newCommandTimeout"; @@ -41,9 +43,8 @@ default T setNewCommandTimeout(Duration duration) { * @return allowed time before seeing a new command. */ default Optional getNewCommandTimeout() { - Object duration = getCapability(NEW_COMMAND_TIMEOUT); - return duration == null - ? Optional.empty() - : Optional.of(Duration.ofSeconds(Long.parseLong("" + duration))); + return Optional.ofNullable( + toSafeDuration(getCapability(NEW_COMMAND_TIMEOUT), Duration::ofSeconds) + ); } } diff --git a/src/main/java/io/appium/java_client/remote/options/SupportsNoResetOption.java b/src/main/java/io/appium/java_client/remote/options/SupportsNoResetOption.java index d386690c5..9116cac48 100644 --- a/src/main/java/io/appium/java_client/remote/options/SupportsNoResetOption.java +++ b/src/main/java/io/appium/java_client/remote/options/SupportsNoResetOption.java @@ -31,7 +31,7 @@ public interface SupportsNoResetOption> extends * * @return self instance for chaining. */ - default T setNoReset() { + default T noReset() { return setNoReset(true); } diff --git a/src/main/java/io/appium/java_client/remote/options/SupportsOrientationOption.java b/src/main/java/io/appium/java_client/remote/options/SupportsOrientationOption.java index b587bf9fa..e27acbdcf 100644 --- a/src/main/java/io/appium/java_client/remote/options/SupportsOrientationOption.java +++ b/src/main/java/io/appium/java_client/remote/options/SupportsOrientationOption.java @@ -41,12 +41,10 @@ default T setOrientation(ScreenOrientation orientation) { * @return ScreenOrientation of the app. */ default Optional getOrientation() { - Object value = getCapability(ORIENTATION_OPTION); - if (value == null) { - return Optional.empty(); - } - return Optional.of(value instanceof ScreenOrientation - ? (ScreenOrientation) value - : ScreenOrientation.valueOf(((String) value).toUpperCase())); + return Optional.ofNullable(getCapability(ORIENTATION_OPTION)) + .map((v) -> v instanceof ScreenOrientation + ? (ScreenOrientation) v + : ScreenOrientation.valueOf((String.valueOf(v)).toUpperCase()) + ); } } diff --git a/src/main/java/io/appium/java_client/remote/options/SupportsPrintPageSourceOnFindFailureOption.java b/src/main/java/io/appium/java_client/remote/options/SupportsPrintPageSourceOnFindFailureOption.java index b9401302a..3d82738c5 100644 --- a/src/main/java/io/appium/java_client/remote/options/SupportsPrintPageSourceOnFindFailureOption.java +++ b/src/main/java/io/appium/java_client/remote/options/SupportsPrintPageSourceOnFindFailureOption.java @@ -31,7 +31,7 @@ public interface SupportsPrintPageSourceOnFindFailureOption Date: Mon, 25 Oct 2021 06:12:11 +0200 Subject: [PATCH 3/6] Rename --- .../remote/options/SupportsNewCommandTimeoutOption.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/appium/java_client/remote/options/SupportsNewCommandTimeoutOption.java b/src/main/java/io/appium/java_client/remote/options/SupportsNewCommandTimeoutOption.java index 5cb8a56c5..2a0c5ab2b 100644 --- a/src/main/java/io/appium/java_client/remote/options/SupportsNewCommandTimeoutOption.java +++ b/src/main/java/io/appium/java_client/remote/options/SupportsNewCommandTimeoutOption.java @@ -25,7 +25,7 @@ public interface SupportsNewCommandTimeoutOption> extends Capabilities, CanSetCapability { - String NEW_COMMAND_TIMEOUT = "newCommandTimeout"; + String NEW_COMMAND_TIMEOUT_OPTION = "newCommandTimeout"; /** * Set the timeout for new commands. @@ -34,7 +34,7 @@ public interface SupportsNewCommandTimeoutOption> exten * @return self instance for chaining. */ default T setNewCommandTimeout(Duration duration) { - return amend(NEW_COMMAND_TIMEOUT, duration.getSeconds()); + return amend(NEW_COMMAND_TIMEOUT_OPTION, duration.getSeconds()); } /** @@ -44,7 +44,7 @@ default T setNewCommandTimeout(Duration duration) { */ default Optional getNewCommandTimeout() { return Optional.ofNullable( - toSafeDuration(getCapability(NEW_COMMAND_TIMEOUT), Duration::ofSeconds) + toSafeDuration(getCapability(NEW_COMMAND_TIMEOUT_OPTION), Duration::ofSeconds) ); } } From e791ab7a5178a44ed8f39188bfb3157d624aab07 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Mon, 25 Oct 2021 06:22:52 +0200 Subject: [PATCH 4/6] Make converters more strict --- .../SupportsAndroidInstallTimeoutOption.java | 5 ++- .../app/SupportsAppDurationOption.java | 4 +- .../SupportsRemoteAppsCacheLimitOption.java | 5 +-- .../server/SupportsSystemPortOption.java | 4 +- ...iautomator2ServerInstallTimeoutOption.java | 4 +- ...Uiautomator2ServerLaunchTimeoutOption.java | 4 +- ...tsUiautomator2ServerReadTimeoutOption.java | 4 +- .../internal/CapabilityHelpers.java | 39 ++++++++++++------- .../SupportsNewCommandTimeoutOption.java | 4 +- 9 files changed, 41 insertions(+), 32 deletions(-) diff --git a/src/main/java/io/appium/java_client/android/options/app/SupportsAndroidInstallTimeoutOption.java b/src/main/java/io/appium/java_client/android/options/app/SupportsAndroidInstallTimeoutOption.java index ed1455685..40a588c2e 100644 --- a/src/main/java/io/appium/java_client/android/options/app/SupportsAndroidInstallTimeoutOption.java +++ b/src/main/java/io/appium/java_client/android/options/app/SupportsAndroidInstallTimeoutOption.java @@ -16,6 +16,7 @@ package io.appium.java_client.android.options.app; +import io.appium.java_client.internal.CapabilityHelpers; import io.appium.java_client.remote.options.BaseOptions; import io.appium.java_client.remote.options.CanSetCapability; import org.openqa.selenium.Capabilities; @@ -23,7 +24,7 @@ import java.time.Duration; import java.util.Optional; -import static io.appium.java_client.internal.CapabilityHelpers.toSafeDuration; +import static io.appium.java_client.internal.CapabilityHelpers.toDuration; public interface SupportsAndroidInstallTimeoutOption> extends Capabilities, CanSetCapability { @@ -47,7 +48,7 @@ default T setAndroidInstallTimeout(Duration installTimeout) { */ default Optional getAndroidInstallTimeout() { return Optional.ofNullable( - toSafeDuration(getCapability(ANDROID_INSTALL_TIMEOUT_OPTION)) + CapabilityHelpers.toDuration(getCapability(ANDROID_INSTALL_TIMEOUT_OPTION)) ); } } diff --git a/src/main/java/io/appium/java_client/android/options/app/SupportsAppDurationOption.java b/src/main/java/io/appium/java_client/android/options/app/SupportsAppDurationOption.java index eb0cb078c..3a3bce7b7 100644 --- a/src/main/java/io/appium/java_client/android/options/app/SupportsAppDurationOption.java +++ b/src/main/java/io/appium/java_client/android/options/app/SupportsAppDurationOption.java @@ -23,7 +23,7 @@ import java.time.Duration; import java.util.Optional; -import static io.appium.java_client.internal.CapabilityHelpers.toSafeDuration; +import static io.appium.java_client.internal.CapabilityHelpers.toDuration; public interface SupportsAppDurationOption> extends Capabilities, CanSetCapability { @@ -46,6 +46,6 @@ default T setAppWaitDuration(Duration appWaitDuration) { * @return Package identifier. */ default Optional getAppWaitDuration() { - return Optional.ofNullable(toSafeDuration(getCapability(APP_WAIT_DURATION_OPTION))); + return Optional.ofNullable(toDuration(getCapability(APP_WAIT_DURATION_OPTION))); } } diff --git a/src/main/java/io/appium/java_client/android/options/app/SupportsRemoteAppsCacheLimitOption.java b/src/main/java/io/appium/java_client/android/options/app/SupportsRemoteAppsCacheLimitOption.java index 137c0f279..e44e8fcf4 100644 --- a/src/main/java/io/appium/java_client/android/options/app/SupportsRemoteAppsCacheLimitOption.java +++ b/src/main/java/io/appium/java_client/android/options/app/SupportsRemoteAppsCacheLimitOption.java @@ -22,8 +22,7 @@ import java.util.Optional; -import static io.appium.java_client.internal.CapabilityHelpers.toSafeInteger; -import static io.appium.java_client.internal.CapabilityHelpers.toSafeLong; +import static io.appium.java_client.internal.CapabilityHelpers.toInteger; public interface SupportsRemoteAppsCacheLimitOption> extends Capabilities, CanSetCapability { @@ -49,6 +48,6 @@ default T setRemoteAppsCacheLimit(int limit) { * @return The maximum amount of cached apps. */ default Optional getRemoteAppsCacheLimit() { - return Optional.ofNullable(toSafeInteger(getCapability(REMOTE_APPS_CACHE_LIMIT_OPTION))); + return Optional.ofNullable(toInteger(getCapability(REMOTE_APPS_CACHE_LIMIT_OPTION))); } } diff --git a/src/main/java/io/appium/java_client/android/options/server/SupportsSystemPortOption.java b/src/main/java/io/appium/java_client/android/options/server/SupportsSystemPortOption.java index b3377d018..cf638ff9f 100644 --- a/src/main/java/io/appium/java_client/android/options/server/SupportsSystemPortOption.java +++ b/src/main/java/io/appium/java_client/android/options/server/SupportsSystemPortOption.java @@ -22,7 +22,7 @@ import java.util.Optional; -import static io.appium.java_client.internal.CapabilityHelpers.toSafeInteger; +import static io.appium.java_client.internal.CapabilityHelpers.toInteger; public interface SupportsSystemPortOption> extends Capabilities, CanSetCapability { @@ -47,6 +47,6 @@ default T setSystemPort(int port) { * @return System port value */ default Optional getSystemPort() { - return Optional.ofNullable(toSafeInteger(getCapability(SYSTEM_PORT_OPTION))); + return Optional.ofNullable(toInteger(getCapability(SYSTEM_PORT_OPTION))); } } diff --git a/src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerInstallTimeoutOption.java b/src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerInstallTimeoutOption.java index 5c8cc7138..dac3b3b38 100644 --- a/src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerInstallTimeoutOption.java +++ b/src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerInstallTimeoutOption.java @@ -23,7 +23,7 @@ import java.time.Duration; import java.util.Optional; -import static io.appium.java_client.internal.CapabilityHelpers.toSafeDuration; +import static io.appium.java_client.internal.CapabilityHelpers.toDuration; public interface SupportsUiautomator2ServerInstallTimeoutOption> extends Capabilities, CanSetCapability { @@ -47,7 +47,7 @@ default T setUiautomator2ServerInstallTimeout(Duration timeout) { */ default Optional getUiautomator2ServerInstallTimeout() { return Optional.ofNullable( - toSafeDuration(getCapability(UIAUTOMATOR2_SERVER_INSTALL_TIMEOUT_OPTION)) + toDuration(getCapability(UIAUTOMATOR2_SERVER_INSTALL_TIMEOUT_OPTION)) ); } } diff --git a/src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerLaunchTimeoutOption.java b/src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerLaunchTimeoutOption.java index 4e44138a7..ea12f0d09 100644 --- a/src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerLaunchTimeoutOption.java +++ b/src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerLaunchTimeoutOption.java @@ -23,7 +23,7 @@ import java.time.Duration; import java.util.Optional; -import static io.appium.java_client.internal.CapabilityHelpers.toSafeDuration; +import static io.appium.java_client.internal.CapabilityHelpers.toDuration; public interface SupportsUiautomator2ServerLaunchTimeoutOption> extends Capabilities, CanSetCapability { @@ -47,7 +47,7 @@ default T setUiautomator2ServerLaunchTimeout(Duration timeout) { */ default Optional getUiautomator2ServerLaunchTimeout() { return Optional.ofNullable( - toSafeDuration(getCapability(UIAUTOMATOR2_SERVER_LAUNCH_TIMEOUT_OPTION)) + toDuration(getCapability(UIAUTOMATOR2_SERVER_LAUNCH_TIMEOUT_OPTION)) ); } } diff --git a/src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerReadTimeoutOption.java b/src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerReadTimeoutOption.java index 07ce85ef1..699b73c48 100644 --- a/src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerReadTimeoutOption.java +++ b/src/main/java/io/appium/java_client/android/options/server/SupportsUiautomator2ServerReadTimeoutOption.java @@ -23,7 +23,7 @@ import java.time.Duration; import java.util.Optional; -import static io.appium.java_client.internal.CapabilityHelpers.toSafeDuration; +import static io.appium.java_client.internal.CapabilityHelpers.toDuration; public interface SupportsUiautomator2ServerReadTimeoutOption> extends Capabilities, CanSetCapability { @@ -49,7 +49,7 @@ default T setUiautomator2ServerReadTimeout(Duration timeout) { */ default Optional getUiautomator2ServerReadTimeout() { return Optional.ofNullable( - toSafeDuration(getCapability(UIAUTOMATOR2_SERVER_READ_TIMEOUT_OPTION)) + toDuration(getCapability(UIAUTOMATOR2_SERVER_READ_TIMEOUT_OPTION)) ); } } diff --git a/src/main/java/io/appium/java_client/internal/CapabilityHelpers.java b/src/main/java/io/appium/java_client/internal/CapabilityHelpers.java index 4a994d2cf..a7ecd3891 100644 --- a/src/main/java/io/appium/java_client/internal/CapabilityHelpers.java +++ b/src/main/java/io/appium/java_client/internal/CapabilityHelpers.java @@ -78,20 +78,22 @@ public static Boolean toSafeBoolean(Object value) { } /** - * Converts generic capability value to integer without - * throwing exceptions. + * Converts generic capability value to integer. * * @param value The capability value. + * @throws NumberFormatException If the given value cannot be parsed + * to a valid integer. * @return null is the passed value is null otherwise the converted value. */ @Nullable - public static Integer toSafeInteger(Object value) { - if (value instanceof String) { - return Integer.parseInt((String) value); + public static Integer toInteger(Object value) { + if (value == null) { + return null; } else if (value instanceof Number) { return ((Number) value).intValue(); + } else { + return Integer.parseInt(String.valueOf(value)); } - return null; } /** @@ -99,16 +101,19 @@ public static Integer toSafeInteger(Object value) { * throwing exceptions. * * @param value The capability value. + * @throws NumberFormatException If the given value cannot be parsed + * to a valid long. * @return null is the passed value is null otherwise the converted value. */ @Nullable - public static Long toSafeLong(Object value) { - if (value instanceof String) { - return Long.parseLong((String) value); + public static Long toLong(Object value) { + if (value == null) { + return null; } else if (value instanceof Number) { return ((Number) value).longValue(); + } else { + return Long.parseLong(String.valueOf(value)); } - return null; } /** @@ -117,11 +122,13 @@ public static Long toSafeLong(Object value) { * measured in milliseconds. * * @param value The capability value. + * @throws NumberFormatException If the given value cannot be parsed + * to a valid number. * @return null is the passed value is null otherwise the converted value. */ @Nullable - public static Duration toSafeDuration(Object value) { - return toSafeDuration(value, Duration::ofMillis); + public static Duration toDuration(Object value) { + return toDuration(value, Duration::ofMillis); } /** @@ -130,12 +137,14 @@ public static Duration toSafeDuration(Object value) { * * @param value The capability value. * @param converter Converts the numeric value to a Duration instance. + * @throws NumberFormatException If the given value cannot be parsed + * to a valid number. * @return null is the passed value is null otherwise the converted value. */ @Nullable - public static Duration toSafeDuration(Object value, - Function converter) { - Long v = toSafeLong(value); + public static Duration toDuration(Object value, + Function converter) { + Long v = toLong(value); return v == null ? null : converter.apply(v); } } diff --git a/src/main/java/io/appium/java_client/remote/options/SupportsNewCommandTimeoutOption.java b/src/main/java/io/appium/java_client/remote/options/SupportsNewCommandTimeoutOption.java index 2a0c5ab2b..f9358fa95 100644 --- a/src/main/java/io/appium/java_client/remote/options/SupportsNewCommandTimeoutOption.java +++ b/src/main/java/io/appium/java_client/remote/options/SupportsNewCommandTimeoutOption.java @@ -21,7 +21,7 @@ import java.time.Duration; import java.util.Optional; -import static io.appium.java_client.internal.CapabilityHelpers.toSafeDuration; +import static io.appium.java_client.internal.CapabilityHelpers.toDuration; public interface SupportsNewCommandTimeoutOption> extends Capabilities, CanSetCapability { @@ -44,7 +44,7 @@ default T setNewCommandTimeout(Duration duration) { */ default Optional getNewCommandTimeout() { return Optional.ofNullable( - toSafeDuration(getCapability(NEW_COMMAND_TIMEOUT_OPTION), Duration::ofSeconds) + toDuration(getCapability(NEW_COMMAND_TIMEOUT_OPTION), Duration::ofSeconds) ); } } From 950213f41f269bd9844099e14e7ddd1f5fc9a8e4 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Mon, 25 Oct 2021 06:24:47 +0200 Subject: [PATCH 5/6] Tune toBoolean conversion --- .../io/appium/java_client/internal/CapabilityHelpers.java | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/main/java/io/appium/java_client/internal/CapabilityHelpers.java b/src/main/java/io/appium/java_client/internal/CapabilityHelpers.java index a7ecd3891..4e8732e89 100644 --- a/src/main/java/io/appium/java_client/internal/CapabilityHelpers.java +++ b/src/main/java/io/appium/java_client/internal/CapabilityHelpers.java @@ -22,7 +22,6 @@ import java.time.Duration; import java.util.ArrayList; import java.util.List; -import java.util.Objects; import java.util.function.Function; public class CapabilityHelpers { @@ -69,12 +68,7 @@ public static T getCapability(Capabilities caps, String name, Class expec */ @Nullable public static Boolean toSafeBoolean(Object value) { - if (value == null) { - return null; - } - return (value instanceof String) - ? Boolean.parseBoolean((String) value) - : Objects.equals(value, true); + return value == null ? null : Boolean.parseBoolean(String.valueOf(value)); } /** From 3005b1de3afb9c7ca13133ad2a6c49fe68f8aad4 Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Mon, 25 Oct 2021 06:30:12 +0200 Subject: [PATCH 6/6] Fix styling --- .../java_client/internal/CapabilityHelpers.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main/java/io/appium/java_client/internal/CapabilityHelpers.java b/src/main/java/io/appium/java_client/internal/CapabilityHelpers.java index 4e8732e89..106b96c59 100644 --- a/src/main/java/io/appium/java_client/internal/CapabilityHelpers.java +++ b/src/main/java/io/appium/java_client/internal/CapabilityHelpers.java @@ -75,8 +75,7 @@ public static Boolean toSafeBoolean(Object value) { * Converts generic capability value to integer. * * @param value The capability value. - * @throws NumberFormatException If the given value cannot be parsed - * to a valid integer. + * @throws NumberFormatException If the given value cannot be parsed to a valid integer. * @return null is the passed value is null otherwise the converted value. */ @Nullable @@ -95,8 +94,7 @@ public static Integer toInteger(Object value) { * throwing exceptions. * * @param value The capability value. - * @throws NumberFormatException If the given value cannot be parsed - * to a valid long. + * @throws NumberFormatException If the given value cannot be parsed to a valid long. * @return null is the passed value is null otherwise the converted value. */ @Nullable @@ -116,8 +114,7 @@ public static Long toLong(Object value) { * measured in milliseconds. * * @param value The capability value. - * @throws NumberFormatException If the given value cannot be parsed - * to a valid number. + * @throws NumberFormatException If the given value cannot be parsed to a valid number. * @return null is the passed value is null otherwise the converted value. */ @Nullable @@ -131,8 +128,7 @@ public static Duration toDuration(Object value) { * * @param value The capability value. * @param converter Converts the numeric value to a Duration instance. - * @throws NumberFormatException If the given value cannot be parsed - * to a valid number. + * @throws NumberFormatException If the given value cannot be parsed to a valid number. * @return null is the passed value is null otherwise the converted value. */ @Nullable