/** * Take screenshot of the current window. * * @param target The target type/format of the Screenshot * @return Screenshot of current window, in the requested format */ @Override public <X> X getScreenshotAs(OutputType<X> target) { // Get the screenshot as base64 and convert it to the requested type (i.e. OutputType<T>) String base64 = (String) execute(DriverCommand.SCREENSHOT).getValue(); return target.convertFromBase64Png(base64); }
"script", script, "args", Lists.newArrayList(convertedArgs)); return execute(COMMAND_EXECUTE_PHANTOM_SCRIPT, params).getValue();
private static <T> T handleResponse(Response response) { if (response != null) { return (T) response.getValue(); } return null; } }
/** * Gets device date and time for both iOS(host time is returned for simulators) and Android devices. * The default format since Appium 1.8.2 is `YYYY-MM-DDTHH:mm:ssZ`, which complies to ISO-8601. * * @return Device time string */ default String getDeviceTime() { Response response = execute(GET_DEVICE_TIME); return response.getValue().toString(); } }
@Override public String getContext() { String contextName = String.valueOf(execute(DriverCommand.GET_CURRENT_CONTEXT_HANDLE).getValue()); if ("null".equalsIgnoreCase(contextName)) { return null; } return contextName; }
/** * Gather the output from the previously started screen recording to a media file * with default options. * * @return Base-64 encoded content of the recorded media file. */ default String stopRecordingScreen() { return this.execute(STOP_RECORDING_SCREEN).getValue().toString(); } }
/** * Start asynchronous screen recording process with default options. * * @return Base-64 encoded content of the recorded media file. */ default String startRecordingScreen() { return this.execute(START_RECORDING_SCREEN).getValue().toString(); }
/** * Gets device date and time for both iOS(host time is returned for simulators) and Android devices. * * @param format The set of format specifiers. Read * https://momentjs.com/docs/ to get the full list of supported * datetime format specifiers. The default format is * `YYYY-MM-DDTHH:mm:ssZ`, which complies to ISO-8601 * @return Device time string */ default String getDeviceTime(String format) { Response response = execute(GET_DEVICE_TIME, ImmutableMap.of("format", format)); return response.getValue().toString(); }
@SuppressWarnings("unchecked") @Override public IOSBatteryInfo getBatteryInfo() { return new IOSBatteryInfo((Map<String, Object>) execute(EXECUTE_SCRIPT, ImmutableMap.of( "script", "mobile: batteryInfo", "args", Collections.emptyList())).getValue()); }
/** * Get settings stored for this test session It's probably better to use a * convenience function, rather than use this function directly. Try finding * the method for the specific setting you want to read. * * @return JsonObject, a straight-up hash of settings. */ @SuppressWarnings("unchecked") default Map<String, Object> getSettings() { Map.Entry<String, Map<String, ?>> keyValuePair = getSettingsCommand(); Response response = execute(keyValuePair.getKey(), keyValuePair.getValue()); return ImmutableMap.<String, Object>builder() .putAll(Map.class.cast(response.getValue())).build(); } }
/** * The current session details. * * @return a map with values that hold session details. */ @SuppressWarnings("unchecked") default Map<String, Object> getSessionDetails() { Response response = execute(GET_SESSION); Map<String, Object> resultMap = Map.class.cast(response.getValue()); //this filtering was added to clear returned result. //results of further operations should be simply interpreted by users return ImmutableMap.<String, Object>builder() .putAll(resultMap.entrySet() .stream().filter(entry -> { String key = entry.getKey(); Object value = entry.getValue(); return !isBlank(key) && value != null && !isBlank(String.valueOf(value)); }).collect(toMap(Map.Entry::getKey, Map.Entry::getValue))).build(); }
@SuppressWarnings("unchecked") @Override public AndroidBatteryInfo getBatteryInfo() { return new AndroidBatteryInfo((Map<String, Object>) execute(EXECUTE_SCRIPT, ImmutableMap.of( "script", "mobile: batteryInfo", "args", Collections.emptyList())).getValue()); }
@Override public Set<String> getContextHandles() { Response response = execute(DriverCommand.GET_CONTEXT_HANDLES); Object value = response.getValue(); try { List<String> returnedValues = (List<String>) value; return new LinkedHashSet<>(returnedValues); } catch (ClassCastException ex) { throw new WebDriverException( "Returned value cannot be converted to List<String>: " + value, ex); } }
/** * Pull a file from the simulator/device. * On iOS the server should have ifuse * libraries installed and configured properly for this feature to work * on real devices. * On Android the application under test should be * built with debuggable flag enabled in order to get access to its container * on the internal file system. * * @see <a href="https://github.com/libimobiledevice/ifuse">iFuse GitHub page6</a> * @see <a href="https://github.com/osxfuse/osxfuse/wiki/FAQ">osxFuse FAQ</a> * @see <a href="https://developer.android.com/studio/debug/">'Debug Your App' developer article</a> * * @param remotePath If the path starts with <em>@applicationId/</em>/ prefix, then the file * will be pulled from the root of the corresponding application container. * Otherwise the root folder is considered as / on Android and * on iOS it is a media folder root (real devices only). * @return A byte array of Base64 encoded data. */ default byte[] pullFile(String remotePath) { Response response = execute(PULL_FILE, ImmutableMap.of("path", remotePath)); String base64String = response.getValue().toString(); return Base64.getDecoder().decode(base64String.getBytes(StandardCharsets.UTF_8)); }
/** * Pull a folder content from the simulator/device. * On iOS the server should have ifuse * libraries installed and configured properly for this feature to work * on real devices. * On Android the application under test should be * built with debuggable flag enabled in order to get access to its container * on the internal file system. * * @see <a href="https://github.com/libimobiledevice/ifuse">iFuse GitHub page6</a> * @see <a href="https://github.com/osxfuse/osxfuse/wiki/FAQ">osxFuse FAQ</a> * @see <a href="https://developer.android.com/studio/debug/">'Debug Your App' developer article</a> * * @param remotePath If the path starts with <em>@applicationId/</em> prefix, then the folder * will be pulled from the root of the corresponding application container. * Otherwise the root folder is considered as / on Android and * on iOS it is a media folder root (real devices only). * @return A byte array of Base64 encoded zip archive data. */ default byte[] pullFolder(String remotePath) { Response response = execute(PULL_FOLDER, ImmutableMap.of("path", remotePath)); String base64String = response.getValue().toString(); return Base64.getDecoder().decode(base64String.getBytes(StandardCharsets.UTF_8)); }
/** * This method executes a command supported by Appium JSONWP. * * @param commandName a JSONWP command * @param parameters is a map which contains parameter names as keys and parameter values * @return a command execution result */ public Object execute(String commandName, Map<String, ?> parameters) { Response response; if (parameters == null || parameters.isEmpty()) { response = driver.execute(commandName, ImmutableMap.of()); } else { response = driver.execute(commandName, parameters); } return response.getValue(); } }
@Override public ScreenOrientation getOrientation() { Response response = execute(DriverCommand.GET_SCREEN_ORIENTATION); String orientation = response.getValue().toString().toLowerCase(); if (orientation.equals(ScreenOrientation.LANDSCAPE.value())) { return ScreenOrientation.LANDSCAPE; } else if (orientation.equals(ScreenOrientation.PORTRAIT.value())) { return ScreenOrientation.PORTRAIT; } else { throw new WebDriverException("Unexpected orientation returned: " + orientation); } }
@Override public DeviceRotation rotation() { Response response = execute(DriverCommand.GET_SCREEN_ROTATION); DeviceRotation deviceRotation = new DeviceRotation((Map<String, Number>) response.getValue()); if (deviceRotation.getX() < 0 || deviceRotation.getY() < 0 || deviceRotation.getZ() < 0) { throw new WebDriverException("Unexpected orientation returned: " + deviceRotation); } return deviceRotation; }
@Override public String installExtension(Path path) { return (String) execute(ExtraCommands.INSTALL_EXTENSION, ImmutableMap.of("path", path.toAbsolutePath().toString(), "temporary", false)).getValue(); }
/** * Gets device date and time for both iOS(host time is returned for simulators) and Android devices. * The default format since Appium 1.8.2 is `YYYY-MM-DDTHH:mm:ssZ`, which complies to ISO-8601. * * @return Device time string */ default String getDeviceTime() { Response response = execute(GET_DEVICE_TIME); return response.getValue().toString(); } }