Best Cerberus-source code snippet using org.cerberus.service.appium.impl.Coordinates.Coordinates
Source:AppiumService.java
...146 public MessageEvent click(final Session session, final Identifier identifier) {147 try {148 final TouchAction action = new TouchAction(session.getAppiumDriver());149 if (identifier.isSameIdentifier(Identifier.Identifiers.COORDINATE)) {150 final Coordinates coordinates = getCoordinates(identifier);151 action.tap(PointOption.point(coordinates.getX(), coordinates.getY())).perform();152 } else {153 action.tap(ElementOption.element(getElement(session, identifier, false, false))).perform();154 }155 return new MessageEvent(MessageEventEnum.ACTION_SUCCESS_CLICK).resolveDescription("ELEMENT", identifier.toString());156 } catch (NoSuchElementException e) {157 if (LOG.isDebugEnabled()) {158 LOG.debug(e.getMessage());159 }160 return new MessageEvent(MessageEventEnum.ACTION_FAILED_CLICK_NO_SUCH_ELEMENT).resolveDescription("ELEMENT", identifier.toString());161 } catch (WebDriverException e) {162 LOG.warn(e.getMessage());163 return parseWebDriverException(e);164 }165 }166 /**167 * @author vertigo17168 * @param exception the exception need to be parsed by Cerberus169 * @return A new Event Message with selenium related description170 */171 private MessageEvent parseWebDriverException(WebDriverException exception) {172 MessageEvent mes;173 LOG.error(exception.toString(), exception);174 mes = new MessageEvent(MessageEventEnum.ACTION_FAILED_SELENIUM_CONNECTIVITY);175 mes.setDescription(mes.getDescription().replace("%ERROR%", exception.getMessage().split("\n")[0]));176 return mes;177 }178 /**179 * Get the {@link Coordinates} represented by the given {@link Identifier}180 *181 * @param identifier the {@link Identifier} to parse to get the182 * {@link Coordinates}183 * @return the {@link Coordinates} represented by the given184 * {@link Identifier}185 * @throws NoSuchElementException if no {@link Coordinates} can be found186 * inside the given {@link Identifier}187 */188 private Coordinates getCoordinates(final Identifier identifier) {189 if (identifier == null || !identifier.isSameIdentifier(Identifier.Identifiers.COORDINATE)) {190 throw new NoSuchElementException("Unable to get coordinates from a non coordinates identifier");191 }192 final Matcher coordinates = Identifier.Identifiers.COORDINATE_VALUE_PATTERN.matcher(identifier.getLocator());193 if (!coordinates.find()) {194 throw new NoSuchElementException("Bad coordinates format");195 }196 try {197 return new Coordinates(198 Integer.valueOf(coordinates.group("xCoordinate")),199 Integer.valueOf(coordinates.group("yCoordinate"))200 );201 } catch (NumberFormatException e) {202 throw new NoSuchElementException("Bad coordinates format", e);203 }204 }205 private By getBy(Identifier identifier) {206 LOG.debug("Finding selenium Element : " + identifier.getLocator() + " by : " + identifier.getIdentifier());207 if (identifier.getIdentifier().equalsIgnoreCase("id")) {208 return By.id(identifier.getLocator());209 } else if (identifier.getIdentifier().equalsIgnoreCase("name")) {210 return By.name(identifier.getLocator());211 } else if (identifier.getIdentifier().equalsIgnoreCase("class")) {212 return By.className(identifier.getLocator());213 } else if (identifier.getIdentifier().equalsIgnoreCase("css")) {214 return By.cssSelector(identifier.getLocator());215 } else if (identifier.getIdentifier().equalsIgnoreCase("xpath")) {216 return By.xpath(identifier.getLocator());217 } else if (identifier.getIdentifier().equalsIgnoreCase("link")) {218 return By.linkText(identifier.getLocator());219 } else if (identifier.getIdentifier().equalsIgnoreCase("data-cerberus")) {220 return By.xpath("//*[@data-cerberus='" + identifier.getLocator() + "']");221 } else if (identifier.getIdentifier().equalsIgnoreCase("accesibility-id")) {222 return MobileBy.AccessibilityId(identifier.getLocator());223 } else {224 throw new NoSuchElementException(identifier.getIdentifier());225 }226 }227 private WebElement getElement(Session session, Identifier identifier, boolean visible, boolean clickable) {228 AppiumDriver driver = session.getAppiumDriver();229 By locator = this.getBy(identifier);230 LOG.debug("Waiting for Element : " + identifier.getIdentifier() + "=" + identifier.getLocator());231 try {232 WebDriverWait wait = new WebDriverWait(driver, TimeUnit.MILLISECONDS.toSeconds(session.getCerberus_appium_wait_element()));233 if (visible) {234 if (clickable) {235 wait.until(ExpectedConditions.elementToBeClickable(locator));236 } else {237 wait.until(ExpectedConditions.visibilityOfElementLocated(locator));238 }239 } else {240 wait.until(ExpectedConditions.presenceOfElementLocated(locator));241 }242 } catch (TimeoutException exception) {243 LOG.fatal("Exception waiting for element :" + exception.toString());244 throw new NoSuchElementException(identifier.getIdentifier() + "=" + identifier.getLocator());245 }246 LOG.debug("Finding Element : " + identifier.getIdentifier() + "=" + identifier.getLocator());247 return driver.findElement(locator);248 }249 /**250 *251 * @param session252 * @param action253 * @return254 * @throws IllegalArgumentException255 */256 @Override257 public Direction getDirectionForSwipe(Session session, SwipeAction action) throws IllegalArgumentException {258 Dimension window = session.getAppiumDriver().manage().window().getSize();259 SwipeAction.Direction direction;260 switch (action.getActionType()) {261 case UP:262 direction = SwipeAction.Direction.fromLine(263 new Line2D.Double(264 window.getWidth() / 2,265 2 * window.getHeight() / 3,266 0,267 -window.getHeight() / 3268 )269 );270 break;271 case DOWN:272 direction = SwipeAction.Direction.fromLine(273 new Line2D.Double(274 window.getWidth() / 2,275 window.getHeight() / 3,276 0,277 window.getHeight() / 3278 )279 );280 break;281 case LEFT:282 direction = SwipeAction.Direction.fromLine(283 new Line2D.Double(284 2 * window.getWidth() / 3,285 window.getHeight() / 2,286 -window.getWidth() / 3,287 0288 )289 );290 break;291 case RIGHT:292 direction = SwipeAction.Direction.fromLine(293 new Line2D.Double(294 window.getWidth() / 3,295 window.getHeight() / 2,296 window.getWidth() / 3,297 0298 )299 );300 break;301 case CUSTOM:302 direction = action.getCustomDirection();303 break;304 default:305 throw new IllegalArgumentException("Unknown direction");306 }307 return direction;308 }309 @Override310 public MessageEvent scrollTo(Session session, Identifier element, String numberScrollDownMax) throws IllegalArgumentException {311 AppiumDriver driver = session.getAppiumDriver();312 MessageEvent message;313 try {314 message = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_SCROLLTO);315 int numberOfScrollDown = 8;316 try {317 numberOfScrollDown = Integer.parseInt(numberScrollDownMax);318 } catch (NumberFormatException e) {319 // do nothing320 }321 // check text322 if (element.getIdentifier().equals("text")) {323 scrollDown(driver, By.xpath("//*[contains(@text,'" + element.getLocator() + "')]"), numberOfScrollDown);324 } else {325 scrollDown(driver, this.getBy(element), numberOfScrollDown);326 }327 message.setDescription(message.getDescription().replace("%VALUE%", element.toString()));328 return message;329 } catch (Exception e) {330 LOG.error("An error occured during scroll to (element:" + element + ",numberScrollDownMax:" + numberScrollDownMax + ")", e);331 message = new MessageEvent(MessageEventEnum.ACTION_FAILED_GENERIC);332 message.setDescription(message.getDescription().replace("%DETAIL%", e.getMessage()));333 return message;334 }335 }336 /**337 * Scroll down and stop when element is present338 *339 * @param driver340 * @param element341 * @return342 */343 private boolean scrollDown(AppiumDriver driver, By element, int numberOfScrollDown) {344 int pressX = driver.manage().window().getSize().width / 2;345 int bottomY = driver.manage().window().getSize().height * 4 / 5;346 int topY = driver.manage().window().getSize().height / 8;347 int i = 0;348 do {349 boolean isPresent = driver.findElements(element).size() > 0;350 if (isPresent) {351 Object elmtObj = driver.findElements(element).get(0);352 if (elmtObj != null && ((MobileElement) elmtObj).isDisplayed()) {353 return true;354 }355 } else {356 scroll(driver, pressX, bottomY, pressX, topY);357 }358 i++;359 } while (i <= numberOfScrollDown);360 return false;361 }362 private void scroll(AppiumDriver driver, int fromX, int fromY, int toX, int toY) {363 TouchAction touchAction = new TouchAction(driver);364 touchAction.longPress(PointOption.point(fromX, fromY)).moveTo(PointOption.point(toX, toY)).release().perform();365 }366 public abstract String executeCommandString(Session session, String cmd, String args) throws IllegalArgumentException, JSONException;367 public String getElementPosition(Session session, Identifier identifier) {368 AppiumDriver driver = session.getAppiumDriver();369 MobileElement element = (MobileElement) driver.findElement(this.getBy(identifier));370 Point location = element.getLocation();371 return location.getX() + ";" + location.getY();372 }373 @Override374 public MessageEvent longPress(final Session session, final Identifier identifier, final Integer timeDuration) {375 try {376 final TouchAction action = new TouchAction(session.getAppiumDriver());377 if (identifier.isSameIdentifier(Identifier.Identifiers.COORDINATE)) {378 final Coordinates coordinates = getCoordinates(identifier);379 action.press(PointOption.point(coordinates.getX(), coordinates.getY())).waitAction(WaitOptions.waitOptions(Duration.ofMillis(timeDuration))).release().perform();380 } else {381 action.press(ElementOption.element(getElement(session, identifier, false, false))).waitAction(WaitOptions.waitOptions(Duration.ofMillis(timeDuration))).release().perform();382 }383 return new MessageEvent(MessageEventEnum.ACTION_SUCCESS_LONG_CLICK).resolveDescription("ELEMENT", identifier.toString());384 } catch (NoSuchElementException e) {385 if (LOG.isDebugEnabled()) {386 LOG.debug(e.getMessage());387 }388 return new MessageEvent(MessageEventEnum.ACTION_FAILED_LONG_CLICK_NO_SUCH_ELEMENT).resolveDescription("ELEMENT", identifier.toString());389 } catch (WebDriverException e) {390 LOG.warn(e.getMessage());391 return parseWebDriverException(e);392 }393 }394 @Override395 public MessageEvent clearField(final Session session, final Identifier identifier) {396 try {397 final TouchAction action = new TouchAction(session.getAppiumDriver());398 if (identifier.isSameIdentifier(Identifier.Identifiers.COORDINATE)) {399 final Coordinates coordinates = getCoordinates(identifier);400 click(session, identifier);401 } else {402 click(session, identifier);403 //action.press(ElementOption.element(getElement(session, identifier, false, false))).waitAction(WaitOptions.waitOptions(Duration.ofMillis(8000))).release().perform();404 //MobileElement element = (MobileElement) session.getAppiumDriver().findElementByAccessibilityId("SomeAccessibilityID");405 //element.clear();406 // WebElement elmt = this.getElement(session, identifier, false, false);407 ((MobileElement) this.getElement(session, identifier, false, false)).clear();408 }409 return new MessageEvent(MessageEventEnum.ACTION_SUCCESS_CLEAR).resolveDescription("ELEMENT", identifier.toString());410 } catch (NoSuchElementException e) {411 if (LOG.isDebugEnabled()) {412 LOG.debug(e.getMessage());413 }...
Coordinates
Using AI Code Generation
1public Coordinates getCoordinates() {2return new Coordinates() {3public Point onScreen() {4return new Point(0, 0);5}6public Point inViewPort() {7return new Point(0, 0);8}9public Point onPage() {10return new Point(0, 0);11}12};13}14public Coordinates getCoordinates() {15return new Coordinates() {16public Point onScreen() {17return new Point(0, 0);18}19public Point inViewPort() {20return new Point(0, 0);21}22public Point onPage() {23return new Point(0, 0);24}25};26}
Coordinates
Using AI Code Generation
1import org.cerberus.service.appium.impl.Coordinates;2Coordinates coordinates = new Coordinates();3coordinates.setCoordinates("0,0");4import org.cerberus.service.appium.impl.Coordinates;5Coordinates coordinates = new Coordinates();6coordinates.setCoordinates("0,0");7import org.cerberus.service.appium.impl.Coordinates;8Coordinates coordinates = new Coordinates();9coordinates.setCoordinates("0,0");10import org.cerberus.service.appium.impl.Coordinates;11Coordinates coordinates = new Coordinates();12coordinates.setCoordinates("0,0");13import org.cerberus.service.appium.impl.Coordinates;14Coordinates coordinates = new Coordinates();15coordinates.setCoordinates("0,0");16import org.cerberus.service.appium.impl.Coordinates;17Coordinates coordinates = new Coordinates();18coordinates.setCoordinates("0,0");19import org.cerberus.service.appium.impl.Coordinates;20Coordinates coordinates = new Coordinates();21coordinates.setCoordinates("0,0");22import org.cerberus.service.appium.impl.Coordinates;23Coordinates coordinates = new Coordinates();24coordinates.setCoordinates("0,0");
Coordinates
Using AI Code Generation
1WebElement element = driver.findElement(By.id("com.android.calculator2:id/digit_1"));2Coordinates coordinates = new Coordinates(element);3coordinates.tap();4coordinates.tap(100);5coordinates.tap(100, 10);6coordinates.tap(100, 10, 1);7coordinates.tap(100, 10, 1, 0.5);8coordinates.tap(100, 10, 1, 0.5, 0);9coordinates.tap(100, 10, 1, 0.5, 0, 0);10coordinates.tap(100, 10, 1, 0.5, 0, 0, 0);11coordinates.tap(100, 10, 1, 0.5, 0, 0, 0, 0);
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!