@Override public Response execute(String driverCommand) { return driver.execute(driverCommand); } };
@Override public String getContext() { String contextName = String.valueOf(execute(DriverCommand.GET_CURRENT_CONTEXT_HANDLE).getValue()); if ("null".equalsIgnoreCase(contextName)) { return null; } return contextName; }
/** * 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 Response execute(String driverCommand, Map<String, ?> parameters) { return driver.execute(driverCommand, parameters); }
@Override public void rotate(ScreenOrientation orientation) { execute(DriverCommand.SET_SCREEN_ORIENTATION, ImmutableMap.of("orientation", orientation.value().toUpperCase())); }
@Override public void rotate(DeviceRotation rotation) { execute(DriverCommand.SET_SCREEN_ROTATION, rotation.parameters()); }
@Override public WebDriver context(String name) { checkNotNull(name, "Must supply a context name"); try { execute(DriverCommand.SWITCH_TO_CONTEXT, ImmutableMap.of("name", name)); return this; } catch (WebDriverException e) { throw new NoSuchContextException(e.getMessage(), e); } }
@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); } }
@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; }
/** * Executes a command * * @param driverCommand Driver command name to execute * @param parameters Parameters used to execute the command * @return Response of the execution */ public Response execute(String driverCommand, Map<String, ?> parameters) { return this.driver.execute(driverCommand, parameters); }
/** * 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 String getContext() { String contextName = String.valueOf(execute(DriverCommand.GET_CURRENT_CONTEXT_HANDLE).getValue()); if ("null".equalsIgnoreCase(contextName)) { return null; } return contextName; }
@Override public void rotate(ScreenOrientation orientation) { execute(DriverCommand.SET_SCREEN_ORIENTATION, ImmutableMap.of("orientation", orientation.value().toUpperCase())); }
@Override public void rotate(DeviceRotation rotation) { execute(DriverCommand.SET_SCREEN_ROTATION, rotation.parameters()); }
@Override public WebDriver context(String name) { checkNotNull(name, "Must supply a context name"); try { execute(DriverCommand.SWITCH_TO_CONTEXT, ImmutableMap.of("name", name)); return this; } catch (WebDriverException e) { throw new NoSuchContextException(e.getMessage(), e); } }
@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); } }
@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; }