How to use getX method of org.cerberus.service.appium.impl.Coordinates class

Best Cerberus-source code snippet using org.cerberus.service.appium.impl.Coordinates.getX

Source:AppiumService.java Github

copy

Full Screen

...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 }...

Full Screen

Full Screen

getX

Using AI Code Generation

copy

Full Screen

1WebElement element = driver.findElement(By.id("id"));2int x = element.getCoordinates().getX();3System.out.println("X coordinate of the element is : " + x);4WebElement element = driver.findElement(By.id("id"));5int y = element.getCoordinates().getY();6System.out.println("Y coordinate of the element is : " + y);7WebElement element = driver.findElement(By.id("id"));8Point location = element.getCoordinates().getLocation();9System.out.println("Location of the element is : " + location);10WebElement element = driver.findElement(By.id("id"));11Point locationInDom = element.getCoordinates().getLocationInDom();12System.out.println("Location in DOM of the element is : " + locationInDom);13WebElement element = driver.findElement(By.id("id"));14Point locationOnScreen = element.getCoordinates().getLocationOnScreen();15System.out.println("Location on screen of the element is : " + locationOnScreen);16WebElement element = driver.findElement(By.id("id"));17Dimension size = element.getCoordinates().getSize();18System.out.println("Size of the element is : " + size);

Full Screen

Full Screen

getX

Using AI Code Generation

copy

Full Screen

1package com.cerberus.appium;2import io.appium.java_client.AppiumDriver;3import io.appium.java_client.MobileElement;4import io.appium.java_client.android.AndroidDriver;5import io.appium.java_client.android.AndroidElement;6import io.appium.java_client.android.AndroidKeyCode;7import io.appium.java_client.android.AndroidTouchAction;8import io.appium.java_client.remote.MobileCapabilityType;9import io.appium.java_client.service.local.AppiumDriverLocalService;10import io.appium.java_client.service.local.AppiumServiceBuilder;11import io.appium.java_client.service.local.flags.GeneralServerFlag;12import io.appium.java_client.touch.WaitOptions;13import io.appium.java_client.touch.offset.PointOption;14import org.openqa.selenium.By;15import org.openqa.selenium.Dimension;16import org.openqa.selenium.Point;17import org.openqa.selenium.WebElement;18import org.openqa.selenium.remote.DesiredCapabilities;19import org.openqa.selenium.support.ui.WebDriverWait;20import org.testng.annotations.AfterTest;21import org.testng.annotations.BeforeTest;22import org.testng.annotations.Test;23import java.io.File;24import java.net.MalformedURLException;25import java.net.URL;26import java.time.Duration;27import java.util.List;28import java.util.concurrent.TimeUnit;29public class GetXCoordinateOfElement {30 public static AppiumDriver<MobileElement> driver;31 public static AndroidDriver<AndroidElement> driver1;32 public static WebDriverWait wait;33 public void setup() throws MalformedURLException {34 AppiumDriverLocalService service = AppiumDriverLocalService.buildService(new AppiumServiceBuilder()35 .usingDriverExecutable(new File("C:\\Program Files\\nodejs\\node.exe"))36 .withAppiumJS(new File("C:\\Users\\admin\\AppData\\Roaming\\npm\\node_modules\\appium\\build\\lib\\main.js"))37 .withLogFile(new File("C:\\Users\\admin\\AppData\\Local\\Programs\\Appium\\logs\\appiumLogs.txt"))38 .withArgument(GeneralServerFlag.LOCAL_TIMEZONE)39 .withIPAddress("

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Cerberus-source automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in Coordinates

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful