mittalri
mittalri

Reputation: 21

How to use actions or do scroll in app in Java?

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();

Upvotes: 1

Views: 4032

Answers (3)

Sourabh Kumra
Sourabh Kumra

Reputation: 11

Dimension size = getDriver().manage().window().getSize();
    int startY = (int) (size.height * 0.70);
    int endY = (int) (size.height * -0.2);
    int centerX = size.width / 2;
    //Type of Pointer Input
    PointerInput finger = new PointerInput (PointerInput.Kind. TOUCH, "finger");
    //Creating Sequence object to add actions
    Sequence swipe = new Sequence(finger, 1);
    //Move finger into starting position
    swipe.addAction(finger.createPointerMove(Duration.ofSeconds(0),PointerInput.Origin.viewport(), centerX, (int)startY));
    //Finger comes down into contact with screen
    swipe.addAction(finger.createPointerDown(0));
    //Finger moves to end position
    swipe.addAction(finger.createPointerMove (Duration.ofMillis(1000),PointerInput.Origin.viewport(),centerX, (int)endY));
    //Get up Finger from Srceen
    swipe.addAction(finger.createPointerUp(0));
    //Perform the actions
    getDriver().perform(Arrays.asList(swipe));

Upvotes: 0

Divita Goswami
Divita Goswami

Reputation: 1

great solution for implied issue, however how do you tap on a particular element here? as in this case looks like tap is performed on some point on screen, i am trying to apply this to tap on one of the widget element on mobile screen

Upvotes: 0

Max Daroshchanka
Max Daroshchanka

Reputation: 2968

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

Upvotes: 6

Related Questions