appium long press and than move element(drag and drop) is not working

Viewed 12624

I have a scenario to test an IOS app like this:

  1. long press on an element.
  2. move that element to desired location.

I am using the following code:

TouchAction action = new TouchAction(driver)
action.long_press(element1).move_to(element2).wait(500).release().perform()

but its not working for me. Need any good suggestion.

6 Answers

I found none of the longPress() combinations to work, so I got to this variant where you force it to perform the press and then move. Tested on Android and iOS, didn't seem to work for UWP

new TouchAction(driver)
    .press(PointOption.point(256, 1115))
    .waitAction(WaitOptions.waitOptions(Duration.ofMillis(2000)))
    .perform()
    .moveTo(PointOption.point(256, 600))
    .release()
    .perform();
//You need to import following 
import org.openqa.selenium.WebElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.touch.LongPressOptions;
import io.appium.java_client.touch.offset.ElementOption;

//first for the intial location to be long pressed
WebElement first= driver.findElementByXPath("//*[@content-desc='15']");

//second location on which you need to move to
WebElement second= driver.findElementByXPath("//*[@content-desc='45']");

TouchAction action = new TouchAction(driver);

//performing the long press
action.longPress(new LongPressOptions().withElement(new 
                       ElementOption().withElement(first))).perform();

//performing the move to touch operation
action.moveTo(new ElementOption().withElement(second)).perform();

if U have references of elements already then you'll do like this:

TouchAction action = new TouchAction(driver);
        action.longPress(new ElementOption().withElement(first))
                .waitAction(WaitOptions.waitOptions(Duration.ofMillis(3000)))
                .moveTo(new ElementOption().withElement(last))
                .release()
                .perform();

Following solution works for me on iOS with python:

TouchAction(context.driver).long_press(source_element).move_to(destination_element).release().perform()

Related