Best Cerberus-source code snippet using org.cerberus.service.appium.impl.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
1import org.cerberus.service.appium.impl.Coordinates;2Coordinates coordinates = new Coordinates(0, 0);3import org.openqa.selenium.Coordinates;4Coordinates coordinates = new Coordinates(0, 0);5import org.openqa.selenium.interactions.Coordinates;6Coordinates coordinates = new Coordinates(0, 0);7import org.cerberus.service.appium.impl.Coordinates;8Coordinates coordinates = new Coordinates(0, 0);9import org.openqa.selenium.Coordinates;10Coordinates coordinates = new Coordinates(0, 0);11import org.openqa.selenium.interactions.Coordinates;12Coordinates coordinates = new Coordinates(0, 0);
Coordinates
Using AI Code Generation
1import org.cerberus.service.appium.impl.Coordinates;2import org.openqa.selenium.Point;3import org.openqa.selenium.WebElement;4public class TestAppium {5 public static void main(String[] args) {6 Coordinates coordinates = new Coordinates();7 System.out.println("Coordinates of the element: " + point);8 }9}10import io.appium.java_client.TouchAction;11import io.appium.java_client.android.AndroidDriver;12import io.appium.java_client.android.AndroidElement;13import io.appium.java_client.touch.offset.PointOption;14import org.openqa.selenium.By;15import org.openqa.selenium.Point;16import org.openqa.selenium.WebElement;17import java.net.MalformedURLException;18import java.util.concurrent.TimeUnit;19public class TestAppium {20 public static void main(String[] args) throws MalformedURLException {21 AndroidDriver<AndroidElement> driver = Capabilities();22 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);23 driver.findElementByAndroidUIAutomator("text(\"Views\")").click();24 WebElement expandList = driver.findElementByAndroidUIAutomator("text(\"Expandable Lists\")");25 Point point = expandList.getLocation();26 System.out.println("Coordinates of the element: " + point);27 TouchAction touchAction = new TouchAction(driver);28 touchAction.tap(PointOption.point(point.x, point.y)).perform();29 }30 public static AndroidDriver<AndroidElement> Capabilities() throws MalformedURLException {31 AndroidDriver<AndroidElement> driver;32 DesiredCapabilities capabilities = new DesiredCapabilities();33 capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Device");34 capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");35 capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "10.0");
Coordinates
Using AI Code Generation
1import org.cerberus.service.appium.impl.Coordinates;2import org.openqa.selenium.WebDriver;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.remote.RemoteWebDriver;5import java.util.HashMap;6import java.util.Map;7public class TestAppium {8 public static void main(String[] args) {9 WebElement element = driver.findElement(By.id("some_id"));10 Map<String, Object> params = new HashMap<>();11 params.put("element", ((RemoteWebElement) element).getId());12 params.put("x", 100);13 params.put("y", 100);14 driver.executeScript("mobile: tap", params);15 driver.quit();16 }17}18import org.cerberus.service.appium.impl.AppiumService;19import org.cerberus.service.appium.impl.Coordinates;20import org.openqa.selenium.WebDriver;21import org.openqa.selenium.WebElement;22import org.openqa.selenium.remote.RemoteWebDriver;23import java.util.HashMap;24import java.util.Map;25public class TestAppium {26 public static void main(String[] args) {27 WebElement element = driver.findElement(By.id("some_id"));28 new AppiumService().tap(new Coordinates(element), driver);29 driver.quit();30 }31}
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!!