|
| 1 | +/*************************************************************************************************** |
| 2 | + * |
| 3 | + * Copyright (c) 2025 Universitat Politecnica de Valencia - www.upv.es |
| 4 | + * Copyright (c) 2025 Open Universiteit - www.ou.nl |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions are met: |
| 8 | + * |
| 9 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 10 | + * this list of conditions and the following disclaimer. |
| 11 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | + * notice, this list of conditions and the following disclaimer in the |
| 13 | + * documentation and/or other materials provided with the distribution. |
| 14 | + * 3. Neither the name of the copyright holder nor the names of its |
| 15 | + * contributors may be used to endorse or promote products derived from |
| 16 | + * this software without specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 22 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | + * POSSIBILITY OF SUCH DAMAGE. |
| 29 | + *******************************************************************************************************/ |
| 30 | + |
| 31 | +package org.testar.monkey.alayer.android; |
| 32 | + |
| 33 | +import com.google.gson.JsonObject; |
| 34 | +import com.google.gson.JsonParser; |
| 35 | +import org.openqa.selenium.remote.DesiredCapabilities; |
| 36 | + |
| 37 | +import java.io.File; |
| 38 | +import java.io.FileReader; |
| 39 | +import java.io.IOException; |
| 40 | +import java.util.Objects; |
| 41 | + |
| 42 | +/** |
| 43 | + * Factory responsible for creating Appium DesiredCapabilities |
| 44 | + * and determining the Appium server URL from a JSON configuration. |
| 45 | + */ |
| 46 | +public class AndroidCapabilitiesFactory { |
| 47 | + |
| 48 | + private final String defaultAppiumUrl; |
| 49 | + |
| 50 | + /** |
| 51 | + * @param defaultAppiumUrl the URL that will be used when the JSON does not override it. |
| 52 | + */ |
| 53 | + public AndroidCapabilitiesFactory(String defaultAppiumUrl) { |
| 54 | + this.defaultAppiumUrl = Objects.requireNonNull(defaultAppiumUrl); |
| 55 | + } |
| 56 | + |
| 57 | + public Result fromJsonFile(String capabilitiesJsonFile) { |
| 58 | + try (FileReader reader = new FileReader(capabilitiesJsonFile)) { |
| 59 | + JsonObject json = JsonParser.parseReader(reader).getAsJsonObject(); |
| 60 | + return fromJsonObject(json); |
| 61 | + } catch (IOException | IllegalStateException e) { |
| 62 | + System.err.println("ERROR: Exception reading Appium Desired Capabilities from JSON file: " + capabilitiesJsonFile); |
| 63 | + e.printStackTrace(); |
| 64 | + |
| 65 | + // Preserve previous behaviour: return empty capabilities and keep the default URL. |
| 66 | + return new Result(new DesiredCapabilities(), defaultAppiumUrl); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + Result fromJsonObject(JsonObject json) { |
| 71 | + DesiredCapabilities cap = new DesiredCapabilities(); |
| 72 | + |
| 73 | + // https://appium.io/docs/en/2.0/guides/caps/ |
| 74 | + cap.setCapability("platformName", getString(json, "platformName", "Android")); |
| 75 | + |
| 76 | + cap.setCapability("appium:deviceName", getString(json, "deviceName", "Android Emulator")); |
| 77 | + cap.setCapability("appium:automationName", getString(json, "automationName", "UiAutomator2")); |
| 78 | + cap.setCapability("appium:newCommandTimeout", getInt(json, "newCommandTimeout", 600)); |
| 79 | + cap.setCapability("appium:autoGrantPermissions", getBool(json, "autoGrantPermissions", false)); |
| 80 | + |
| 81 | + String appiumUrl = defaultAppiumUrl; |
| 82 | + |
| 83 | + // If the APK is already installed we use appPackage identifier |
| 84 | + if (getBool(json, "isApkInstalled", false)) { |
| 85 | + String appPackage = getString(json, "appPackage", null); |
| 86 | + String appActivity = getString(json, "appActivity", null); |
| 87 | + |
| 88 | + if (appPackage == null || appPackage.isEmpty()) { |
| 89 | + throw new IllegalArgumentException("When isApkInstalled=true, 'appPackage' is required."); |
| 90 | + } |
| 91 | + if (appActivity == null || appActivity.isEmpty()) { |
| 92 | + throw new IllegalArgumentException(String.join("\n", |
| 93 | + "When isApkInstalled=true, 'appActivity' is required (multiple launcher activities can exist).", |
| 94 | + "", |
| 95 | + "How to find it on Windows:", |
| 96 | + "1) Manually open the app on the emulator.", |
| 97 | + "2) Run:", |
| 98 | + " adb shell dumpsys activity activities | findstr /R /C:\"ResumedActivity\" /C:\"topResumedActivity\"", |
| 99 | + "3) From the output, take the activity after the package name." |
| 100 | + )); |
| 101 | + } |
| 102 | + |
| 103 | + cap.setCapability("appium:appPackage", appPackage); |
| 104 | + cap.setCapability("appium:appActivity", appActivity); |
| 105 | + } else { |
| 106 | + // Else we need to install the APK |
| 107 | + String appPath = getString(json, "app", null); |
| 108 | + if (appPath == null || appPath.isEmpty()) { |
| 109 | + throw new IllegalArgumentException( |
| 110 | + "When isApkInstalled=false, 'app' (APK path or URL) must be provided."); |
| 111 | + } |
| 112 | + |
| 113 | + boolean isEmulatorDocker = getBool(json, "isEmulatorDocker", false); |
| 114 | + String ipAddressAppium = getString(json, "ipAddressAppium", null); |
| 115 | + |
| 116 | + // If emulator is running inside a docker use the APK raw URL |
| 117 | + if (isEmulatorDocker && ipAddressAppium != null && !ipAddressAppium.isEmpty()) { |
| 118 | + // Docker container (budtmo/docker-android) + Appium v2 do not use /wd/hub suffix anymore |
| 119 | + // It can be enabled using the APPIUM_ADDITIONAL_ARGS "--base-path /wd/hub" command |
| 120 | + cap.setCapability("appium:app", appPath); |
| 121 | + appiumUrl = "http://" + ipAddressAppium + ":4723/wd/hub"; |
| 122 | + } else { |
| 123 | + // Else, obtain the local directory that contains the APK file |
| 124 | + try { |
| 125 | + cap.setCapability("appium:app", new File(appPath).getCanonicalPath()); |
| 126 | + } catch (IOException e) { |
| 127 | + System.err.println("ERROR: Cannot resolve canonical path for APK: " + appPath); |
| 128 | + e.printStackTrace(); |
| 129 | + cap.setCapability("appium:app", appPath); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + return new Result(cap, appiumUrl); |
| 135 | + } |
| 136 | + |
| 137 | + private static String getString(JsonObject json, String key, String def) { |
| 138 | + return json.has(key) && !json.get(key).isJsonNull() ? json.get(key).getAsString() : def; |
| 139 | + } |
| 140 | + |
| 141 | + private static boolean getBool(JsonObject json, String key, boolean def) { |
| 142 | + return json.has(key) && !json.get(key).isJsonNull() ? json.get(key).getAsBoolean() : def; |
| 143 | + } |
| 144 | + |
| 145 | + private static int getInt(JsonObject json, String key, int def) { |
| 146 | + return json.has(key) && !json.get(key).isJsonNull() ? json.get(key).getAsInt() : def; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Value object returned by the factory so callers can configure both |
| 151 | + * the driver capabilities and the Appium server URL. |
| 152 | + */ |
| 153 | + public static final class Result { |
| 154 | + private final DesiredCapabilities capabilities; |
| 155 | + private final String appiumServerUrl; |
| 156 | + |
| 157 | + Result(DesiredCapabilities capabilities, String appiumServerUrl) { |
| 158 | + this.capabilities = Objects.requireNonNull(capabilities, "capabilities"); |
| 159 | + this.appiumServerUrl = Objects.requireNonNull(appiumServerUrl, "appiumServerUrl"); |
| 160 | + } |
| 161 | + |
| 162 | + public DesiredCapabilities getCapabilities() { |
| 163 | + return capabilities; |
| 164 | + } |
| 165 | + |
| 166 | + public String getAppiumServerUrl() { |
| 167 | + return appiumServerUrl; |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | +} |
0 commit comments