@Test public void dragNDropByElementTest() { Activity activity = new Activity("io.appium.android.apis", ".view.DragAndDropDemo"); driver.startActivity(activity); WebElement dragDot1 = driver.findElement(By.id("io.appium.android.apis:id/drag_dot_1")); WebElement dragDot3 = driver.findElement(By.id("io.appium.android.apis:id/drag_dot_3")); WebElement dragText = driver.findElement(By.id("io.appium.android.apis:id/drag_text")); assertEquals("Drag text not empty", "", dragText.getText()); TouchAction dragNDrop = new TouchAction(driver) .longPress(element(dragDot1)) .moveTo(element(dragDot3)) .release(); dragNDrop.perform(); assertNotEquals("Drag text empty", "", dragText.getText()); }
@Test public void dragNDropByElementAndDurationTest() { Activity activity = new Activity("io.appium.android.apis", ".view.DragAndDropDemo"); driver.startActivity(activity); WebElement dragDot1 = driver.findElement(By.id("io.appium.android.apis:id/drag_dot_1")); WebElement dragDot3 = driver.findElement(By.id("io.appium.android.apis:id/drag_dot_3")); WebElement dragText = driver.findElement(By.id("io.appium.android.apis:id/drag_text")); assertEquals("Drag text not empty", "", dragText.getText()); TouchAction dragNDrop = new TouchAction(driver) .longPress(longPressOptions() .withElement(element(dragDot1)) .withDuration(ofSeconds(2))) .moveTo(element(dragDot3)) .release(); dragNDrop.perform(); assertNotEquals("Drag text empty", "", dragText.getText()); }
@Test public void dragNDropByCoordinatesTest() { Activity activity = new Activity("io.appium.android.apis", ".view.DragAndDropDemo"); driver.startActivity(activity); AndroidElement dragDot1 = driver.findElement(By.id("io.appium.android.apis:id/drag_dot_1")); AndroidElement dragDot3 = driver.findElement(By.id("io.appium.android.apis:id/drag_dot_3")); WebElement dragText = driver.findElement(By.id("io.appium.android.apis:id/drag_text")); assertEquals("Drag text not empty", "", dragText.getText()); Point center1 = dragDot1.getCenter(); Point center2 = dragDot3.getCenter(); TouchAction dragNDrop = new TouchAction(driver) .longPress(point(center1.x, center1.y)) .moveTo(point(center2.x, center2.y)) .release(); dragNDrop.perform(); assertNotEquals("Drag text empty", "", dragText.getText()); }
@Test public void dragNDropByCoordinatesAndDurationTest() { Activity activity = new Activity("io.appium.android.apis", ".view.DragAndDropDemo"); driver.startActivity(activity); AndroidElement dragDot1 = driver.findElement(By.id("io.appium.android.apis:id/drag_dot_1")); AndroidElement dragDot3 = driver.findElement(By.id("io.appium.android.apis:id/drag_dot_3")); WebElement dragText = driver.findElement(By.id("io.appium.android.apis:id/drag_text")); assertEquals("Drag text not empty", "", dragText.getText()); Point center1 = dragDot1.getCenter(); Point center2 = dragDot3.getCenter(); TouchAction dragNDrop = new TouchAction(driver) .longPress(longPressOptions() .withPosition(point(center1.x, center1.y)) .withDuration(ofSeconds(2))) .moveTo(point(center2.x, center2.y)) .release(); dragNDrop.perform(); assertNotEquals("Drag text empty", "", dragText.getText()); }
public void longPressByImage(String targetImgPath) { Point2D coords = getCoords(takeScreenshot(), targetImgPath); if ((coords.getX() >= 0) && (coords.getY() >= 0)) { TouchAction touchA = new TouchAction(driver); touchA.longPress((int) coords.getX(), (int) coords.getY(), 1000).release().perform(); } else { throw new ElementNotVisibleException("Element not found - " + targetImgPath); } } // Convenience method to long press an element on screen with customisable press duration
public void longPressByImage(String targetImgPath, int pressDuration) { Point2D coords = getCoords(takeScreenshot(), targetImgPath); if ((coords.getX() >= 0) && (coords.getY() >= 0)) { TouchAction touchA = new TouchAction(driver); touchA.longPress((int) coords.getX(), (int) coords.getY(), pressDuration).release().perform(); } else { throw new ElementNotVisibleException("Element not found - " + targetImgPath); } }
public static void LongPressCoordinate(int x, int y){ log.info("LongPressCoordinate : x :" + x + " y : " + y); TouchAction touchAction = new TouchAction(driver); int endX = x; int endY = y + 10; try { //touchAction.longPress(PointOption.point(x,y)).waitAction(WaitOptions.waitOptions(Duration.ofSeconds(10))).perform(); if(!Util.isAndroid()){ endX = 0; y = 10; } touchAction.longPress(PointOption.point(x, y)).waitAction(WaitOptions.waitOptions(Duration.ofSeconds(10))) .moveTo(PointOption.point(endX, endY)).release().perform(); }catch (Exception e){ e.printStackTrace(); log.error("Fail to do long press"); } }
/** * Performs a long press */ public void longPress() { TouchAction touchAction = new TouchAction(driver); touchAction.longPress(gestureBox).perform(); }
/** * @author wasiq.bhamla * @since Sep 18, 2018 3:21:26 PM */ public void longPress () { perform ("Performing long press on", e -> this.touch.waitAction (WaitOptions.waitOptions (ofMillis (this.afterTap))) .longPress (LongPressOptions.longPressOptions () .withElement (ElementOption.element (e))) .waitAction (WaitOptions.waitOptions (ofMillis (this.afterTap))) .perform ()); }
public static void drag(int headX, int headY, int tailX , int tailY){ log.info(MyLogger.getMethodName()); if(headY < 200){ headY = 200; } log.info("Drag operation " + " headX: " + headX + " headY: " + headY + " tailX: " + tailX + " tailY:" + tailY); TouchAction dragAction = new TouchAction(driver); try { if(!Util.isAndroid()){ tailX = tailX - headX; tailY = tailY - headY; } if(Util.isAndroid()) { dragAction.longPress(PointOption.point(headX, headY)).moveTo(PointOption.point(tailX, tailY)).release().perform(); }else { dragAction.longPress(PointOption.point(headX, headY)).waitAction(WaitOptions.waitOptions(Duration.ofSeconds(1))).moveTo(PointOption.point(tailX, tailY)).release().perform(); //.longPress(LongPressOptions.longPressOptions().withDuration(Duration.ofSeconds(3))) } }catch (Exception e){ log.error("Fail to perform drag operation"); } }
/** * Tap and Hold (LongPress) on element * * @param element ExtendedWebElement * @return boolean */ default public boolean longPress(ExtendedWebElement element) { //TODO: SZ migrate to FluentWaits try { WebDriver driver = getDriver(); @SuppressWarnings("rawtypes") TouchAction<?> action = new TouchAction((MobileDriver<?>) driver); LongPressOptions options = LongPressOptions.longPressOptions().withElement(ElementOption.element(element.getElement())); action.longPress(options).release().perform(); return true; } catch (Exception e) { LOGGER.info("Error occurs during longPress: " + e, e); } return false; }
/** * Tap and Hold (LongPress) on element * * @param element ExtendedWebElement * @return boolean */ public static boolean longPress(ExtendedWebElement element) { //TODO: SZ migrate to FluentWaits try { WebDriver driver = getDriver(); @SuppressWarnings("rawtypes") TouchAction<?> action = new TouchAction((MobileDriver<?>) driver); LongPressOptions options = LongPressOptions.longPressOptions().withElement(ElementOption.element(element.getElement())); action.longPress(options).release().perform(); return true; } catch (Exception e) { LOGGER.info("Error occurs during longPress: " + e, e); } return false; }
@Override public void longClickTopLeft(WebElement webElement) { logger.entering(webElement); Point currentPoint = webElement.getLocation(); TouchAction clickTopLeft = new TouchAction(this); clickTopLeft.longPress(currentPoint.getX(), currentPoint.getY()).release().perform(); logger.exiting(); }
@Override public Object invoke(final Object proxy, final MethodInfo methodInfo, final Configuration configuration) { final AppiumDriver driver = configuration.getContext(AppiumDriverContext.class) .orElseThrow(() -> new AtlasException("AppiumDriver is missing")).getValue(); final TouchAction action = new TouchAction(driver); final Point location = ((WebElement) proxy).getLocation(); final Dimension size = ((WebElement) proxy).getSize(); final int x = location.getX() + size.width / 2; final int y = location.getY() + size.height / 2; action.longPress(longPressOptions().withDuration(LONG_TAP_DURATION) .withPosition(new PointOption().withCoordinates(x, y))).perform(); return proxy; } }
@Override public void longClickBottomRight(WebElement webElement) { logger.entering(webElement); Point currentPoint = webElement.getLocation(); Dimension dimension = webElement.getSize(); TouchAction clickBottomRight = new TouchAction(this); int newX = currentPoint.getX() + dimension.getWidth(); int newY = currentPoint.getY() + dimension.getHeight(); clickBottomRight.longPress(newX - 1, newY - 1).release().perform(); logger.exiting(); }
@Override public Object invoke(final Object proxy, final MethodInfo methodInfo, final Configuration configuration) { final AppiumDriver driver = configuration.getContext(AppiumDriverContext.class) .orElseThrow(() -> new AtlasException("AppiumDriver is missing")).getValue(); given().atMost(120, SECONDS).pollInterval(1, SECONDS).ignoreExceptions().until(() -> { if (!displayed().matches(proxy)) { final Dimension size = driver.manage().window().getSize(); final int startX = size.width / 2; final int startY = (int) (size.height * BOTTOM.getValue()); final int endX = size.width / 2; final int endY = (int) (size.height * TOP.getValue()); final TouchAction touchAction = new TouchAction(driver); touchAction.longPress(new PointOption().withCoordinates(startX, startY)) .moveTo(new PointOption().withCoordinates(endX, endY)).release().perform(); } return displayed().matches(proxy); }); return proxy; } }
@Override public Object invoke(final Object proxy, final MethodInfo methodInfo, final Configuration configuration) { final AppiumDriver driver = configuration.getContext(AppiumDriverContext.class) .orElseThrow(() -> new AtlasException("AppiumDriver is missing")).getValue(); given().atMost(120, SECONDS).pollInterval(1, SECONDS).ignoreExceptions().until(() -> { if (!displayed().matches(proxy)) { final Dimension size = driver.manage().window().getSize(); final int startX = size.width / 2; final int startY = (int) (size.height * TOP.getValue()); final int endX = size.width / 2; final int endY = (int) (size.height * BOTTOM.getValue()); final TouchAction touchAction = new TouchAction(driver); touchAction.longPress(new PointOption().withCoordinates(startX, startY)) .moveTo(new PointOption().withCoordinates(endX, endY)).release().perform(); } return displayed().matches(proxy); }); return proxy; } }