Home > database >  How to use actions or do scroll in app in Java?
How to use actions or do scroll in app in Java?

Time:02-11

TouchAction is deprecated. Any pointers for new usage to do vertical Scroll? Below code used. But not working as TouchAction is deprecated.

Dimension dimension =driver.manage().window().getSize();
int start_x=(int) (dimension.width*0.5); 
int start_y=(int) (dimension.height*0.9); 
int end_x=(int) (dimension.width*0.2); 
int end_y=(int) (dimension.height*0.1); 
TouchAction touch =new TouchAction(driver); 
touch.press(PointOption.point(start_x,start_y))
   .waitAction(WaitOptions.waitOptions(Duration.ofSeconds(1)))
   .moveTo(PointOption.point(end_x, end_y))
   .release()
   .perform(); 
Thread.sleep(3000); 
dimension =driver.manage().window().getSize(); 
start_x=(int) (dimension.width*0.2); 
start_y=(int) (dimension.height*0.2); 
end_x=(int) (dimension.width*0.5); 
end_y=(int) (dimension.height*0.8); 
touch =new TouchAction(driver); 
touch.press(PointOption.point(start_x,start_y))
    .waitAction(WaitOptions.waitOptions(Duration.ofSeconds(1)))
    .moveTo(PointOption.point(end_x, end_y))
    .release()
    .perform();

CodePudding user response:

In Selenium 4/Appium 8 TouchActions replaced with W3C actions.

http://appium.io/docs/en/commands/interactions/actions/

For your example:

W3cActions Util Class

import org.openqa.selenium.Point
import org.openqa.selenium.interactions.PointerInput
import org.openqa.selenium.interactions.Sequence
import static java.time.Duration.ofMillis
import static org.openqa.selenium.interactions.PointerInput.Kind.TOUCH
import static org.openqa.selenium.interactions.PointerInput.MouseButton.LEFT
import static org.openqa.selenium.interactions.PointerInput.Origin.viewport

public class W3cActions {

    private final static PointerInput FINGER = new PointerInput(TOUCH, 'finger');

    public static void doSwipe(AppiumDriver driver, Point start, Point end, int duration) {
        Sequence swipe = new Sequence(FINGER, 1)
                .addAction(FINGER.createPointerMove(ofMillis(0), viewport(), start.getX(), start.getY()))
                .addAction(FINGER.createPointerDown(LEFT.asArg()))
                .addAction(FINGER.createPointerMove(ofMillis(duration), viewport(), end.getX(), end.getY()))
                .addAction(FINGER.createPointerUp(LEFT.asArg()));
        driver.perform(Arrays.asList(swipe));
    }

    public static void doTap(AppiumDriver driver, Point point, int duration) {
        Sequence tap = new Sequence(FINGER, 1)
                .addAction(FINGER.createPointerMove(ofMillis(0), viewport(), point.getX(), point.getY()))
                .addAction(FINGER.createPointerDown(LEFT.asArg()))
                .addAction(new Pause(FINGER, ofMillis(duration)))
                .addAction(FINGER.createPointerUp(LEFT.asArg()));
        driver.perform(Arrays.asList(tap));
    }

}

Swipe

Dimension dimension = driver.manage().window().getSize();
Point start = new Point((int)(dimension.width*0.5), (int)(dimension.height*0.9));
Point end = new Point((int)(dimension.width*0.2), (int)(dimension.height*0.1));
W3cActions.doSwipe(driver, start, end, 1000);  //with duration 1s

Thread.sleep(3000); 

start = new Point((int)(dimension.width*0.2), (int)(dimension.height*0.2));
end = new Point((int)(dimension.width*0.5), (int)(dimension.height*0.8)); 
W3cActions.doSwipe(driver, start, end, 1000); //with duration 1s

Tap

Dimension dimension = driver.manage().window().getSize();
Point forTap = new Point((int)(dimension.width*0.5), (int)(dimension.height*0.9));
W3cActions.doTap(driver, forTap, 200); //with duration 200ms
  • Related