/** * @param command The {@link AndroidCommand} * @return {@link AndroidCommandResult} * @throws JSONException * @see * io.appium.android.bootstrap.CommandHandler#execute(io.appium.android.bootstrap.AndroidCommand) */ @Override public AndroidCommandResult execute(final AndroidCommand command) throws JSONException { initalize(); try { params = command.params(); // isElementCommand doesn't check to see if we actually have an element // so getElement is used instead. try { if (command.getElement() != null) { isElement = true; } } catch (final Exception e) { isElement = false; } if (isElement) { // extract x and y from the element. el = command.getElement(); final Rect bounds = el.getVisibleBounds(); clickX = bounds.centerX(); clickY = bounds.centerY(); } else { // no element so extract x and y from params final Object paramX = params.get("x"); final Object paramY = params.get("y"); double targetX = 0.5; double targetY = 0.5; if (paramX != null) { targetX = Double.parseDouble(paramX.toString()); } if (paramY != null) { targetY = Double.parseDouble(paramY.toString()); } final ArrayList<Integer> posVals = absPosFromCoords(new Double[] {targetX, targetY}); clickX = posVals.get(0); clickY = posVals.get(1); } if (executeTouchEvent()) { return getSuccessResult(true); } } catch (final UiObjectNotFoundException e) { return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage()); } catch (final ElementNotInHashException e) { return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage()); } catch (final Exception e) { return getErrorResult(e.getMessage()); } return getErrorResult("Failed to execute touch event"); }
/* * @param command The {@link AndroidCommand} used for this handler. * * @return {@link AndroidCommandResult} * * @throws JSONException * * @see io.appium.android.bootstrap.CommandHandler#execute(io.appium.android. * bootstrap.AndroidCommand) */ @Override public AndroidCommandResult execute(final AndroidCommand command) throws JSONException { final Hashtable<String, Object> params = command.params(); AndroidElement el; final String direction = params.get("direction").toString(); final Integer percent = (Integer) params.get("percent"); final Integer steps = (Integer) params.get("steps"); try { el = command.getElement(); if (el == null) { return getErrorResult( "Could not find an element with elementId: " + params.get("elementId")); } } catch (final Exception e) { // JSONException, NullPointerException, etc. return getErrorResult("Unknown error:" + e.getMessage()); } Logger.debug( "Pinching " + direction + " " + percent.toString() + "%" + " with steps: " + steps.toString()); boolean res; if (direction.equals("in")) { try { res = el.pinchIn(percent, steps); } catch (final UiObjectNotFoundException e) { return getErrorResult("Selector could not be matched to any UI element displayed"); } } else { try { res = el.pinchOut(percent, steps); } catch (final UiObjectNotFoundException e) { return getErrorResult("Selector could not be matched to any UI element displayed"); } } if (res) { return getSuccessResult(res); } else { return getErrorResult("Pinch did not complete successfully"); } }
/* * @param command The {@link AndroidCommand} used for this handler. * * @return {@link AndroidCommandResult} * * @throws JSONException * * @see io.appium.android.bootstrap.CommandHandler#execute(io.appium.android. * bootstrap.AndroidCommand) */ @Override public AndroidCommandResult execute(final AndroidCommand command) throws JSONException { if (command.isElementCommand()) { // Only makes sense on an element try { final Hashtable<String, Object> params = command.params(); final AndroidElement el = command.getElement(); final String text = params.get("text").toString(); return getSuccessResult(el.setText(text)); } catch (final UiObjectNotFoundException e) { return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage()); } catch (final ElementNotInHashException e) { return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage()); } } else { return getErrorResult("Unable to set text without an element."); } }
/* * @param command The {@link AndroidCommand} used for this handler. * * @return {@link AndroidCommandResult} * * @throws JSONException * * @see io.appium.android.bootstrap.CommandHandler#execute(io.appium.android. * bootstrap.AndroidCommand) */ @Override public AndroidCommandResult execute(final AndroidCommand command) throws JSONException { if (!command.isElementCommand()) { final Hashtable<String, Object> params = command.params(); final Integer xSpeed = (Integer) params.get("xSpeed"); final Integer ySpeed = (Integer) params.get("ySpeed"); final UiDevice d = UiDevice.getInstance(); final Integer screenX = d.getDisplayWidth(); final Integer screenY = d.getDisplayHeight(); final Integer startX = screenX / 2; final Integer startY = screenY / 2; final Double speedRatio = (double) xSpeed / ySpeed; Integer xOff; Integer yOff; if (speedRatio < 1) { yOff = screenY / 4; xOff = (int) ((double) screenX / 4 * speedRatio); } else { xOff = screenX / 4; yOff = (int) ((double) screenY / 4 / speedRatio); } final Integer endX = startX + Integer.signum(xSpeed) * xOff; final Integer endY = startY + Integer.signum(ySpeed) * yOff; final Double speed = Math.max(1250, Math.sqrt(xSpeed * xSpeed + ySpeed * ySpeed)); final Integer steps = 1250 / speed.intValue() + 1; final boolean res = d.swipe(startX, startY, endX, endY, steps); if (res) { return getSuccessResult(res); } else { return getErrorResult("Flick did not complete successfully"); } } else { return getErrorResult("Flick not yet implemented on the element level."); } }
/* * @param command The {@link AndroidCommand} * * @return {@link AndroidCommandResult} * * @throws JSONException * * @see io.appium.android.bootstrap.CommandHandler#execute(io.appium.android. * bootstrap.AndroidCommand) */ @Override public AndroidCommandResult execute(final AndroidCommand command) throws JSONException { if (command.isElementCommand()) { try { final AndroidElement el = command.getElement(); final boolean res = el.click(); return getSuccessResult(res); } catch (final UiObjectNotFoundException e) { return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage()); } catch (final ElementNotInHashException e) { return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage()); } } else { final Hashtable<String, Object> params = command.params(); final Double[] coords = { Double.parseDouble(params.get("x").toString()), Double.parseDouble(params.get("y").toString()) }; final ArrayList<Integer> posVals = absPosFromCoords(coords); final boolean res = UiDevice.getInstance().click(posVals.get(0), posVals.get(1)); return getSuccessResult(res); } }
/** * @param command The {@link AndroidCommand} * @return {@link AndroidCommandResult} * @throws JSONException * @see * io.appium.android.bootstrap.CommandHandler#execute(io.appium.android.bootstrap.AndroidCommand) */ @Override public AndroidCommandResult execute(final AndroidCommand command) throws JSONException { try { final Hashtable<String, Object> params = command.params(); AndroidElement el = null; int clickX = -1; int clickY = -1; boolean isElement = false; // isElementCommand doesn't check to see if we actually have an element // so getElement is used instead. try { if (command.getElement() != null) { isElement = true; } } catch (final Exception e) { } if (isElement) { // extract x and y from the element. el = command.getElement(); final Rect bounds = el.getVisibleBounds(); clickX = bounds.centerX(); clickY = bounds.centerY(); } else { // no element so extract x and y from params final Object paramX = params.get("x"); final Object paramY = params.get("y"); double targetX = 0.5; double targetY = 0.5; if (paramX != null) { targetX = Double.parseDouble(paramX.toString()); } if (paramY != null) { targetY = Double.parseDouble(paramY.toString()); } final ArrayList<Integer> posVals = absPosFromCoords(new Double[] {targetX, targetY}); clickX = posVals.get(0); clickY = posVals.get(1); } final Object paramDuration = params.get("duration"); int duration = 2000; // two seconds if (paramDuration != null) { duration = Integer.parseInt(paramDuration.toString()); } Logger.debug( "longClick using element? " + isElement + " x: " + clickX + ", y: " + clickY + ", duration: " + duration); if (correctLongClick(clickX, clickY, duration)) { return getSuccessResult(true); } // if correctLongClick failed and we have an element // then uiautomator's longClick is used as a fallback. if (isElement) { Logger.debug("Falling back to broken longClick"); final boolean res = el.longClick(); return getSuccessResult(res); } } catch (final UiObjectNotFoundException e) { return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage()); } catch (final ElementNotInHashException e) { return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage()); } catch (final Exception e) { return getErrorResult(e.getMessage()); } return getErrorResult("Failed to long click"); }
/* * @param command The {@link AndroidCommand} used for this handler. * * @return {@link AndroidCommandResult} * * @throws JSONException * * @see io.appium.android.bootstrap.CommandHandler#execute(io.appium.android. * bootstrap.AndroidCommand) */ @Override public AndroidCommandResult execute(final AndroidCommand command) throws JSONException { final Hashtable<String, Object> params = command.params(); // only makes sense on a device final Strategy strategy = Strategy.fromString((String) params.get("strategy")); final String text = (String) params.get("selector"); final String contextId = (String) params.get("context"); Logger.debug( "Finding " + text + " using " + strategy.toString() + " with the contextId: " + contextId); final Boolean multiple = (Boolean) params.get("multiple"); final boolean isXpath = strategy.equals("xpath"); if (isXpath) { final JSONArray xpathPath = (JSONArray) params.get("path"); final String xpathAttr = (String) params.get("attr"); final String xpathConstraint = (String) params.get("constraint"); final Boolean xpathSubstr = (Boolean) params.get("substr"); try { if (multiple) { final UiSelector sel = getSelectorForXpath(xpathPath, xpathAttr, xpathConstraint, xpathSubstr); return getSuccessResult(fetchElements(sel, contextId)); } else { final UiSelector sel = getSelectorForXpath(xpathPath, xpathAttr, xpathConstraint, xpathSubstr); return getSuccessResult(fetchElement(sel, contextId)); } } catch (final AndroidCommandException e) { return getErrorResult(e.getMessage()); } catch (final ElementNotFoundException e) { return getErrorResult(e.getMessage()); } catch (final ElementNotInHashException e) { return getErrorResult(e.getMessage()); } catch (final UiObjectNotFoundException e) { return getErrorResult(e.getMessage()); } } else { try { final UiSelector sel = getSelector(strategy, text, multiple); if (multiple) { return getSuccessResult(fetchElements(sel, contextId)); } else { return getSuccessResult(fetchElement(sel, contextId)); } } catch (final InvalidStrategyException e) { return getErrorResult(e.getMessage()); } catch (final ElementNotFoundException e) { return new AndroidCommandResult(WDStatus.NO_SUCH_ELEMENT, e.getMessage()); } catch (final AndroidCommandException e) { return getErrorResult(e.getMessage()); } catch (final ElementNotInHashException e) { return getErrorResult(e.getMessage()); } catch (final UiObjectNotFoundException e) { return getErrorResult(e.getMessage()); } } }